Skip to content

Commit 5507cad

Browse files
committed
comparison filter
1 parent 9af65a2 commit 5507cad

1 file changed

Lines changed: 116 additions & 0 deletions

File tree

core/doctrine-filters.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ To add some search filters, choose over this new list:
186186
- [PartialSearchFilter](#partial-search-filter) (filter using a `LIKE %value%`)
187187
- [FreeTextQueryFilter](#free-text-query-filter) (allows you to apply multiple filters to multiple properties of a resource at the same time, using a single parameter in the URL)
188188
- [OrFilter](#or-filter) (apply a filter using `orWhere` instead of `andWhere` )
189+
- [ComparisonFilter](#comparison-filter) (add `gt`, `gte`, `lt`, `lte` operators to an equality filter)
189190

190191
### Legacy SearchFilter (API Platform < 4.2))
191192

@@ -491,6 +492,121 @@ This request will return all chickens where:
491492
- OR
492493
- the `ean` is exactly "FR123456".
493494

495+
## Comparison Filter
496+
497+
> [!NOTE]
498+
> `ComparisonFilter` is experimental and its API may change before a stable release.
499+
500+
The comparison filter is a decorator that wraps an equality filter (such as `ExactFilter`) and adds comparison
501+
operators to it. It lets clients filter a collection using greater-than, greater-than-or-equal, less-than,
502+
less-than-or-equal, and not-equal comparisons on any filterable property.
503+
504+
Syntax: `?parameter[<gt|gte|lt|lte|ne>]=value`
505+
506+
Available operators:
507+
508+
| Operator | SQL equivalent | Description |
509+
|---|---|---|
510+
| `gt` | `>` | Strictly greater than |
511+
| `gte` | `>=` | Greater than or equal to |
512+
| `lt` | `<` | Strictly less than |
513+
| `lte` | `<=` | Less than or equal to |
514+
| `ne` | `!=` | Not equal to |
515+
516+
`ComparisonFilter` is a decorator: it is applied by wrapping another filter. The canonical pairing is with `ExactFilter`.
517+
It works for Doctrine ORM (`ApiPlatform\Doctrine\Orm\Filter\ComparisonFilter`) and Doctrine MongoDB ODM
518+
(`ApiPlatform\Doctrine\Odm\Filter\ComparisonFilter`).
519+
520+
```php
521+
<?php
522+
// api/src/ApiResource/Product.php
523+
namespace App\ApiResource;
524+
525+
use ApiPlatform\Doctrine\Orm\Filter\ComparisonFilter;
526+
use ApiPlatform\Doctrine\Orm\Filter\ExactFilter;
527+
use ApiPlatform\Metadata\ApiResource;
528+
use ApiPlatform\Metadata\GetCollection;
529+
use ApiPlatform\Metadata\QueryParameter;
530+
531+
#[ApiResource]
532+
#[GetCollection(
533+
parameters: [
534+
'price' => new QueryParameter(
535+
filter: new ComparisonFilter(new ExactFilter()),
536+
property: 'price',
537+
),
538+
],
539+
)]
540+
class Product
541+
{
542+
// ...
543+
}
544+
```
545+
546+
Given that the collection endpoint is `/products`, you can filter products by price range with the following queries:
547+
548+
- `/products?price[gt]=10` — products whose price is strictly greater than 10
549+
- `/products?price[gte]=10` — products whose price is greater than or equal to 10
550+
- `/products?price[lt]=100` — products whose price is strictly less than 100
551+
- `/products?price[lte]=100` — products whose price is less than or equal to 100
552+
- `/products?price[ne]=0` — products whose price is not equal to 0
553+
554+
### Range Queries (Combining Operators)
555+
556+
There is no dedicated `between` operator. To filter within a range, combine `gte` and `lte` (or `gt` and `lt`) in a
557+
single request:
558+
559+
```
560+
GET /products?price[gte]=10&price[lte]=100
561+
```
562+
563+
This returns all products whose price is between 10 and 100 inclusive.
564+
565+
### DateTime Support
566+
567+
`ComparisonFilter` accepts `DateTimeInterface` values. When the underlying property is typed as a `DateTime` or
568+
`DateTimeImmutable`, API Platform automatically casts the raw string from the query string into a `DateTimeImmutable`
569+
before passing it to the filter. Any format accepted by the PHP
570+
[`DateTimeImmutable` constructor](https://www.php.net/manual/en/datetime.construct.php) is valid.
571+
572+
```php
573+
<?php
574+
// api/src/ApiResource/Event.php
575+
namespace App\ApiResource;
576+
577+
use ApiPlatform\Doctrine\Orm\Filter\ComparisonFilter;
578+
use ApiPlatform\Doctrine\Orm\Filter\ExactFilter;
579+
use ApiPlatform\Metadata\ApiResource;
580+
use ApiPlatform\Metadata\GetCollection;
581+
use ApiPlatform\Metadata\QueryParameter;
582+
583+
#[ApiResource]
584+
#[GetCollection(
585+
parameters: [
586+
'startDate' => new QueryParameter(
587+
filter: new ComparisonFilter(new ExactFilter()),
588+
property: 'startDate',
589+
),
590+
],
591+
)]
592+
class Event
593+
{
594+
// ...
595+
}
596+
```
597+
598+
Example request to fetch events starting after a given date:
599+
600+
```
601+
GET /events?startDate[gt]=2025-01-01T00:00:00Z
602+
```
603+
604+
### OpenAPI Documentation
605+
606+
`ComparisonFilter` automatically generates five OpenAPI query parameters for each configured parameter key, one per
607+
operator. For a parameter named `price`, the generated parameters are `price[gt]`, `price[gte]`, `price[lt]`,
608+
`price[lte]`, and `price[ne]`.
609+
494610
## Date Filter
495611

496612
The date filter allows filtering a collection by date intervals.

0 commit comments

Comments
 (0)