Skip to content

Commit ca3a19b

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

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

core/pagination.md

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,38 @@ 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+
327+
### For a Specific Resource
328+
329+
```php
330+
<?php
331+
332+
// api/src/Entity/Book.php
333+
334+
use ApiPlatform\Core\Annotation\ApiFilter;
335+
use ApiPlatform\Core\Annotation\ApiResource;
336+
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Filter\OrderFilter;
337+
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Filter\RangeFilter;
338+
339+
/**
340+
* @ApiResource(attributes={
341+
* "pagination_partial"=true,
342+
* "pagination_via_cursor"={"field"="id", "direction"="DESC"}
343+
* )
344+
* @ApiFilter(RangeFilter::class, properties={"id"})
345+
* @ApiFilter(OrderFilter::class, properties={"id"="DESC"})
346+
*/
347+
class Book
348+
{
349+
// ...
350+
}
351+
```
352+
353+
To know more about cursor-based pagination take a look at [this blog post on medium](https://medium.com/@sroze/74fd1d324723).
354+
323355
## Avoiding double SQL requests on Doctrine ORM
324356

325357
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 +403,21 @@ use Doctrine\Common\Collections\Criteria;
371403
class BookRepository extends ServiceEntityRepository
372404
{
373405
const ITEMS_PER_PAGE = 20;
374-
406+
375407
private $tokenStorage;
376-
408+
377409
public function __construct(
378410
RegistryInterface $registry,
379411
TokenStorageInterface $tokenStorage
380412
) {
381413
$this->tokenStorage = $tokenStorage;
382414
parent::__construct($registry, Book::class);
383415
}
384-
416+
385417
public function getBooksByFavoriteAuthor(int $page = 1): Paginator
386418
{
387419
$firstResult = ($page -1) * self::ITEMS_PER_PAGE;
388-
420+
389421
$user = $this->tokenStorage->getToken()->getUser();
390422
$queryBuilder = $this->createQueryBuilder();
391423
$queryBuilder->select('b')
@@ -446,11 +478,11 @@ namespace App\Repository;
446478
class BookRepository extends ServiceEntityRepository
447479
{
448480
// constant, variables and constructor...
449-
481+
450482
public function getBooksByFavoriteAuthor(int $page = 1): Paginator
451483
{
452484
$firstResult = ($page -1) * self::ITEMS_PER_PAGE;
453-
485+
454486
$user = $this->tokenStorage->getToken()->getUser();
455487
$queryBuilder = $this->createQueryBuilder();
456488
$queryBuilder->select('b')

0 commit comments

Comments
 (0)