diff --git a/src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php b/src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php index 9add7946fbea5..8ebd5fbe00b34 100644 --- a/src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php +++ b/src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php @@ -152,17 +152,18 @@ public function getTypes($class, $property, array $context = []) } if ($metadata->hasField($property)) { - $nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property); - if (null !== $enumClass = $metadata->getFieldMapping($property)['enumType'] ?? null) { - return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $enumClass)]; - } - $typeOfField = $metadata->getTypeOfField($property); if (!$builtinType = $this->getPhpType($typeOfField)) { return null; } + $nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property); + $enumType = null; + if (null !== $enumClass = $metadata->getFieldMapping($property)['enumType'] ?? null) { + $enumType = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $enumClass); + } + switch ($builtinType) { case Type::BUILTIN_TYPE_OBJECT: switch ($typeOfField) { @@ -192,11 +193,22 @@ public function getTypes($class, $property, array $context = []) case self::$useDeprecatedConstants ? DBALType::TARRAY : Types::ARRAY: // no break case 'json_array': + // return null if $enumType is set, because we can't determine if collectionKeyType is string or int + if ($enumType) { + return null; + } + return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)]; case self::$useDeprecatedConstants ? DBALType::SIMPLE_ARRAY : Types::SIMPLE_ARRAY: - return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))]; + return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, new Type(Type::BUILTIN_TYPE_INT), $enumType ?? new Type(Type::BUILTIN_TYPE_STRING))]; + } + case Type::BUILTIN_TYPE_INT: + case Type::BUILTIN_TYPE_STRING: + if ($enumType !== null) { + return [$enumType]; } + // no break } return [new Type($builtinType, $nullable)]; diff --git a/src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php b/src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php index b1e327968242a..32caea8bd3093 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php @@ -185,6 +185,9 @@ public function testExtractEnum() } $this->assertEquals([new Type(Type::BUILTIN_TYPE_OBJECT, false, EnumString::class)], $this->createExtractor()->getTypes(DoctrineEnum::class, 'enumString', [])); $this->assertEquals([new Type(Type::BUILTIN_TYPE_OBJECT, false, EnumInt::class)], $this->createExtractor()->getTypes(DoctrineEnum::class, 'enumInt', [])); + $this->assertEquals(null, $this->createExtractor()->getTypes(DoctrineEnum::class, 'enumStringArray', [])); + $this->assertEquals([new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_OBJECT, false, EnumInt::class))], $this->createExtractor()->getTypes(DoctrineEnum::class, 'enumIntArray', [])); + $this->assertEquals(null, $this->createExtractor()->getTypes(DoctrineEnum::class, 'enumCustom', [])); } public function typesProvider() diff --git a/src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/DoctrineEnum.php b/src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/DoctrineEnum.php index 467522cbd3914..fd5271fc47730 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/DoctrineEnum.php +++ b/src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/DoctrineEnum.php @@ -35,4 +35,19 @@ class DoctrineEnum * @Column(type="integer", enumType="Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumInt") */ protected $enumInt; + + /** + * @Column(type="array", enumType="Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumString") + */ + protected $enumStringArray; + + /** + * @Column(type="simple_array", enumType="Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumInt") + */ + protected $enumIntArray; + + /** + * @Column(type="custom_foo", enumType="Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumInt") + */ + protected $enumCustom; }