brandembassy / doctrine-enum-type
Installs: 188 961
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 7
Forks: 0
Open Issues: 0
Requires
- php: >=8.1
- doctrine/orm: ^2.14.3
Requires (Dev)
- brandembassy/coding-standard: ^11.2
- doctrine/annotations: ^2.0
- marc-mabe/php-enum: ^3.2 || ^4.4
- mockery/mockery: ^1.0
- myclabs/php-enum: ^1.6
- nette/neon: ^2.4
- phpunit/phpunit: ^10.5
- roave/security-advisories: dev-latest
- symfony/cache: ^5.4 || ^6.2
Suggests
- marc-mabe/php-enum: For marc-mabe/php-enum enums support
- myclabs/php-enum: For myclabs/php-enum enums support
This package is auto-updated.
Last update: 2024-11-16 14:50:06 UTC
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.