Skip to content

Commit 826cec8

Browse files
committed
Merge branch '5.1' into 5.x
* 5.1: failing test for issue 38861 [DoctrineBridge] indexBy could reference to association columns
2 parents 6998e4f + f8824e9 commit 826cec8

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

PropertyInfo/DoctrineExtractor.php

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

9191
if (isset($associationMapping['indexBy'])) {
92-
$indexColumn = $associationMapping['indexBy'];
9392
/** @var ClassMetadataInfo $subMetadata */
9493
$subMetadata = $this->entityManager ? $this->entityManager->getClassMetadata($associationMapping['targetEntity']) : $this->classMetadataFactory->getMetadataFor($associationMapping['targetEntity']);
95-
$typeOfField = $subMetadata->getTypeOfField($subMetadata->getFieldForColumn($indexColumn));
94+
95+
// Check if indexBy value is a property
96+
$fieldName = $associationMapping['indexBy'];
97+
if (null === ($typeOfField = $subMetadata->getTypeOfField($fieldName))) {
98+
$fieldName = $subMetadata->getFieldForColumn($associationMapping['indexBy']);
99+
//Not a property, maybe a column name?
100+
if (null === ($typeOfField = $subMetadata->getTypeOfField($fieldName))) {
101+
//Maybe the column name is the association join column?
102+
$associationMapping = $subMetadata->getAssociationMapping($fieldName);
103+
104+
/** @var ClassMetadataInfo $subMetadata */
105+
$indexProperty = $subMetadata->getSingleAssociationReferencedJoinColumnName($fieldName);
106+
$subMetadata = $this->entityManager ? $this->entityManager->getClassMetadata($associationMapping['targetEntity']) : $this->classMetadataFactory->getMetadataFor($associationMapping['targetEntity']);
107+
$typeOfField = $subMetadata->getTypeOfField($indexProperty);
108+
}
109+
}
96110

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

Tests/PropertyInfo/DoctrineExtractorTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,13 @@ public function testGetProperties()
6464
$expected = array_merge($expected, [
6565
'foo',
6666
'bar',
67+
'indexedRguid',
6768
'indexedBar',
6869
'indexedFoo',
70+
'indexedBaz',
6971
'indexedByDt',
7072
'indexedByCustomType',
73+
'indexedBuz',
7174
]);
7275

7376
$this->assertEquals(
@@ -143,6 +146,14 @@ public function typesProvider()
143146
new Type(Type::BUILTIN_TYPE_INT),
144147
new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
145148
)]],
149+
['indexedRguid', [new Type(
150+
Type::BUILTIN_TYPE_OBJECT,
151+
false,
152+
'Doctrine\Common\Collections\Collection',
153+
true,
154+
new Type(Type::BUILTIN_TYPE_STRING),
155+
new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
156+
)]],
146157
['indexedBar', [new Type(
147158
Type::BUILTIN_TYPE_OBJECT,
148159
false,
@@ -159,6 +170,14 @@ public function typesProvider()
159170
new Type(Type::BUILTIN_TYPE_STRING),
160171
new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
161172
)]],
173+
['indexedBaz', [new Type(
174+
Type::BUILTIN_TYPE_OBJECT,
175+
false,
176+
Collection::class,
177+
true,
178+
new Type(Type::BUILTIN_TYPE_INT),
179+
new Type(Type::BUILTIN_TYPE_OBJECT, false, DoctrineRelation::class)
180+
)]],
162181
['simpleArray', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))]],
163182
['customFoo', null],
164183
['notMapped', null],
@@ -171,6 +190,14 @@ public function typesProvider()
171190
new Type(Type::BUILTIN_TYPE_OBJECT, false, DoctrineRelation::class)
172191
)]],
173192
['indexedByCustomType', null],
193+
['indexedBuz', [new Type(
194+
Type::BUILTIN_TYPE_OBJECT,
195+
false,
196+
Collection::class,
197+
true,
198+
new Type(Type::BUILTIN_TYPE_STRING),
199+
new Type(Type::BUILTIN_TYPE_OBJECT, false, DoctrineRelation::class)
200+
)]],
174201
['json', null],
175202
];
176203

Tests/PropertyInfo/Fixtures/DoctrineDummy.php

Lines changed: 15 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
*/
@@ -123,6 +133,11 @@ class DoctrineDummy
123133
*/
124134
private $indexedByCustomType;
125135

136+
/**
137+
* @OneToMany(targetEntity="DoctrineRelation", mappedBy="buzField", indexBy="buzField")
138+
*/
139+
protected $indexedBuz;
140+
126141
/**
127142
* @Column(type="json", nullable=true)
128143
*/

Tests/PropertyInfo/Fixtures/DoctrineRelation.php

Lines changed: 11 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
*/
@@ -49,4 +54,10 @@ class DoctrineRelation
4954
* @Column(type="foo")
5055
*/
5156
private $customType;
57+
58+
/**
59+
* @Column(type="guid", name="different_than_field")
60+
* @ManyToOne(targetEntity="DoctrineDummy", inversedBy="indexedBuz")
61+
*/
62+
protected $buzField;
5263
}

0 commit comments

Comments
 (0)