diff --git a/doctrine.rst b/doctrine.rst index 9f1abdabcaf..b9d1ed0b3a5 100644 --- a/doctrine.rst +++ b/doctrine.rst @@ -1076,6 +1076,7 @@ Learn more doctrine/multiple_entity_managers doctrine/resolve_target_entity doctrine/reverse_engineering + doctrine/use_enum testing/database .. _`Doctrine`: https://www.doctrine-project.org/ diff --git a/doctrine/use_enum.rst b/doctrine/use_enum.rst new file mode 100644 index 00000000000..38486d11777 --- /dev/null +++ b/doctrine/use_enum.rst @@ -0,0 +1,43 @@ +Use Enums +========= + +PHP 8.1 added native Enumerations, which can be used to define a custom type +and its possible values. Those can be used in association with Doctrine in +order to define a limited set of values availables for an entity property. + +First step is to create an enum:: + + // src/Enum/Suit.php + namespace App\Enum; + + enum Suit: string { + case Hearts = 'H'; + case Diamonds = 'D'; + case Clubs = 'C'; + case Spades = 'S'; + } + +.. note:: + + Only backed enums can be used with properties as Doctrine use the scalar + equivalent of each value for storing. + +When the enum is created, you can use the ``enumType`` parameter of +``#[ORM\Column]`` attribute or use it directly for a more typed property:: + + // src/Entity/Card.php + namespace App\Entity; + + #[Column(type: Types::STRING, enumType: Suit::class)] + public string $suit; + + // or for a more typed property + #[Column(type: Types::STRING)] + public Suit $suit; + +.. caution:: + + If you use the Symfony Maker bundle to create or update your entities, + there is no EnumType available. It still can be used to generate property + with getter and setter but you will need to update declaration according + to your needs. \ No newline at end of file