Skip to content

Commit 6e0c40d

Browse files
authored
Merge pull request #807 from soyuka/improve-eagerloadingextension
Improve EagerLoadingExtension by using the associationMappings array
2 parents 4e3170d + 676a929 commit 6e0c40d

File tree

2 files changed

+6
-11
lines changed

2 files changed

+6
-11
lines changed

src/Bridge/Doctrine/Orm/Extension/EagerLoadingExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ private function joinRelations(QueryBuilder $queryBuilder, string $resourceClass
7474
$entityManager = $queryBuilder->getEntityManager();
7575
$classMetadata = $entityManager->getClassMetadata($resourceClass);
7676
$j = 0;
77+
$i = 0;
7778

78-
foreach ($classMetadata->getAssociationNames() as $i => $association) {
79-
$mapping = $classMetadata->associationMappings[$association];
79+
foreach ($classMetadata->associationMappings as $association => $mapping) {
8080
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $association, $propertyMetadataOptions);
8181

8282
if (ClassMetadataInfo::FETCH_EAGER !== $mapping['fetch'] || false === $propertyMetadata->isReadableLink()) {
@@ -95,7 +95,7 @@ private function joinRelations(QueryBuilder $queryBuilder, string $resourceClass
9595
$method = 'leftJoin';
9696
}
9797

98-
$associationAlias = $relationAlias.$i;
98+
$associationAlias = $relationAlias.$i++;
9999
$queryBuilder->{$method}($originAlias.'.'.$association, $associationAlias);
100100
$select = [];
101101
$targetClassMetadata = $entityManager->getClassMetadata($mapping['targetEntity']);

tests/Bridge/Doctrine/Orm/Extension/EagerLoadingExtensionTest.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ public function testApplyToCollection()
6161
$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
6262

6363
$classMetadataProphecy = $this->prophesize(ClassMetadata::class);
64-
$classMetadataProphecy->getAssociationNames()->shouldBeCalled()->willReturn([0 => 'relatedDummy', 'relatedDummy2']);
6564
$classMetadataProphecy->associationMappings = [
6665
'relatedDummy' => ['fetch' => 3, 'joinColumns' => [['nullable' => true]], 'targetEntity' => RelatedDummy::class],
6766
'relatedDummy2' => ['fetch' => 3, 'joinColumns' => [['nullable' => false]], 'targetEntity' => RelatedDummy::class],
@@ -75,7 +74,7 @@ public function testApplyToCollection()
7574
}
7675
}
7776

78-
$relatedClassMetadataProphecy->getAssociationNames()->shouldBeCalled()->willReturn([]);
77+
$relatedClassMetadataProphecy->associationMappings = [];
7978

8079
$emProphecy = $this->prophesize(EntityManager::class);
8180
$emProphecy->getClassMetadata(Dummy::class)->shouldBeCalled()->willReturn($classMetadataProphecy->reveal());
@@ -110,6 +109,7 @@ public function testApplyToItem()
110109
$propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy2', [])->willReturn($relationPropertyMetadata)->shouldBeCalled();
111110
$propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy3', [])->willReturn($relationPropertyMetadata)->shouldBeCalled();
112111
$propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy4', [])->willReturn($relationPropertyMetadata)->shouldBeCalled();
112+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy5', [])->willReturn($relationPropertyMetadata)->shouldBeCalled();
113113

114114
$idPropertyMetadata = new PropertyMetadata();
115115
$idPropertyMetadata = $idPropertyMetadata->withIdentifier(true);
@@ -130,7 +130,6 @@ public function testApplyToItem()
130130
$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
131131

132132
$classMetadataProphecy = $this->prophesize(ClassMetadata::class);
133-
$classMetadataProphecy->getAssociationNames()->shouldBeCalled()->willReturn(['relatedDummy', 'relatedDummy2', 'relatedDummy3', 'relatedDummy4']);
134133
$classMetadataProphecy->associationMappings = [
135134
'relatedDummy' => ['fetch' => 3, 'joinColumns' => [['nullable' => true]], 'targetEntity' => RelatedDummy::class],
136135
'relatedDummy2' => ['fetch' => 3, 'joinColumns' => [['nullable' => false]], 'targetEntity' => UnknownDummy::class],
@@ -147,14 +146,12 @@ public function testApplyToItem()
147146
}
148147
}
149148

150-
$relatedClassMetadataProphecy->getAssociationNames()->shouldBeCalled()->willReturn(['relation']);
151-
152149
$relatedClassMetadataProphecy->associationMappings = [
153150
'relation' => ['fetch' => 3, 'joinColumns' => [['nullable' => false]], 'targetEntity' => UnknownDummy::class],
154151
];
155152

156153
$unknownClassMetadataProphecy = $this->prophesize(ClassMetadata::class);
157-
$unknownClassMetadataProphecy->getAssociationNames()->shouldBeCalled()->willReturn([]);
154+
$unknownClassMetadataProphecy->associationMappings = [];
158155

159156
$emProphecy = $this->prophesize(EntityManager::class);
160157
$emProphecy->getClassMetadata(Dummy::class)->shouldBeCalled()->willReturn($classMetadataProphecy->reveal());
@@ -188,7 +185,6 @@ public function testCreateItemWithOperationName()
188185
$propertyMetadataFactoryProphecy->create(Dummy::class, 'foo', ['item_operation_name' => 'item_operation'])->shouldBeCalled()->willReturn(new PropertyMetadata());
189186

190187
$classMetadataProphecy = $this->prophesize(ClassMetadata::class);
191-
$classMetadataProphecy->getAssociationNames()->shouldBeCalled()->willReturn(['foo']);
192188
$classMetadataProphecy->associationMappings = [
193189
'foo' => ['fetch' => 1],
194190
];
@@ -209,7 +205,6 @@ public function testCreateCollectionWithOperationName()
209205
$propertyMetadataFactoryProphecy->create(Dummy::class, 'foo', ['collection_operation_name' => 'collection_operation'])->shouldBeCalled()->willReturn(new PropertyMetadata());
210206

211207
$classMetadataProphecy = $this->prophesize(ClassMetadata::class);
212-
$classMetadataProphecy->getAssociationNames()->shouldBeCalled()->willReturn(['foo']);
213208
$classMetadataProphecy->associationMappings = [
214209
'foo' => ['fetch' => 1],
215210
];

0 commit comments

Comments
 (0)