From 37ee36b8cd8c281c1ce81afbd8126315261eeb19 Mon Sep 17 00:00:00 2001 From: Matthieu Lempereur Date: Wed, 22 Nov 2023 12:02:25 +0100 Subject: [PATCH 1/5] documentation for enum usage with doctrine --- doctrine.rst | 1 + doctrine/use_enum.rst | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 doctrine/use_enum.rst diff --git a/doctrine.rst b/doctrine.rst index 9f1abdabcaf..3bdbe6602ab 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_enums 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..47914d4b439 --- /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\Enum; + + #[Column(type: Types::TEXT, enumType: Suit::class)] + public string $suit; + + // or for a more typed property + #[Column(type: Types::TEXT)] + 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 From 612483ef6a97c625cd934ac06475f725d902290c Mon Sep 17 00:00:00 2001 From: Matthieu Lempereur Date: Wed, 22 Nov 2023 12:06:53 +0100 Subject: [PATCH 2/5] remove trailing whitespaces --- doctrine.rst | 2 +- doctrine/use_enum.rst | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/doctrine.rst b/doctrine.rst index 3bdbe6602ab..b9d1ed0b3a5 100644 --- a/doctrine.rst +++ b/doctrine.rst @@ -1076,7 +1076,7 @@ Learn more doctrine/multiple_entity_managers doctrine/resolve_target_entity doctrine/reverse_engineering - doctrine/use_enums + doctrine/use_enum testing/database .. _`Doctrine`: https://www.doctrine-project.org/ diff --git a/doctrine/use_enum.rst b/doctrine/use_enum.rst index 47914d4b439..818cb8f8f90 100644 --- a/doctrine/use_enum.rst +++ b/doctrine/use_enum.rst @@ -2,7 +2,7 @@ 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 +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:: @@ -12,8 +12,8 @@ First step is to create an enum:: enum Suit: string { case Hearts = 'H'; - case Diamonds = 'D'; - case Clubs = 'C'; + case Diamonds = 'D'; + case Clubs = 'C'; case Spades = 'S'; } @@ -22,7 +22,7 @@ First step is to create an enum:: 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 +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 From 5a87539bedbc2963f1680e9f9cb407b0b0a88199 Mon Sep 17 00:00:00 2001 From: Matthieu Lempereur Date: Wed, 22 Nov 2023 12:08:08 +0100 Subject: [PATCH 3/5] trailing whitespace --- doctrine/use_enum.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doctrine/use_enum.rst b/doctrine/use_enum.rst index 818cb8f8f90..9ebb7e9dced 100644 --- a/doctrine/use_enum.rst +++ b/doctrine/use_enum.rst @@ -13,7 +13,7 @@ First step is to create an enum:: enum Suit: string { case Hearts = 'H'; case Diamonds = 'D'; - case Clubs = 'C'; + case Clubs = 'C'; case Spades = 'S'; } From 6ff1b9713742ab82d2fe397a09da53daf8a61cf3 Mon Sep 17 00:00:00 2001 From: Matthieu Lempereur Date: Wed, 22 Nov 2023 12:13:49 +0100 Subject: [PATCH 4/5] syntax --- doctrine/use_enum.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doctrine/use_enum.rst b/doctrine/use_enum.rst index 9ebb7e9dced..1aa93799d5e 100644 --- a/doctrine/use_enum.rst +++ b/doctrine/use_enum.rst @@ -15,7 +15,7 @@ First step is to create an enum:: case Diamonds = 'D'; case Clubs = 'C'; case Spades = 'S'; -} + } .. note:: From 54604137bbcba3e296507ea83054cc0f40c71a13 Mon Sep 17 00:00:00 2001 From: Matthieu Lempereur Date: Wed, 22 Nov 2023 22:26:52 +0100 Subject: [PATCH 5/5] review --- doctrine/use_enum.rst | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/doctrine/use_enum.rst b/doctrine/use_enum.rst index 1aa93799d5e..38486d11777 100644 --- a/doctrine/use_enum.rst +++ b/doctrine/use_enum.rst @@ -10,11 +10,11 @@ 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'; + enum Suit: string { + case Hearts = 'H'; + case Diamonds = 'D'; + case Clubs = 'C'; + case Spades = 'S'; } .. note:: @@ -26,13 +26,13 @@ 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\Enum; + namespace App\Entity; - #[Column(type: Types::TEXT, enumType: Suit::class)] + #[Column(type: Types::STRING, enumType: Suit::class)] public string $suit; // or for a more typed property - #[Column(type: Types::TEXT)] + #[Column(type: Types::STRING)] public Suit $suit; .. caution::