[DoctrineBridge] indexBy could reference to association columns

This commit is contained in:
Juan Miguel Besada 2020-10-19 12:26:04 +02:00 committed by Nicolas Grekas
parent 427e314212
commit 4c36145664
4 changed files with 49 additions and 2 deletions

View File

@ -110,10 +110,24 @@ class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeE
$associationMapping = $metadata->getAssociationMapping($property);
if (isset($associationMapping['indexBy'])) {
$indexColumn = $associationMapping['indexBy'];
/** @var ClassMetadataInfo $subMetadata */
$subMetadata = $this->classMetadataFactory->getMetadataFor($associationMapping['targetEntity']);
$typeOfField = $subMetadata->getTypeOfField($subMetadata->getFieldForColumn($indexColumn));
// Check if indexBy value is a property
$fieldName = $associationMapping['indexBy'];
if (null === ($typeOfField = $subMetadata->getTypeOfField($fieldName))) {
$fieldName = $subMetadata->getFieldForColumn($associationMapping['indexBy']);
//Not a property, maybe a column name?
if (null === ($typeOfField = $subMetadata->getTypeOfField($fieldName))) {
//Maybe the column name is the association join column?
$associationMapping = $subMetadata->getAssociationMapping($fieldName);
/** @var ClassMetadataInfo $subMetadata */
$indexProperty = $subMetadata->getSingleAssociationReferencedJoinColumnName($fieldName);
$subMetadata = $this->classMetadataFactory->getMetadataFor($associationMapping['targetEntity']);
$typeOfField = $subMetadata->getTypeOfField($indexProperty);
}
}
if (!$collectionKeyType = $this->getPhpType($typeOfField)) {
return null;

View File

@ -72,8 +72,10 @@ class DoctrineExtractorTest extends TestCase
$expected = array_merge($expected, [
'foo',
'bar',
'indexedRguid',
'indexedBar',
'indexedFoo',
'indexedBaz',
'indexedByDt',
'indexedByCustomType',
]);
@ -151,6 +153,14 @@ class DoctrineExtractorTest extends TestCase
new Type(Type::BUILTIN_TYPE_INT),
new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
)]],
['indexedRguid', [new Type(
Type::BUILTIN_TYPE_OBJECT,
false,
'Doctrine\Common\Collections\Collection',
true,
new Type(Type::BUILTIN_TYPE_STRING),
new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
)]],
['indexedBar', [new Type(
Type::BUILTIN_TYPE_OBJECT,
false,
@ -167,6 +177,14 @@ class DoctrineExtractorTest extends TestCase
new Type(Type::BUILTIN_TYPE_STRING),
new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
)]],
['indexedBaz', [new Type(
Type::BUILTIN_TYPE_OBJECT,
false,
Collection::class,
true,
new Type(Type::BUILTIN_TYPE_INT),
new Type(Type::BUILTIN_TYPE_OBJECT, false, DoctrineRelation::class)
)]],
['simpleArray', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))]],
['customFoo', null],
['notMapped', null],

View File

@ -41,6 +41,11 @@ class DoctrineDummy
*/
public $bar;
/**
* @ManyToMany(targetEntity="DoctrineRelation", indexBy="rguid")
*/
protected $indexedRguid;
/**
* @ManyToMany(targetEntity="DoctrineRelation", indexBy="rguid_column")
*/
@ -51,6 +56,11 @@ class DoctrineDummy
*/
protected $indexedFoo;
/**
* @OneToMany(targetEntity="DoctrineRelation", mappedBy="baz", indexBy="baz_id")
*/
protected $indexedBaz;
/**
* @Column(type="guid")
*/

View File

@ -40,6 +40,11 @@ class DoctrineRelation
*/
protected $foo;
/**
* @ManyToOne(targetEntity="DoctrineDummy")
*/
protected $baz;
/**
* @Column(type="datetime")
*/