8000 Merge branch '4.4' into 5.1 · symfony/symfony@05e944b · GitHub
[go: up one dir, main page]

Skip to content

Commit 05e944b

Browse files
committed
Merge branch '4.4' into 5.1
* 4.4: failing test for issue 38861 [DoctrineBridge] indexBy could reference to association columns
2 parents 382d6fc + cdfa9c2 commit 05e944b

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

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

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

9797
if (isset($associationMapping['indexBy'])) {
98-
$indexColumn = $associationMapping['indexBy'];
9998
/** @var ClassMetadataInfo $subMetadata */
10099
$subMetadata = $this->entityManager ? $this->entityManager->getClassMetadata($associationMapping['targetEntity']) : $this->classMetadataFactory->getMetadataFor($associationMapping['targetEntity']);
101-
$typeOfField = $subMetadata->getTypeOfField($subMetadata->getFieldForColumn($indexColumn));
100+
101+
// Check if indexBy value is a property
102+
$fieldName = $associationMapping['indexBy'];
103+
if (null === ($typeOfField = $subMetadata->getTypeOfField($fieldName))) {
104+
$fieldName = $subMetadata->getFieldForColumn($associationMapping['indexBy']);
105+
//Not a property, maybe a column name?
106+
if (null === ($typeOfField = $subMetadata->getTypeOfField($fieldName))) {
107+
//Maybe the column name is the association join column?
108+
$associationMapping = $subMetadata->getAssociationMapping($fieldName);
109+
110+
/** @var ClassMetadataInfo $subMetadata */
111+
$indexProperty = $subMetadata->getSingleAssociationReferencedJoinColumnName($fieldName);
112+
$subMetadata = $this->entityManager ? $this->entityManager->getClassMetadata($associationMapping['targetEntity']) : $this->classMetadataFactory->getMetadataFor($associationMapping['targetEntity']);
113+
$typeOfField = $subMetadata->getTypeOfField($indexProperty);
114+
}
115+
}
102116

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

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,13 @@ public function testGetProperties()
6868
$expected = array_merge($expected, [
6969
'foo',
7070
'bar',
71+
'indexedRguid',
7172
'indexedBar',
7273
'indexedFoo',
74+
'indexedBaz',
7375
'indexedByDt',
7476
'indexedByCustomType',
77+
'indexedBuz',
7578
]);
7679

7780
$this->assertEquals(
@@ -147,6 +150,14 @@ public function typesProvider()
147150
new Type(Type::BUILTIN_TYPE_INT),
148151
new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
149152
)]],
153+
['indexedRguid', [new Type(
154+
Type::BUILTIN_TYPE_OBJECT,
155+
false,
156+
'Doctrine\Common\Collections\Collection',
157+
true,
158+
new Type(Type::BUILTIN_TYPE_STRING),
159+
new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
160+
)]],
150161
['indexedBar', [new Type(
151162
Type::BUILTIN_TYPE_OBJECT,
152163
false,
@@ -163,6 +174,14 @@ public function typesProvider()
163174
new Type(Type::BUILTIN_TYPE_STRING),
164175
new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
165176
)]],
177+
['indexedBaz', [new Type(
178+
Type::BUILTIN_TYPE_OBJECT,
179+
false,
180+
Collection::class,
181+
true,
182+
new Type(Type::BUILTIN_TYPE_INT),
183+
new Type(Type::BUILTIN_TYPE_OBJECT, false, DoctrineRelation::class)
184+
)]],
166185
['simpleArray', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))]],
167186
['customFoo', null],
168187
['notMapped', null],
@@ -175,6 +194,14 @@ public function typesProvider()
175194
new Type(Type::BUILTIN_TYPE_OBJECT, false, DoctrineRelation::class)
176195
)]],
177196
['indexedByCustomType', null],
197+
['indexedBuz', [new Type(
198+
Type::BUILTIN_TYPE_OBJECT,
199+
false,
200+
Collection::class,
201+
true,
202+
new Type(Type::BUILTIN_TYPE_STRING),
203+
new Type(Type::BUILTIN_TYPE_OBJECT, false, DoctrineRelation::class)
204+
)]],
178205
];
179206

180207
if (class_exists(Types::class)) {

src/Symfony/Bridge/Doctrine/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
*/
@@ -122,4 +132,9 @@ class DoctrineDummy
122132
* @OneToMany(targetEntity="DoctrineRelation", mappedBy="customType", indexBy="customType")
123133
*/
124134
private $indexedByCustomType;
135+
136+
/**
137+
* @OneToMany(targetEntity="DoctrineRelation", mappedBy="buzField", indexBy="buzField")
138+
*/
139+
protected $indexedBuz;
125140
}

src/Symfony/Bridge/Doctrine/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)
0