8000 [DoctrineBridge] indexBy could reference to association columns · mpdude/symfony@4c36145 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4c36145

Browse files
juanmiguelbesadanicolas-grekas
authored andcommitted
[DoctrineBridge] indexBy could reference to association columns
1 parent 427e314 commit 4c36145

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php

Lines changed: 16 additions & 2 deletions
124
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,24 @@ public function getTypes($class, $property, array $context = [])
110110
$associationMapping = $metadata->getAssociationMapping($property);
111111

112112
if (isset($associationMapping['indexBy'])) {
113-
$indexColumn = $associationMapping['indexBy'];
114113
/** @var ClassMetadataInfo $subMetadata */
115114
$subMetadata = $this->classMetadataFactory->getMetadataFor($associationMapping['targetEntity']);
116-
$typeOfField = $subMetadata->getTypeOfField($subMetadata->getFieldForColumn($indexColumn));
115+
116+
// Check if indexBy value is a property
117+
$fieldName = $associationMapping['indexBy'];
118+
if (null === ($typeOfField = $subMetadata->getTypeOfField($fieldName))) {
119+
$fieldName = $subMetadata->getFieldForColumn($associationMapping['indexBy']);
120+
//Not a property, maybe a column name?
121+
if (null === ($typeOfField = $subMetadata->getTypeOfField($fieldName))) {
122+
//Maybe the column name is the association join column?
123+
$associationMapping = $subMetadata->getAssociationMapping($fieldName);
+
125+
/** @var ClassMetadataInfo $subMetadata */
126+
$indexProperty = $subMetadata->getSingleAssociationReferencedJoinColumnName($fieldName);
127+
$subMetadata = $this->classMetadataFactory->getMetadataFor($associationMapping['targetEntity']);
128+
$typeOfField = $subMetadata->getTypeOfField($indexProperty);
129+
}
130+
}
117131

118132
if (!$collectionKeyType = $this->getPhpType($typeOfField)) {
119133
return null;

src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ public function testGetProperties()
7272
$expected = array_merge($expected, [
7373
'foo',
7474
'bar',
75+
'indexedRguid',
7576
'indexedBar',
7677
'indexedFoo',
78+
'indexedBaz',
7779
'indexedByDt',
7880
'indexedByCustomType',
7981
]);
@@ -151,6 +153,14 @@ public function typesProvider()
151153
new Type(Type::BUILTIN_TYPE_INT),
152154
new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
153155
)]],
156+
['indexedRguid', [new Type(
157+
Type::BUILTIN_TYPE_OBJECT,
158+
false,
159+
'Doctrine\Common\Collections\Collection',
160+
true,
161+
new Type(Type::BUILTIN_TYPE_STRING),
162+
new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
163+
)]],
154164
['indexedBar', [new Type(
155165
Type::BUILTIN_TYPE_OBJECT,
156166
false,
@@ -167,6 +177,14 @@ public function typesProvider()
167177
new Type(Type::BUILTIN_TYPE_STRING),
168178
new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
169179
)]],
180+
['indexedBaz', [new Type(
181+
Type::BUILTIN_TYPE_OBJECT,
182+
false,
183+
Collection::class,
184+
true,
185+
new Type(Type::BUILTIN_TYPE_INT),
186+
new Type(Type::BUILTIN_TYPE_OBJECT, false, DoctrineRelation::class)
187+
)]],
170188
['simpleArray', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))]],
171189
['customFoo', null],
172190
['notMapped', null],

src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/DoctrineDummy.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ class DoctrineDummy
4141
*/
4242
public $bar;
4343

44+
/**
45+
* @ManyToMany(targetEntity="DoctrineRelation", indexBy="rguid")
46+
*/
47+
protected $indexedRguid;
48+
4449
/**
4550
* @ManyToMany(targetEntity="DoctrineRelation", indexBy="rguid_column")
4651
*/
@@ -51,6 +56,11 @@ class DoctrineDummy
5156
*/
5257
protected $indexedFoo;
5358

59+
/**
60+
* @OneToMany(targetEntity="DoctrineRelation", mappedBy="baz", indexBy="baz_id")
61+
*/
62+
protected $indexedBaz;
63+
5464
/**
5565
* @Column(type="guid")
5666
*/

src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/DoctrineRelation.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class DoctrineRelation
4040
*/
4141
protected $foo;
4242

43+
/**
44+
* @ManyToOne(targetEntity="DoctrineDummy")
45+
*/
46+
protected $baz;
47+
4348
/**
4449
* @Column(type="datetime")
4550
*/

0 commit comments

Comments
 (0)
0