nepada/consistence-doctrine 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nepada/consistence-doctrine

最新稳定版本:v1.1.1

Composer 安装命令:

composer require nepada/consistence-doctrine

包简介

Consistence Enum types for Doctrine.

README 文档

README

Build Status Coverage Status Downloads this Month Latest stable

Package abandoned

This package is considered obsolete and abandoned. Since PHP 8.1 introduced native enum support, there is no need for user-land implementation of enums, neither their custom integration into Doctrine.

Installation

Via Composer:

$ composer require nepada/consistence-doctrine

Usage

Define Doctrine type for the enum

/**
 * @phpstan-extends \Nepada\ConsistenceDoctrine\StringEnumType<\FooEnum>
 */
class FooEnumType extends \Nepada\ConsistenceDoctrine\StringEnumType
{

    protected function getEnumClassName(): string
    {
        return \FooEnum::class;
    }

}

You can choose to inherit from StringEnumType, IntegerEnumType, FloatEnumType or BooleanEnumType depending on the enum values.

Register the created type in Doctrine

\Doctrine\DBAL\Types\Type::addType(\FooEnum::class, \FooEnumType::class);

In Nette with nettrine/dbal integration, you can register the types in your configuration:

dbal:
    connection:
        types:
            FooEnum: FooEnumType

Use the type in entity

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class SomeEntity
{

    /** @ORM\Column(type=\FooEnum::class, nullable=false) */
    private \FooEnum $foo;

    // ...

}

Use enum in query builder

$result = $repository->createQueryBuilder('bar')
    ->select('bar.foo') // FooEnum instances are created during hydratation
    ->where('bar.foo = :fooEnum')
    ->setParameter('fooEnum', \FooEnum::get(\FooEnum::VALUE), \FooEnum::class) // enum instance gets serialized
    ->getQuery()
    ->setMaxResults(1)
    ->getSingleResult();

PHPStan support (via phpstan/phpstan-doctrine)

The abstract enum type classes are anotated as PHPStan generics and define proper typehints for their convert* methods. This means you can teach PHPStan your custom enum types via ReflectionDescriptor:

services:
    -
        factory: PHPStan\Type\Doctrine\Descriptors\ReflectionDescriptor(FooEnumType)
        tags: [phpstan.doctrine.typeDescriptor]

Differences from the official consistence/consistence-doctrine

The official integration consistence/consistence-doctrine uses postload entity event to convert data stored in database to enum instances.

The main advantage of that approach is that you don't need to create and register new doctrine type for every enum.

The disadvantages are:

  • You can't use native property typehints for entity enum attributes.
  • When you're not hydrating entites, you will get enum values instead of enum instances.
  • There is no easy way how to make PHPStan understand and check doctrine and PHP types of your enum fields.

统计信息

  • 总下载量: 13.73k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2020-03-29