Skip to content

Commit 68c0da1

Browse files
committed
Document cursor-based pagination
1 parent 0865332 commit 68c0da1

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

core/pagination.md

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,39 @@ class Book
320320
}
321321
```
322322

323+
## Cursor based pagination
324+
325+
To configure your resource to use the cursor-based pagination, select your unique sorted field as well as the direction you’ll like the pagination to go via filters and enable the `pagination_via_cursor` option.
326+
Note that for now you have to declare a `RangeFilter` and an `OrderFilter` on the property used for the cursor-based pagination.
327+
328+
The following configuration also works on a specific operation:
329+
330+
```php
331+
<?php
332+
333+
// api/src/Entity/Book.php
334+
335+
use ApiPlatform\Core\Annotation\ApiFilter;
336+
use ApiPlatform\Core\Annotation\ApiResource;
337+
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Filter\OrderFilter;
338+
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Filter\RangeFilter;
339+
340+
/**
341+
* @ApiResource(attributes={
342+
* "pagination_partial"=true,
343+
* "pagination_via_cursor"={"field"="id", "direction"="DESC"}
344+
* )
345+
* @ApiFilter(RangeFilter::class, properties={"id"})
346+
* @ApiFilter(OrderFilter::class, properties={"id"="DESC"})
347+
*/
348+
class Book
349+
{
350+
// ...
351+
}
352+
```
353+
354+
To know more about cursor-based pagination take a look at [this blog post on medium](https://medium.com/@sroze/74fd1d324723).
355+
323356
## Avoiding double SQL requests on Doctrine ORM
324357

325358
By default, pagination assumes that there will be collection fetched on a resource and thus will set `useFetchJoinCollection` to `true` on the Doctrine Paginator class. Having this option implies that 2 SQL requests will be executed (so this avoid having less results than expected).
@@ -371,21 +404,21 @@ use Doctrine\Common\Collections\Criteria;
371404
class BookRepository extends ServiceEntityRepository
372405
{
373406
const ITEMS_PER_PAGE = 20;
374-
407+
375408
private $tokenStorage;
376-
409+
377410
public function __construct(
378411
RegistryInterface $registry,
379412
TokenStorageInterface $tokenStorage
380413
) {
381414
$this->tokenStorage = $tokenStorage;
382415
parent::__construct($registry, Book::class);
383416
}
384-
417+
385418
public function getBooksByFavoriteAuthor(int $page = 1): Paginator
386419
{
387420
$firstResult = ($page -1) * self::ITEMS_PER_PAGE;
388-
421+
389422
$user = $this->tokenStorage->getToken()->getUser();
390423
$queryBuilder = $this->createQueryBuilder();
391424
$queryBuilder->select('b')
@@ -446,11 +479,11 @@ namespace App\Repository;
446479
class BookRepository extends ServiceEntityRepository
447480
{
448481
// constant, variables and constructor...
449-
482+
450483
public function getBooksByFavoriteAuthor(int $page = 1): Paginator
451484
{
452485
$firstResult = ($page -1) * self::ITEMS_PER_PAGE;
453-
486+
454487
$user = $this->tokenStorage->getToken()->getUser();
455488
$queryBuilder = $this->createQueryBuilder();
456489
$queryBuilder->select('b')

0 commit comments

Comments
 (0)