8000 bug #38628 [DoctrineBridge] indexBy could reference to association c… · symfony/symfony@6724ca7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6724ca7

Browse files
bug #38628 [DoctrineBridge] indexBy could reference to association columns (juanmiguelbesada)
This PR was squashed before being merged into the 3.4 branch. Discussion ---------- [DoctrineBridge] indexBy could reference to association columns | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #37982 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | - <!-- Replace this notice by a short README for your feature/bugfix. This will help people understand your PR and can be used as a start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Never break backward compatibility (see https://symfony.com/bc). - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too.) - Features and deprecations must be submitted against branch 5.x. --> This is my approach to solve #37982. It partials reverts @xabbuh PR #38604 This is my first Symfony contribution, so please, tell me if I need to do something more or something is wrong. Also, this bug affects 4.x and 5.x versions. I think merging in this branches is done automatically. If not, please tell me. Thanks you Commits ------- f9a0e00 failing test for issue 38861 4c36145 [DoctrineBridge] indexBy could reference to association columns
2 parents 427e314 + f9a0e00 commit 6724ca7

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
@@ -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);
124+
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,13 @@ public function testGetProperties()
7272
$expected = array_merge($expected, [
7373
'foo',
7474
'bar',
75+
'indexedRguid',
7576
'indexedBar',
7677
'indexedFoo',
78+
'indexedBaz',
7779
'indexedByDt',
7880
'indexedByCustomType',
81+
'indexedBuz',
7982
]);
8083

8184
$this->assertEquals(
@@ -151,6 +154,14 @@ public function typesProvider()
151154
new Type(Type::BUILTIN_TYPE_INT),
152155
new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
153156
)]],
157+
['indexedRguid', [new Type(
158+
Type::BUILTIN_TYPE_OBJECT,
159+
false,
160+
'Doctrine\Common\Collections\Collection',
161+
true,
162+
new Type(Type::BUILTIN_TYPE_STRING),
163+
new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
164+
)]],
154165
['indexedBar', [new Type(
155166
Type::BUILTIN_TYPE_OBJECT,
156167
false,
@@ -167,6 +178,14 @@ public function typesProvider()
167178
new Type(Type::BUILTIN_TYPE_STRING),
168179
new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
169180
)]],
181+
['indexedBaz', [new Type(
182+
Type::BUILTIN_TYPE_OBJECT,
183+
false,
184+
Collection::class,
185+
true,
186+
new Type(Type::BUILTIN_TYPE_INT),
187+
new Type(Type::BUILTIN_TYPE_OBJECT, false, DoctrineRelation::class)
188+
)]],
170189
['simpleArray', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))]],
171190
['customFoo', null],
172191
['notMapped', null],
@@ -179,6 +198,14 @@ public function typesProvider()
179198
new Type(Type::BUILTIN_TYPE_OBJECT, false, DoctrineRelation::class)
180199
)]],
181200
['indexedByCustomType', null],
201+
['indexedBuz', [new Type(
202+
Type::BUILTIN_TYPE_OBJECT,
203+
false,
204+
Collection::class,
205+
true,
206+
new Type(Type::BUILTIN_TYPE_STRING),
207+
new Type(Type::BUILTIN_TYPE_OBJECT, false, DoctrineRelation::class)
208+
)]],
182209
];
183210

184211
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