Skip to content

Commit 5506e78

Browse files
lo-alixmaurogreg0ire
authored andcommitted
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
1 parent d31aabb commit 5506e78

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/Query/SqlWalker.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,9 @@ public function walkJoinAssociationDeclaration($joinAssociationDeclaration, $joi
10621062
}
10631063
}
10641064

1065-
if ($relation['fetch'] === ClassMetadata::FETCH_EAGER && $condExpr !== null) {
1065+
$fetchMode = $this->query->getHint('fetchMode')[$assoc['sourceEntity']][$assoc['fieldName']] ?? $relation['fetch'];
1066+
1067+
if ($fetchMode === ClassMetadata::FETCH_EAGER && $condExpr !== null) {
10661068
throw QueryException::eagerFetchJoinWithNotAllowed($assoc['sourceEntity'], $assoc['fieldName']);
10671069
}
10681070

tests/Tests/ORM/Functional/EagerFetchCollectionTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ public function testSubselectFetchJoinWithNotAllowed(): void
8888
$query->getResult();
8989
}
9090

91+
public function testSubselectFetchJoinWithAllowedWhenOverriddenNotEager(): void
92+
{
93+
$query = $this->_em->createQuery('SELECT o, c FROM ' . EagerFetchOwner::class . ' o JOIN o.children c WITH c.id = 1');
94+
$query->setFetchMode(EagerFetchChild::class, 'owner', ORM\ClassMetadata::FETCH_LAZY);
95+
96+
$this->assertIsString($query->getSql());
97+
}
98+
9199
public function testEagerFetchWithIterable(): void
92100
{
93101
$this->createOwnerWithChildren(2);

0 commit comments

Comments
 (0)