[DoctrineBridge][DoctrineExtractor] Fix indexBy with custom and some core types

This commit is contained in:
Thomas Calvet 2020-02-19 23:15:06 +01:00
parent 212841b3e6
commit 018ec1ae5c
4 changed files with 84 additions and 27 deletions

View File

@ -117,7 +117,9 @@ class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeE
$typeOfField = $subMetadata->getTypeOfField($indexProperty); $typeOfField = $subMetadata->getTypeOfField($indexProperty);
} }
$collectionKeyType = $this->getPhpType($typeOfField); if (!$collectionKeyType = $this->getPhpType($typeOfField)) {
return null;
}
} }
} }
@ -137,39 +139,46 @@ class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeE
if ($metadata->hasField($property)) { if ($metadata->hasField($property)) {
$typeOfField = $metadata->getTypeOfField($property); $typeOfField = $metadata->getTypeOfField($property);
if (!$builtinType = $this->getPhpType($typeOfField)) {
return null;
}
$nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property); $nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property);
switch ($typeOfField) { switch ($builtinType) {
case DBALType::DATE: case Type::BUILTIN_TYPE_OBJECT:
case DBALType::DATETIME: switch ($typeOfField) {
case DBALType::DATETIMETZ: case DBALType::DATE:
case 'vardatetime': case DBALType::DATETIME:
case DBALType::TIME: case DBALType::DATETIMETZ:
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTime')]; case 'vardatetime':
case DBALType::TIME:
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTime')];
case 'date_immutable': case 'date_immutable':
case 'datetime_immutable': case 'datetime_immutable':
case 'datetimetz_immutable': case 'datetimetz_immutable':
case 'time_immutable': case 'time_immutable':
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTimeImmutable')]; return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTimeImmutable')];
case 'dateinterval': case 'dateinterval':
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateInterval')]; return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateInterval')];
}
case DBALType::TARRAY: break;
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)]; case Type::BUILTIN_TYPE_ARRAY:
switch ($typeOfField) {
case DBALType::TARRAY:
case DBALType::JSON_ARRAY:
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)];
case DBALType::SIMPLE_ARRAY: case DBALType::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), new Type(Type::BUILTIN_TYPE_STRING))];
}
case DBALType::JSON_ARRAY:
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)];
default:
$builtinType = $this->getPhpType($typeOfField);
return $builtinType ? [new Type($builtinType, $nullable)] : null;
} }
return [new Type($builtinType, $nullable)];
} }
return null; return null;
@ -234,7 +243,22 @@ class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeE
return Type::BUILTIN_TYPE_RESOURCE; return Type::BUILTIN_TYPE_RESOURCE;
case DBALType::OBJECT: case DBALType::OBJECT:
case DBALType::DATE:
case DBALType::DATETIME:
case DBALType::DATETIMETZ:
case 'vardatetime':
case DBALType::TIME:
case 'date_immutable':
case 'datetime_immutable':
case 'datetimetz_immutable':
case 'time_immutable':
case 'dateinterval':
return Type::BUILTIN_TYPE_OBJECT; return Type::BUILTIN_TYPE_OBJECT;
case DBALType::TARRAY:
case DBALType::SIMPLE_ARRAY:
case DBALType::JSON_ARRAY:
return Type::BUILTIN_TYPE_ARRAY;
} }
return null; return null;

View File

@ -11,11 +11,13 @@
namespace Symfony\Bridge\Doctrine\Tests\PropertyInfo; namespace Symfony\Bridge\Doctrine\Tests\PropertyInfo;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Type as DBALType; use Doctrine\DBAL\Types\Type as DBALType;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Setup; use Doctrine\ORM\Tools\Setup;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor; use Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor;
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation;
use Symfony\Component\PropertyInfo\Type; use Symfony\Component\PropertyInfo\Type;
/** /**
@ -62,6 +64,8 @@ class DoctrineExtractorTest extends TestCase
'bar', 'bar',
'indexedBar', 'indexedBar',
'indexedFoo', 'indexedFoo',
'indexedByDt',
'indexedByCustomType',
], ],
$this->extractor->getProperties('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy') $this->extractor->getProperties('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy')
); );
@ -153,6 +157,15 @@ class DoctrineExtractorTest extends TestCase
['simpleArray', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))]], ['simpleArray', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))]],
['customFoo', null], ['customFoo', null],
['notMapped', null], ['notMapped', null],
['indexedByDt', [new Type(
Type::BUILTIN_TYPE_OBJECT,
false,
Collection::class,
true,
new Type(Type::BUILTIN_TYPE_OBJECT),
new Type(Type::BUILTIN_TYPE_OBJECT, false, DoctrineRelation::class)
)]],
['indexedByCustomType', null],
]; ];
} }

View File

@ -112,4 +112,14 @@ class DoctrineDummy
private $bigint; private $bigint;
public $notMapped; public $notMapped;
/**
* @OneToMany(targetEntity="DoctrineRelation", mappedBy="dt", indexBy="dt")
*/
protected $indexedByDt;
/**
* @OneToMany(targetEntity="DoctrineRelation", mappedBy="customType", indexBy="customType")
*/
private $indexedByCustomType;
} }

View File

@ -39,4 +39,14 @@ class DoctrineRelation
* @ManyToOne(targetEntity="DoctrineDummy", inversedBy="indexedFoo") * @ManyToOne(targetEntity="DoctrineDummy", inversedBy="indexedFoo")
*/ */
protected $foo; protected $foo;
/**
* @Column(type="datetime")
*/
private $dt;
/**
* @Column(type="foo")
*/
private $customType;
} }