Skip to content

Commit 159154f

Browse files
authored
Improved joins when using the properties filter (#1552)
1 parent ec7e38b commit 159154f

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,24 @@ private function joinRelations(QueryBuilder $queryBuilder, QueryNameGeneratorInt
141141
continue;
142142
}
143143

144-
if ($inAttributes = isset($context[AbstractNormalizer::ATTRIBUTES][$association])) {
145-
// prepare the child context
146-
$context[AbstractNormalizer::ATTRIBUTES] = $context[AbstractNormalizer::ATTRIBUTES][$association];
144+
if (isset($context[AbstractNormalizer::ATTRIBUTES])) {
145+
if ($inAttributes = isset($context[AbstractNormalizer::ATTRIBUTES][$association])) {
146+
// prepare the child context
147+
$context[AbstractNormalizer::ATTRIBUTES] = $context[AbstractNormalizer::ATTRIBUTES][$association];
148+
} else {
149+
unset($context[AbstractNormalizer::ATTRIBUTES]);
150+
}
147151
} else {
148-
unset($context[AbstractNormalizer::ATTRIBUTES]);
152+
$inAttributes = null;
149153
}
150154

155+
$isNotReadableLink = false === $propertyMetadata->isReadableLink();
151156
if (
152-
((!$inAttributes && false === $propertyMetadata->isReadableLink()) || false === $propertyMetadata->isReadable()) &&
153-
false === $propertyMetadata->getAttribute('fetchEager', false)
157+
false === $propertyMetadata->getAttribute('fetchEager', false) &&
158+
(
159+
false === $propertyMetadata->isReadable() ||
160+
((null === $inAttributes && $isNotReadableLink) || (false === $inAttributes))
161+
)
154162
) {
155163
continue;
156164
}

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,47 @@ public function testAttributes()
726726
$eagerExtensionTest->applyToCollection($queryBuilder, new QueryNameGenerator(), Dummy::class);
727727
}
728728

729+
public function testNotInAttributes()
730+
{
731+
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
732+
$resourceMetadataFactoryProphecy->create(Dummy::class)->willReturn(new ResourceMetadata());
733+
734+
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
735+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
736+
$relationPropertyMetadata = new PropertyMetadata();
737+
$relationPropertyMetadata = $relationPropertyMetadata->withReadableLink(true);
738+
739+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', ['serializer_groups' => ['foo']])->willReturn($relationPropertyMetadata)->shouldBeCalled();
740+
741+
$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
742+
743+
$classMetadataProphecy = $this->prophesize(ClassMetadata::class);
744+
$classMetadataProphecy->associationMappings = [
745+
'relatedDummy' => ['fetch' => 3, 'joinColumns' => [['nullable' => true]], 'targetEntity' => RelatedDummy::class],
746+
];
747+
748+
$relatedClassMetadataProphecy = $this->prophesize(ClassMetadata::class);
749+
$relatedClassMetadataProphecy->associationMappings = [];
750+
751+
$emProphecy = $this->prophesize(EntityManager::class);
752+
$emProphecy->getClassMetadata(Dummy::class)->shouldBeCalled()->willReturn($classMetadataProphecy->reveal());
753+
754+
$queryBuilderProphecy->getRootAliases()->willReturn(['o']);
755+
$queryBuilderProphecy->getEntityManager()->willReturn($emProphecy);
756+
757+
$request = Request::create('/api/dummies', 'GET', []);
758+
759+
$requestStack = new RequestStack();
760+
$requestStack->push($request);
761+
762+
$serializerContextBuilderProphecy = $this->prophesize(SerializerContextBuilderInterface::class);
763+
$serializerContextBuilderProphecy->createFromRequest($request, true)->shouldBeCalled()->willReturn([AbstractNormalizer::GROUPS => ['foo'], AbstractNormalizer::ATTRIBUTES => ['relatedDummy']]);
764+
765+
$queryBuilder = $queryBuilderProphecy->reveal();
766+
$eagerExtensionTest = new EagerLoadingExtension($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $resourceMetadataFactoryProphecy->reveal(), 30, false, $requestStack, $serializerContextBuilderProphecy->reveal(), true);
767+
$eagerExtensionTest->applyToCollection($queryBuilder, new QueryNameGenerator(), Dummy::class);
768+
}
769+
729770
public function testApplyToCollectionNoPartial()
730771
{
731772
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);

0 commit comments

Comments
 (0)