Skip to content

Commit

Permalink
fix: force disabling fetch join collection on Doctrine ORM paginator …
Browse files Browse the repository at this point in the history
…when the query has no max results set
  • Loading branch information
Kreyu committed Nov 22, 2023
1 parent 6ce880c commit 94a64b4
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/Bridge/Doctrine/Orm/Query/DoctrineOrmProxyQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,16 @@ public function paginate(PaginationData $paginationData): void
*/
public function getPagination(): PaginationInterface
{
$paginator = $this->createPaginator();
$maxResults = $this->queryBuilder->getMaxResults();

$paginator = $this->createPaginator(forceDisabledFetchJoinCollection: null === $maxResults);

try {
return new Pagination(
items: $paginator->getIterator(),
currentPageNumber: $this->getCurrentPageNumber(),
totalItemCount: $paginator->count(),
itemNumberPerPage: $this->queryBuilder->getMaxResults(),
itemNumberPerPage: $maxResults,
);
} catch (CurrentPageOutOfRangeException) {
$this->queryBuilder->setFirstResult(null);
Expand All @@ -105,7 +107,7 @@ public function getPagination(): PaginationInterface

public function getItems(): iterable
{
$paginator = $this->createPaginator();
$paginator = $this->createPaginator(forceDisabledFetchJoinCollection: true);

$batchSize = $this->batchSize;

Expand Down Expand Up @@ -177,7 +179,7 @@ private function getCurrentPageNumber(): int
return (int) ($firstResult / $maxResults) + 1;
}

private function createPaginator(): Paginator
private function createPaginator(bool $forceDisabledFetchJoinCollection = false): Paginator
{
$rootEntity = current($this->queryBuilder->getRootEntities());

Expand All @@ -199,7 +201,13 @@ private function createPaginator(): Paginator

$query->setHydrationMode($this->hydrationMode);

return new Paginator($query, $hasSingleIdentifierName && $hasJoins);
$fetchJoinCollection = $hasSingleIdentifierName && $hasJoins;

if ($forceDisabledFetchJoinCollection) {
$fetchJoinCollection = false;
}

return new Paginator($query, $fetchJoinCollection);
}

private function applyQueryHints(Query $query): void
Expand Down

0 comments on commit 94a64b4

Please sign in to comment.