brandembassy/doctrine-enum-type
最新稳定版本:1.3.0
Composer 安装命令:
composer require brandembassy/doctrine-enum-type
包简介
无描述信息
README 文档
README
Doctrine Enum Type
This extension brings easy usage of enums in your Doctrine entities.
Why to use enums
- You can type-check passed value
- You document what values are valid
- You can add useful methods to your enum class
Installation
composer require brandembassy/doctrine-enum-type Choosing enum library
Currently three enum libraries are supported out of the box:
Don't worry, you can use another Enum implementation, just create bridge implementing BrandEmbassy\Doctrine\EnumType\EnumImplementation interface.
Usage
Setup Doctrine
Let's say we have this enum with colors:
class Color extends Enum { public const BLACK = 'black'; public const WHITE = 'white'; public const RED = 'read'; public const GREEN = 'green'; public const BLUE = 'blue'; }
Now we need to tell Doctrine about our enum class. You can use BrandEmbassy\Doctrine\EnumType\EnumTypesManager for it:
use BrandEmbassy\Doctrine\EnumType\Bridges\MarcMabeEnum\MarcMabeEnumBridge; use BrandEmbassy\Doctrine\EnumType\EnumTypesManager; $enumTypesManager = new EnumTypesManager(new MarcMabeEnumBridge()); $enumTypesManager->addEnumTypeDefinition('enumColor', Color::class); $enumTypesManager->initializeEnumTypes();
This initialization must be done before working with entities or schema that is using your enums.
First parameter of EnumTypesManager is object implementing BrandEmbassy\Doctrine\EnumType\EnumImplementation that describes enum implementation you are using.
Method addEnumTypeDefinition accepts the name of your enum that you want to use in your entities, second one is your enum class.
Start using enums in your entities
We are ready to go, we can now define our car entity using color enum:
use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="car") */ final class Car { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue * @var int */ private $id; /** * @ORM\Column(type="string") * @var string */ private $brand; /** * @ORM\Column(type="enumColor") * @var Color */ private $color; public function __construct(string $brand, Color $color) { $this->brand = $brand; $this->color = $color; } ... public function getColor(): Color { return $this->color; } public function setColor(Color $color): void { $this->color = $color; } } $user = new User('Skoda', Color::get(Color::GREEN));
Internals
Database columns generated by this library are of type VARCHAR(255). This brings one constraint to your enums: value of your enum can't be longer than 255 characters.
brandembassy/doctrine-enum-type 适用场景与选型建议
brandembassy/doctrine-enum-type 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 222.99k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 brandembassy/doctrine-enum-type 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 brandembassy/doctrine-enum-type 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 222.99k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04