You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: core/doctrine-filters.md
+33-33Lines changed: 33 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,12 @@
1
1
# Doctrine ORM and MongoDB ODM Filters
2
2
3
+
## Introduction
4
+
3
5
For further documentation on filters (including for Eloquent and Elasticsearch), please see the [Filters documentation](filters.md).
4
6
5
7
> [!WARNING]
6
8
> For maximum flexibility and to ensure future compatibility, it is strongly recommended to configure your filters via
7
-
> the parameters attribute using `QueryParameter`. The legacy method using the `ApiFilter` attribute is **deprecated** and
9
+
> the parameters attribute using `QueryParameter`. The legacy method using the `ApiFilter` attribute is **deprecated** and
8
10
> will be **removed** in version **5.0**.
9
11
10
12
The modern way to declare filters is to associate them directly with an operation's parameters. This allows for more
@@ -32,7 +34,7 @@ class Book {
32
34
}
33
35
```
34
36
> [!TIP]
35
-
> This filter can be also defined directly on a specific operation like `#[GetCollection(...)])` for finer
37
+
> This filter can be also defined directly on a specific operation like `#[GetCollection(...)])` for finer
36
38
> control, like the following code:
37
39
38
40
```php
@@ -179,7 +181,7 @@ services all begin with `api_platform.doctrine_mongodb.odm`.
179
181
### Built-in new Search Filters (API Platform >= 4.2)
180
182
181
183
To add some search filters, choose over this new list:
182
-
- [IriFilter](#iri-filter) (filter on IRIs)
184
+
- [IriFilter](#iri-filter) (filter on IRIs)
183
185
- [ExactFilter](#exact-filter) (filter with exact value)
184
186
- [PartialSearchFilter](#partial-search-filter) (filter using a `LIKE %value%``)
185
187
- [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)
@@ -313,7 +315,7 @@ The iri filter allows filtering a resource using IRIs.
313
315
314
316
Syntax: `?property=value`
315
317
316
-
The value can take any [IRI(Internationalized Resource Identifier)](https://en.wikipedia.org/wiki/Internationalized_Resource_Identifier).
318
+
The value can take any [IRI(Internationalized Resource Identifier)](https://en.wikipedia.org/wiki/Internationalized_Resource_Identifier).
317
319
318
320
Like other [new search filters](#built-in-new-search-filters-api-platform--42) it can be used on the ApiResource attribute
319
321
or in the operation attribute, for e.g., the `#GetCollection()` attribute:
@@ -486,7 +488,7 @@ Given that the endpoint is `/chickens`, you can filter chickens by name with the
486
488
This request will return all chickens where:
487
489
488
490
- the `name` is exactly "FR123456"
489
-
- OR
491
+
- OR
490
492
- the `ean` is exactly "FR123456".
491
493
492
494
## Date Filter
@@ -1402,7 +1404,7 @@ Advantages of the new approach:
1402
1404
- Clarity and Code Quality: The logic is more direct and decoupled.
1403
1405
- Tooling: A make command is available to generate all the boilerplate code.
1404
1406
1405
-
#### Generating the Filter Skeleton
1407
+
#### Generating the Filter ORM Skeleton
1406
1408
1407
1409
To get started, API Platform includes a very handy make command to generate the basic structure of an ORM filter:
1408
1410
@@ -1495,10 +1497,8 @@ class MonthFilter implements FilterInterface
1495
1497
}
1496
1498
```
1497
1499
1498
-
Now that the filter is created, it must be associated with an API resource. We use the `#[QueryParameter]` attribute on
1499
-
a `GetCollection` operation for this. For other synthax please refer to
1500
-
1501
-
[//]: # (TODO: add link to synthax)
1500
+
Now that the filter is created, it must be associated with an API resource. We use the `#[QueryParameter]` attribute on
1501
+
a `GetCollection` operation for this. For other syntax please refer to the [documentation](#introduction).
1502
1502
1503
1503
```php
1504
1504
<?php
@@ -1512,9 +1512,9 @@ use App\Filters\MonthFilter;
1512
1512
1513
1513
#[GetCollection(
1514
1514
parameters: [
1515
-
'createdAtMonth' => new QueryParameter(
1516
-
filter: new MonthFilter(),
1517
-
property: 'createdAt'
1515
+
'createdAtMonth' => new QueryParameter(
1516
+
filter: new MonthFilter(),
1517
+
property: 'createdAt'
1518
1518
),
1519
1519
]
1520
1520
)]
@@ -1527,9 +1527,9 @@ class Invoice
1527
1527
And that's it! ✅ Your filter is operational. A request like `GET /invoices?createdAtMonth=7` will now correctly return
1528
1528
the invoices from July!
1529
1529
1530
-
#### Adding Custom Filter Validation And A Better Typing
1530
+
#### Adding Custom Filter ORM Validation And A Better Typing
1531
1531
1532
-
Currently, our filter accepts any value, like `createdAtMonth=99` or `createdAtMonth=foo`, which could cause errors.
1532
+
Currently, our filter accepts any value, like `createdAtMonth=99` or `createdAtMonth=foo`, which could cause errors.
1533
1533
To validate inputs and ensure the correct type, we can implement the `JsonSchemaFilterInterface`.
1534
1534
1535
1535
This allows delegating validation to API Platform, respecting the [SOLID Principles](https://en.wikipedia.org/wiki/SOLID).
@@ -1550,8 +1550,8 @@ final class MonthFilter implements FilterInterface, JsonSchemaFilterInterface
1550
1550
public function getSchema(Parameter $parameter): array
0 commit comments