Skip to content

[DoctrineBridge] indexBy could reference to association columns #38628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,24 @@ public function getTypes($class, $property, array $context = [])
$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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ public function testGetProperties()
$expected = array_merge($expected, [
'foo',
'bar',
'indexedRguid',
'indexedBar',
'indexedFoo',
'indexedBaz',
'indexedByDt',
'indexedByCustomType',
'indexedBuz',
]);

$this->assertEquals(
Expand Down Expand Up @@ -151,6 +154,14 @@ public function typesProvider()
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,
Expand All @@ -167,6 +178,14 @@ public function typesProvider()
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],
Expand All @@ -179,6 +198,14 @@ public function typesProvider()
new Type(Type::BUILTIN_TYPE_OBJECT, false, DoctrineRelation::class)
)]],
['indexedByCustomType', null],
['indexedBuz', [new Type(
Type::BUILTIN_TYPE_OBJECT,
false,
Collection::class,
true,
new Type(Type::BUILTIN_TYPE_STRING),
new Type(Type::BUILTIN_TYPE_OBJECT, false, DoctrineRelation::class)
)]],
];

if (class_exists(Types::class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class DoctrineDummy
*/
public $bar;

/**
* @ManyToMany(targetEntity="DoctrineRelation", indexBy="rguid")
*/
protected $indexedRguid;

/**
* @ManyToMany(targetEntity="DoctrineRelation", indexBy="rguid_column")
*/
Expand All @@ -51,6 +56,11 @@ class DoctrineDummy
*/
protected $indexedFoo;

/**
* @OneToMany(targetEntity="DoctrineRelation", mappedBy="baz", indexBy="baz_id")
*/
protected $indexedBaz;

/**
* @Column(type="guid")
*/
Expand Down Expand Up @@ -122,4 +132,9 @@ class DoctrineDummy
* @OneToMany(targetEntity="DoctrineRelation", mappedBy="customType", indexBy="customType")
*/
private $indexedByCustomType;

/**
* @OneToMany(targetEntity="DoctrineRelation", mappedBy="buzField", indexBy="buzField")
*/
protected $indexedBuz;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class DoctrineRelation
*/
protected $foo;

/**
* @ManyToOne(targetEntity="DoctrineDummy")
*/
protected $baz;

/**
* @Column(type="datetime")
*/
Expand All @@ -49,4 +54,10 @@ class DoctrineRelation
* @Column(type="foo")
*/
private $customType;

/**
* @Column(type="guid", name="different_than_field")
* @ManyToOne(targetEntity="DoctrineDummy", inversedBy="indexedBuz")
*/
protected $buzField;
}