Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pagination): UuidRangeFilter #1520

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions core/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,28 @@ It will return all offers with `price` between 12.99 and 15.99.

You can filter offers by joining two values, for example: `/offers?price[gt]=12.99&price[lt]=19.99`.

### Uuid Range Filter

In case you want to filter based on UUIDs (V1 and V6) you'll need to use the `UuidRangeFilter` instead of the `RangeFilter`.
The syntax and behaviour is the same as the normal `RangeFilter`:

```php
<?php
// api/src/Entity/Offer.php
namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\UuidRangeFilter;

#[ApiResource]
#[ApiFilter(UuidRangeFilter::class, properties: ['price'])]
class Offer
{
// ...
}
```

### Exists Filter

The exists filter allows you to select items based on a nullable field value.
Expand Down
30 changes: 29 additions & 1 deletion core/pagination.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,14 @@ class Book
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 `paginationViaCursor` option.
Note that for now you have to declare a `RangeFilter` and an `OrderFilter` on the property used for the cursor-based pagination.

Please also keep in mind that the order is not applied by default, so you'll have to apply it yourself in order for the pagination to work correctly.

The following configuration also works on a specific operation:

```php
<?php

// api/src/Entity/Book.php
namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
Expand All @@ -338,6 +340,32 @@ class Book
}
```

If you are using a UUID as primary key (Uuid V1 or V6) you'll have to use the `UuidRangeFilter` instead:

```php
<?php

// api/src/Entity/Book.php

use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Filter\UuidRangeFilter;

#[ApiResource(attributes: [
paginationPartial: true,
paginationViaCursor: [
['field' => 'id', 'direction' => 'DESC']
]
)]
#[ApiFilter(UuidRangeFilter::class, properties: ["id"])]
#[ApiFilter(OrderFilter::class, properties: ["id" => "DESC"])]
class Book
{
// ...
}
```

To know more about cursor-based pagination take a look at [this blog post on medium (draft)](https://medium.com/@sroze/74fd1d324723).

## Controlling The Behavior of The Doctrine ORM Paginator
Expand Down