From 54a64755929123c156b70da2291738569544fefb Mon Sep 17 00:00:00 2001 From: Sajito Date: Sun, 18 Sep 2022 12:33:51 +0200 Subject: [PATCH] fix: check fetch joined queries based on all aliases fixes #4936 --- .../Bridge/Doctrine/Orm/Util/QueryChecker.php | 20 +++---------------- src/Doctrine/Orm/Util/QueryChecker.php | 20 +++---------------- tests/Doctrine/Orm/Util/QueryCheckerTest.php | 4 ++-- 3 files changed, 8 insertions(+), 36 deletions(-) diff --git a/src/Core/Bridge/Doctrine/Orm/Util/QueryChecker.php b/src/Core/Bridge/Doctrine/Orm/Util/QueryChecker.php index 568a0ca3523..5d9956e87a5 100644 --- a/src/Core/Bridge/Doctrine/Orm/Util/QueryChecker.php +++ b/src/Core/Bridge/Doctrine/Orm/Util/QueryChecker.php @@ -93,7 +93,6 @@ public static function hasMaxResults(QueryBuilder $queryBuilder): bool public static function hasOrderByOnFetchJoinedToManyAssociation(QueryBuilder $queryBuilder, ManagerRegistry $managerRegistry): bool { if ( - 0 === \count($selectParts = $queryBuilder->getDQLPart('select')) || 0 === \count($queryBuilder->getDQLPart('join')) || 0 === \count($orderByParts = $queryBuilder->getDQLPart('orderBy')) ) { @@ -102,21 +101,6 @@ public static function hasOrderByOnFetchJoinedToManyAssociation(QueryBuilder $qu $rootAliases = $queryBuilder->getRootAliases(); - $selectAliases = []; - - foreach ($selectParts as $select) { - foreach ($select->getParts() as $part) { - [$alias] = explode('.', $part); - - $selectAliases[] = $alias; - } - } - - $selectAliases = array_diff($selectAliases, $rootAliases); - if (0 === \count($selectAliases)) { - return false; - } - $orderByAliases = []; foreach ($orderByParts as $orderBy) { @@ -134,11 +118,13 @@ public static function hasOrderByOnFetchJoinedToManyAssociation(QueryBuilder $qu return false; } + $allAliases = $queryBuilder->getAllAliases(); + foreach ($orderByAliases as $orderByAlias) { $inToManyContext = false; foreach (QueryBuilderHelper::traverseJoins($orderByAlias, $queryBuilder, $managerRegistry) as $alias => [$metadata, $association]) { - if ($inToManyContext && \in_array($alias, $selectAliases, true)) { + if ($inToManyContext && \in_array($alias, $allAliases, true)) { return true; } diff --git a/src/Doctrine/Orm/Util/QueryChecker.php b/src/Doctrine/Orm/Util/QueryChecker.php index 92fa096b878..74f30f4fd3d 100644 --- a/src/Doctrine/Orm/Util/QueryChecker.php +++ b/src/Doctrine/Orm/Util/QueryChecker.php @@ -92,7 +92,6 @@ public static function hasMaxResults(QueryBuilder $queryBuilder): bool public static function hasOrderByOnFetchJoinedToManyAssociation(QueryBuilder $queryBuilder, ManagerRegistry $managerRegistry): bool { if ( - 0 === \count($selectParts = $queryBuilder->getDQLPart('select')) || 0 === \count($queryBuilder->getDQLPart('join')) || 0 === \count($orderByParts = $queryBuilder->getDQLPart('orderBy')) ) { @@ -101,21 +100,6 @@ public static function hasOrderByOnFetchJoinedToManyAssociation(QueryBuilder $qu $rootAliases = $queryBuilder->getRootAliases(); - $selectAliases = []; - - foreach ($selectParts as $select) { - foreach ($select->getParts() as $part) { - [$alias] = explode('.', $part); - - $selectAliases[] = $alias; - } - } - - $selectAliases = array_diff($selectAliases, $rootAliases); - if (0 === \count($selectAliases)) { - return false; - } - $orderByAliases = []; foreach ($orderByParts as $orderBy) { @@ -133,11 +117,13 @@ public static function hasOrderByOnFetchJoinedToManyAssociation(QueryBuilder $qu return false; } + $allAliases = $queryBuilder->getAllAliases(); + foreach ($orderByAliases as $orderByAlias) { $inToManyContext = false; foreach (QueryBuilderHelper::traverseJoins($orderByAlias, $queryBuilder, $managerRegistry) as $alias => [$metadata, $association]) { - if ($inToManyContext && \in_array($alias, $selectAliases, true)) { + if ($inToManyContext && \in_array($alias, $allAliases, true)) { return true; } diff --git a/tests/Doctrine/Orm/Util/QueryCheckerTest.php b/tests/Doctrine/Orm/Util/QueryCheckerTest.php index d223831272d..ca411727ca0 100644 --- a/tests/Doctrine/Orm/Util/QueryCheckerTest.php +++ b/tests/Doctrine/Orm/Util/QueryCheckerTest.php @@ -175,7 +175,7 @@ public function testHasOrderByOnFetchJoinedToManyAssociationNotFetchJoined() $managerRegistryProphecy->getManagerForClass(Dummy::class)->willReturn($entityManagerProphecy); $managerRegistryProphecy->getManagerForClass(RelatedDummy::class)->willReturn($entityManagerProphecy); - $this->assertFalse(QueryChecker::hasOrderByOnFetchJoinedToManyAssociation($queryBuilder, $managerRegistryProphecy->reveal())); + $this->assertTrue(QueryChecker::hasOrderByOnFetchJoinedToManyAssociation($queryBuilder, $managerRegistryProphecy->reveal())); } public function testHasOrderByOnFetchJoinedToManyAssociationWithJoinByAssociation() @@ -277,7 +277,7 @@ public function testHasOrderByOnToManyJoinNotFetchJoined() $managerRegistryProphecy->getManagerForClass(Dummy::class)->willReturn($entityManagerProphecy); $managerRegistryProphecy->getManagerForClass(RelatedDummy::class)->willReturn($entityManagerProphecy); - $this->assertFalse(LegacyQueryChecker::hasOrderByOnToManyJoin($queryBuilder, $managerRegistryProphecy->reveal())); + $this->assertTrue(LegacyQueryChecker::hasOrderByOnToManyJoin($queryBuilder, $managerRegistryProphecy->reveal())); } /**