From 9696c3434d8cc8d4f0f54b170cba65772bb5eb6d Mon Sep 17 00:00:00 2001 From: Alix Mauro Date: Fri, 19 Jan 2024 14:16:24 +0100 Subject: [PATCH] Consider usage of setFetchMode when checking for simultaneous usage of fetch-mode EAGER and WITH condition. This fixes a bug that arises when an entity relation is mapped with fetch-mode EAGER but setFetchMode LAZY (or anything that is not EAGER) has been used on the query. If the query use WITH condition, an exception is incorrectly raised (Associations with fetch-mode=EAGER may not be using WITH conditions). Fixes #11128 Co-Authored-By: Albert Prat --- src/Query/SqlWalker.php | 4 +++- tests/Tests/ORM/Functional/EagerFetchCollectionTest.php | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Query/SqlWalker.php b/src/Query/SqlWalker.php index 4948be46536..4c25fb63a68 100644 --- a/src/Query/SqlWalker.php +++ b/src/Query/SqlWalker.php @@ -1062,7 +1062,9 @@ public function walkJoinAssociationDeclaration($joinAssociationDeclaration, $joi } } - if ($relation['fetch'] === ClassMetadata::FETCH_EAGER && $condExpr !== null) { + $fetchMode = $this->query->getHint('fetchMode')[$assoc['sourceEntity']][$assoc['fieldName']] ?? $relation['fetch']; + + if ($fetchMode === ClassMetadata::FETCH_EAGER && $condExpr !== null) { throw QueryException::eagerFetchJoinWithNotAllowed($assoc['sourceEntity'], $assoc['fieldName']); } diff --git a/tests/Tests/ORM/Functional/EagerFetchCollectionTest.php b/tests/Tests/ORM/Functional/EagerFetchCollectionTest.php index ff0eab56d63..88397c6a12f 100644 --- a/tests/Tests/ORM/Functional/EagerFetchCollectionTest.php +++ b/tests/Tests/ORM/Functional/EagerFetchCollectionTest.php @@ -88,6 +88,14 @@ public function testSubselectFetchJoinWithNotAllowed(): void $query->getResult(); } + public function testSubselectFetchJoinWithAllowedWhenOverriddenNotEager(): void + { + $query = $this->_em->createQuery('SELECT o, c FROM ' . EagerFetchOwner::class . ' o JOIN o.children c WITH c.id = 1'); + $query->setFetchMode(EagerFetchChild::class, 'owner', ORM\ClassMetadata::FETCH_LAZY); + + $this->assertIsString($query->getSql()); + } + public function testEagerFetchWithIterable(): void { $this->createOwnerWithChildren(2);