Skip to content

Commit d333f5e

Browse files
committed
[CursorPaginator] Fix cursor building when ordering by ManyToOne association
1 parent e981cce commit d333f5e

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/Tools/Pagination/CursorPaginator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,13 @@ private function getParametersForItem(mixed $item): array
325325
$fieldName = $orderByItem->expression->field;
326326
$orderMetadata = $orderByItem->metadata ?? $metadata;
327327
$value = $metadata?->getFieldValue($item, $fieldName) ?? $item->$fieldName;
328-
$type = PersisterHelper::getTypeOfField($fieldName, $orderMetadata, $em)[0];
328+
329+
if ($value !== null && $orderMetadata !== null && isset($orderMetadata->associationMappings[$fieldName])) {
330+
$targetMetadata = $em->getClassMetadata($orderMetadata->associationMappings[$fieldName]->targetEntity);
331+
$value = $targetMetadata->getIdentifierValues($value)[$targetMetadata->getSingleIdentifierFieldName()] ?? null;
332+
}
333+
334+
$type = PersisterHelper::getTypeOfField($fieldName, $orderMetadata, $em)[0];
329335

330336
$result[$orderByItem->paramKey] = $connection->convertToDatabaseValue($value, $type);
331337
}

tests/Tests/ORM/Tools/Pagination/CursorPaginatorTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,4 +482,32 @@ public function testCloneQueryCopiesExistingHints(): void
482482

483483
self::assertSame(1, $paginator->countPageItems());
484484
}
485+
486+
public function testGetNextCursorUnwrapsManyToOneAssociationToIdentifier(): void
487+
{
488+
$author = new Author();
489+
$author->id = 42;
490+
$blogPost1 = new BlogPost();
491+
$blogPost1->author = $author;
492+
493+
$author2 = new Author();
494+
$author2->id = 43;
495+
$blogPost2 = new BlogPost();
496+
$blogPost2->author = $author2;
497+
498+
$this->hydrator->method('hydrateAll')->willReturn([$blogPost1, $blogPost2]);
499+
$result = $this->createMock(Result::class);
500+
$this->connection->method('executeQuery')->willReturn($result);
501+
502+
$query = new Query($this->em);
503+
$query->setDQL('SELECT b FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost b ORDER BY b.author ASC');
504+
505+
$paginator = new CursorPaginator($query, queryProducesDuplicates: false);
506+
$paginator->paginate(null, 1);
507+
508+
self::assertTrue($paginator->hasNextPage());
509+
510+
$cursor = $paginator->getNextCursor();
511+
self::assertSame(42, $cursor->getParameters()['b.author']);
512+
}
485513
}

0 commit comments

Comments
 (0)