From 00a4751542183d07031bf107fc423079e3cd0ac0 Mon Sep 17 00:00:00 2001 From: Bizley Date: Fri, 21 Mar 2025 11:14:17 +0100 Subject: [PATCH 1/3] Update EagerLoadingExtension.php Added optimization --- src/Doctrine/Orm/Extension/EagerLoadingExtension.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Doctrine/Orm/Extension/EagerLoadingExtension.php b/src/Doctrine/Orm/Extension/EagerLoadingExtension.php index 240c1ff15d..4f5398aa15 100644 --- a/src/Doctrine/Orm/Extension/EagerLoadingExtension.php +++ b/src/Doctrine/Orm/Extension/EagerLoadingExtension.php @@ -123,6 +123,12 @@ private function joinRelations(QueryBuilder $queryBuilder, QueryNameGeneratorInt continue; } + if (isset($attributesMetadata[$association]) + && empty(array_intersect($normalizationContext[AbstractNormalizer::GROUPS] ?? [], $attributesMetadata[$association]->getGroups()))) { + // Skip this association if the current normalization groups do not include the association's groups + continue; + } + try { $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $association, $options); } catch (PropertyNotFoundException) { From ef3ef8787b37cd2d0adb0e54a0c3d80be15876ab Mon Sep 17 00:00:00 2001 From: Bizley Date: Sat, 22 Mar 2025 13:46:50 +0100 Subject: [PATCH 2/3] Move preparing child context up, and check for attributes to skip association --- .../Orm/Extension/EagerLoadingExtension.php | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Doctrine/Orm/Extension/EagerLoadingExtension.php b/src/Doctrine/Orm/Extension/EagerLoadingExtension.php index 4f5398aa15..7703c09075 100644 --- a/src/Doctrine/Orm/Extension/EagerLoadingExtension.php +++ b/src/Doctrine/Orm/Extension/EagerLoadingExtension.php @@ -113,8 +113,7 @@ private function joinRelations(QueryBuilder $queryBuilder, QueryNameGeneratorInt } $currentDepth = $currentDepth > 0 ? $currentDepth - 1 : $currentDepth; - $entityManager = $queryBuilder->getEntityManager(); - $classMetadata = $entityManager->getClassMetadata($resourceClass); + $classMetadata = $queryBuilder->getEntityManager()->getClassMetadata($resourceClass); $attributesMetadata = $this->classMetadataFactory?->getMetadataFor($resourceClass)->getAttributesMetadata(); foreach ($classMetadata->associationMappings as $association => $mapping) { @@ -123,7 +122,18 @@ private function joinRelations(QueryBuilder $queryBuilder, QueryNameGeneratorInt continue; } - if (isset($attributesMetadata[$association]) + // prepare the child context + $childNormalizationContext = $normalizationContext; + if (isset($normalizationContext[AbstractNormalizer::ATTRIBUTES])) { + if ($inAttributes = isset($normalizationContext[AbstractNormalizer::ATTRIBUTES][$association])) { + $childNormalizationContext[AbstractNormalizer::ATTRIBUTES] = $normalizationContext[AbstractNormalizer::ATTRIBUTES][$association]; + } + } else { + $inAttributes = null; + } + + if (true !== $inAttributes + && isset($attributesMetadata[$association]) && empty(array_intersect($normalizationContext[AbstractNormalizer::GROUPS] ?? [], $attributesMetadata[$association]->getGroups()))) { // Skip this association if the current normalization groups do not include the association's groups continue; @@ -149,16 +159,6 @@ private function joinRelations(QueryBuilder $queryBuilder, QueryNameGeneratorInt continue; } - // prepare the child context - $childNormalizationContext = $normalizationContext; - if (isset($normalizationContext[AbstractNormalizer::ATTRIBUTES])) { - if ($inAttributes = isset($normalizationContext[AbstractNormalizer::ATTRIBUTES][$association])) { - $childNormalizationContext[AbstractNormalizer::ATTRIBUTES] = $normalizationContext[AbstractNormalizer::ATTRIBUTES][$association]; - } - } else { - $inAttributes = null; - } - $fetchEager = $propertyMetadata->getFetchEager(); $uriTemplate = $propertyMetadata->getUriTemplate(); From 466d5de85592807e3ef6573c7923a70c2aa08f3f Mon Sep 17 00:00:00 2001 From: Bizley Date: Sat, 22 Mar 2025 14:08:51 +0100 Subject: [PATCH 3/3] Fix test --- src/Doctrine/Orm/Tests/Extension/EagerLoadingExtensionTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Doctrine/Orm/Tests/Extension/EagerLoadingExtensionTest.php b/src/Doctrine/Orm/Tests/Extension/EagerLoadingExtensionTest.php index 2ad7a74d33..74372c3ca1 100644 --- a/src/Doctrine/Orm/Tests/Extension/EagerLoadingExtensionTest.php +++ b/src/Doctrine/Orm/Tests/Extension/EagerLoadingExtensionTest.php @@ -427,9 +427,11 @@ public function testMaxDepth(): void $dummyAttributeMetadata = new AttributeMetadata('dummy'); $dummyAttributeMetadata->setMaxDepth(2); + $dummyAttributeMetadata->addGroup('foo'); $relatedAttributeMetadata = new AttributeMetadata('relatedDummy'); $relatedAttributeMetadata->setMaxDepth(4); + $relatedAttributeMetadata->addGroup('foo'); $dummyClassMetadataInterfaceProphecy->getAttributesMetadata()->willReturn(['relatedDummy' => $dummyAttributeMetadata]); $relatedClassMetadataInterfaceProphecy->getAttributesMetadata()->willReturn(['dummy' => $relatedAttributeMetadata]);