-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Description
Bug Report
| Q | A |
|---|---|
| Version | 2.20.9 |
Summary
Using a PHP backed enum with a Doctrine field mapped as SIMPLE_ARRAY (array of enums) breaks hydration when the column is non‑nullable and/or relies on a default value.
This leads to a runtime failure in Doctrine\ORM\Internal\Hydration\SimpleObjectHydrator.
Attempting to solve this with a database-level default value also hits a MySQL 8.0 limitation. MySQL does not allow default values for TEXT columns (which SIMPLE_ARRAY maps to), and the generated migration therefore fails.
Even if manually adjusted, the next schema diff removes the fix and reintroduces the error.
Current behavior
#[ORM\Column(type: Types::SIMPLE_ARRAY, nullable: false, enumType: MyEnum::class, options: ['default' => '[]'])]
private array $myEnums = [];Hydration error:
Context: Trying to hydrate enum property "App\Entity\MyEntity::$myEnums"
Problem: Case "[]" is not listed in enum "App\Entity\MyEnum"
Solution: Either add the case to the enum type or migrate the database column to use another case of the enum
Expected behavior
Doctrine ORM should be able to hydrate entities that use Types::SIMPLE_ARRAY with enumType reliably on MySQL 8.0, including non‑nullable columns.
If Doctrine or Doctrine Migrations generates a default value for this column type, the generated SQL should be valid for MySQL 8.0.
Example MySQL error when the generated migration is executed:
Syntax error or access violation: 1101 BLOB, TEXT, GEOMETRY or JSON column 'equipement_groupes' can't have a default value
How to reproduce
- Use PHP 8.1+ with a backed enum:
enum MyEnum: string
{
case A = 'A';
case B = 'B';
}- Create an entity with a non-nullable enum array property:
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class Foo
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: Types::INTEGER)]
private int $id;
#[ORM\Column(type: Types::SIMPLE_ARRAY, nullable: false, enumType: MyEnum::class, options: ['default' => '[]'])]
private array $myEnums = [];
}- Run schema/migration generation for MySQL 8.0 and apply it.
- Insert a row where my_enums is not defined to fallback to the default value
- Fetch the entity via ORM (e.g. repository find() / DQL query).
- Doctrine throws during hydration in: Doctrine\ORM\Internal\Hydration\SimpleObjectHydrator
Extra debug
Nullable
#[ORM\Column(type: Types::SIMPLE_ARRAY, enumType: MyEnum::class)]
private array $myNonNullableEnums = [];Works as expected, but NULL values are allowed.
NOT nullable
#[ORM\Column(type: Types::SIMPLE_ARRAY, nullable: false, enumType: MyEnum::class)]
private array $myNonNullableEnums = [];ALTER TABLE my_table ADD my_non_nullable_enums LONGTEXT NOT NULL COMMENT '(DC2Type:simple_array)',When fetching from doctrine
Context: Trying to hydrate enum property "App\Entity\MyEntity::$myNonNullableWithDefaultEnums "
Problem: Case "" is not listed in enum "App\Entity\MyEnum"
Solution: Either add the case to the enum type or migrate the database column to use another case of the enum
NOT nullable with default value
#[ORM\Column(type: Types::SIMPLE_ARRAY, nullable: false, enumType: MyEnum::class, options: ['default' => '[]'])]
private array $myNonNullableWithDefaultEnums = [];Generated migration:
ALTER TABLE my_table ADD my_non_nullable_with_default_enums LONGTEXT DEFAULT '[]' NOT NULL COMMENT '(DC2Type:simple_array)'MySQL error:
SQLSTATE[42000]: Syntax error or access violation: 1101 BLOB, TEXT, GEOMETRY or JSON column 'equipement_groupes' can't have a default valueThis requires manual modification of the migration.
- ALTER TABLE my_table ADD my_non_nullable_with_default_enums LONGTEXT DEFAULT '[]' NOT NULL COMMENT '(DC2Type:simple_array)'
+ ALTER TABLE my_table ADD my_non_nullable_with_default_enums LONGTEXT DEFAULT ('[]') NOT NULL COMMENT '(DC2Type:simple_array)'Even after manually adjusting the migration, Doctrine still throws during hydration:
Context: Trying to hydrate enum property "App\Entity\MyEntity::$myNonNullableWithDefaultEnums "
Problem: Case "[]" is not listed in enum "App\Entity\MyEnum"
Solution: Either add the case to the enum type or migrate the database column to use another case of the enum
If the default value is an empty string instead of an empty array, the message changes slightly but the underlying issue remains: Problem: Case "" is not listed in enum "App\Entity\MyEnum"