Skip to content

Commit 1b650ee

Browse files
authored
Merge pull request #1897 from bendavies/respect-property-force-eager
EagerLoadingExtension: respect fetchEager=false on an association
2 parents 0b35725 + 0b55fdc commit 1b650ee

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,12 @@ private function joinRelations(QueryBuilder $queryBuilder, QueryNameGeneratorInt
170170
$inAttributes = null;
171171
}
172172

173+
if (false === $fetchEager = $propertyMetadata->getAttribute('fetchEager')) {
174+
continue;
175+
}
176+
173177
$isNotReadableLink = false === $propertyMetadata->isReadableLink();
174-
if (
175-
false === $propertyMetadata->getAttribute('fetchEager', false) &&
176-
(
177-
false === $propertyMetadata->isReadable() ||
178-
((null === $inAttributes && $isNotReadableLink) || (false === $inAttributes))
179-
)
180-
) {
178+
if (null === $fetchEager && (false === $propertyMetadata->isReadable() || ((null === $inAttributes && $isNotReadableLink) || (false === $inAttributes)))) {
181179
continue;
182180
}
183181

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,4 +850,45 @@ public function testApplyToCollectionWithANonRedableButFetchEagerProperty()
850850
$eagerExtensionTest = new EagerLoadingExtension($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $resourceMetadataFactoryProphecy->reveal(), 30);
851851
$eagerExtensionTest->applyToCollection($queryBuilder, new QueryNameGenerator(), Dummy::class);
852852
}
853+
854+
public function testApplyToCollectionWithARedableButNotFetchEagerProperty()
855+
{
856+
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
857+
$resourceMetadataFactoryProphecy->create(Dummy::class)->willReturn(new ResourceMetadata());
858+
859+
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
860+
861+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
862+
$relationPropertyMetadata = new PropertyMetadata();
863+
$relationPropertyMetadata = $relationPropertyMetadata->withAttributes(['fetchEager' => false]);
864+
$relationPropertyMetadata = $relationPropertyMetadata->withReadableLink(true);
865+
$relationPropertyMetadata = $relationPropertyMetadata->withReadable(true);
866+
867+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn($relationPropertyMetadata)->shouldBeCalled();
868+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy2', [])->willReturn($relationPropertyMetadata)->shouldBeCalled();
869+
870+
$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
871+
872+
$classMetadataProphecy = $this->prophesize(ClassMetadata::class);
873+
$classMetadataProphecy->associationMappings = [
874+
'relatedDummy' => ['fetch' => ClassMetadataInfo::FETCH_EAGER, 'joinColumns' => [['nullable' => true]], 'targetEntity' => RelatedDummy::class],
875+
'relatedDummy2' => ['fetch' => ClassMetadataInfo::FETCH_EAGER, 'joinColumns' => [['nullable' => false]], 'targetEntity' => RelatedDummy::class],
876+
];
877+
878+
$emProphecy = $this->prophesize(EntityManager::class);
879+
$emProphecy->getClassMetadata(Dummy::class)->shouldBeCalled()->willReturn($classMetadataProphecy->reveal());
880+
$emProphecy->getClassMetadata(RelatedDummy::class)->shouldNotBecalled();
881+
882+
$queryBuilderProphecy->getRootAliases()->willReturn(['o']);
883+
$queryBuilderProphecy->getEntityManager()->willReturn($emProphecy);
884+
885+
$queryBuilderProphecy->leftJoin('o.relatedDummy', 'relatedDummy_a1')->shouldNotBeCalled();
886+
$queryBuilderProphecy->innerJoin('o.relatedDummy2', 'relatedDummy2_a2')->shouldNotBeCalled();
887+
$queryBuilderProphecy->addSelect('relatedDummy_a1')->shouldNotBeCalled();
888+
$queryBuilderProphecy->addSelect('relatedDummy2_a2')->shouldNotBeCalled();
889+
890+
$queryBuilder = $queryBuilderProphecy->reveal();
891+
$eagerExtensionTest = new EagerLoadingExtension($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $resourceMetadataFactoryProphecy->reveal(), 30);
892+
$eagerExtensionTest->applyToCollection($queryBuilder, new QueryNameGenerator(), Dummy::class);
893+
}
853894
}

0 commit comments

Comments
 (0)