Skip to content

Commit c153328

Browse files
added tests
1 parent 642d3b0 commit c153328

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,20 @@ 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+
$fieldName = $subMetadata->getFieldForColumn($associationMapping['indexBy']);
116+
$typeOfField = $subMetadata->getTypeOfField($fieldName);
117+
118+
// Maybe $fieldName is an association
119+
if (null === $typeOfField) {
120+
$associationMapping = $subMetadata->getAssociationMapping($fieldName);
121+
122+
/** @var ClassMetadataInfo $subMetadata */
123+
$indexProperty = $subMetadata->getSingleAssociationReferencedJoinColumnName($fieldName);
124+
$subMetadata = $this->classMetadataFactory->getMetadataFor($associationMapping['targetEntity']);
125+
$typeOfField = $subMetadata->getTypeOfField($indexProperty);
126+
}
117127

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

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public function testGetProperties()
7474
'bar',
7575
'indexedBar',
7676
'indexedFoo',
77+
'indexedBaz',
7778
'indexedByDt',
7879
'indexedByCustomType',
7980
]);
@@ -167,6 +168,14 @@ public function typesProvider()
167168
new Type(Type::BUILTIN_TYPE_STRING),
168169
new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
169170
)]],
171+
['indexedBaz', [new Type(
172+
Type::BUILTIN_TYPE_OBJECT,
173+
false,
174+
Collection::class,
175+
true,
176+
new Type(Type::BUILTIN_TYPE_INT),
177+
new Type(Type::BUILTIN_TYPE_OBJECT, false, DoctrineRelation::class)
178+
)]],
170179
['simpleArray', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))]],
171180
['customFoo', null],
172181
['notMapped', null],

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ class DoctrineDummy
5151
*/
5252
protected $indexedFoo;
5353

54+
/**
55+
* @OneToMany(targetEntity="DoctrineRelation", mappedBy="baz", indexBy="baz_id")
56+
*/
57+
protected $indexedBaz;
58+
5459
/**
5560
* @Column(type="guid")
5661
*/

0 commit comments

Comments
 (0)