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/filters.md
+46-59Lines changed: 46 additions & 59 deletions
Original file line number
Diff line number
Diff line change
@@ -1,28 +1,33 @@
1
1
# Filters
2
2
3
-
The bundle provides a generic system to apply filters on collections. Useful filters
4
-
for the Doctrine ORM are provided with the bundle. However the filter system is
5
-
extensible enough to let you create custom filters that would fit your specific needs
6
-
and for any data provider.
3
+
API Platform Core provides a generic system to apply filters on collections. Useful filters for the Doctrine ORM are provided
4
+
with the library. You can also create custom filters that would fit your specific needs.
5
+
You can also add filtering support to your custom [data providers](data-providers.md) by implementing interfaces provided
6
+
by the library.
7
7
8
8
By default, all filters are disabled. They must be enabled explicitly.
9
9
10
-
When a filter is enabled, it is automatically documented as a `hydra:search` property
11
-
in collection returns. It also automatically appears in the NelmioApiDoc documentation
12
-
if this bundle is active.
10
+
When a filter is enabled, it is automatically documented as a `hydra:search` property in the collection response. It also
11
+
automatically appears in the [NelmioApiDoc documentation](nelmio-api-doc.md) if it is available.
13
12
14
13
## Search filter
15
14
16
-
If Doctrine ORM support is enabled, adding filters is as easy as adding an entry
17
-
in your `app/config/services.yml`file and adding a Attributes in your entity.
15
+
If Doctrine ORM support is enabled, adding filters is as easy as registering a filter service in your `app/config/services.yml`
16
+
file and adding an attribute to your resource configuration.
18
17
19
-
The search filter supports `exact`, `partial`, `start`, `end`, and `word_start` matching strategies.
20
-
-`partial` strategy uses `LIKE %text%` to search for fields that containing the text.
21
-
-`start` strategy uses `LIKE text%` to search for fields that starts with text.
22
-
-`end` strategy uses `LIKE %text` to search for fields that ends with text.
23
-
-`word_start` strategy uses `LIKE text% OR LIKE % text%` to search for fields that contains the word starting with `text`.
18
+
The search filter supports `exact`, `partial`, `start`, `end`, and `word_start` matching strategies:
24
19
25
-
Prepend the letter `i` to the filter if you want it to be case insensitive. For example `ipartial` or `iexact`. Note that this will use the `LOWER` function and **will** impact performances if there is no [*function-based index*](http://use-the-index-luke.com/sql/where-clause/functions/case-insensitive-search).
20
+
*`partial` strategy uses `LIKE %text%` to search for fields that containing the text.
21
+
*`start` strategy uses `LIKE text%` to search for fields that starts with text.
22
+
*`end` strategy uses `LIKE %text` to search for fields that ends with text.
23
+
*`word_start` strategy uses `LIKE text% OR LIKE % text%` to search for fields that contains the word starting with `text`.
24
+
25
+
Prepend the letter `i` to the filter if you want it to be case insensitive. For example `ipartial` or `iexact`. Note that
26
+
this will use the `LOWER` function and **will** impact performances if there is no [*function-based index*](http://use-the-index-luke.com/sql/where-clause/functions/case-insensitive-search).
27
+
28
+
Case insensitivity may already be enforced at the database level depending on the [collation](https://en.wikipedia.org/wiki/Collation)
29
+
used. If you are using MySQL, note that the commonly used `utf8_unicode_ci` collation (and its sibling `utf8mb4_unicode_ci`)
30
+
are already case insensitive, as indicated by the `_ci` part in their names.
26
31
27
32
In the following example, we will see how to allow the filtering of a list of e-commerce offers:
If you use a service definition format other than YAML, you can use the `ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter::EXCLUDE_NULL`
145
+
constant directly.
145
146
146
147
## Order filter
147
148
@@ -177,10 +178,11 @@ class Offer
177
178
}
178
179
```
179
180
180
-
Given that the collection endpoint is `/offers`, you can filter offers by name in
181
-
ascending order and then by ID in descending order with the following query: `/offers?order[name]=desc&order[id]=asc`.
181
+
Given that the collection endpoint is `/offers`, you can filter offers by name in ascending order and then by ID in descending
182
+
order with the following query: `/offers?order[name]=desc&order[id]=asc`.
182
183
183
-
By default, whenever the query does not specify the direction explicitly (e.g: `/offers?order[name]&order[id]`), filters will not be applied unless you configure a default order direction to use:
184
+
By default, whenever the query does not specify the direction explicitly (e.g: `/offers?order[name]&order[id]`), filters
185
+
will not be applied unless you configure a default order direction to use:
184
186
185
187
```yaml
186
188
# app/config/services.yml
@@ -230,7 +232,7 @@ class Offer
230
232
231
233
Given that the collection endpoint is `/offers`, you can filter offers by boolean with the following query: `/offers?isAvailableGenericallyInMyCountry=true`.
232
234
233
-
It will return all offers with `isAvailableGenericallyInMyCountry` equals true
235
+
It will return all offers where `isAvailableGenericallyInMyCountry` equals `true`.
234
236
235
237
## Numeric Filter
236
238
@@ -285,9 +287,8 @@ api_platform:
285
287
286
288
## Filtering on nested properties
287
289
288
-
Sometimes, you need to be able to perform filtering based on some linked resources
289
-
(on the other side of a relation). All built-in filters support nested properties
290
-
using the dot (`.`) syntax, e.g.:
290
+
Sometimes, you need to be able to perform filtering based on some linked resources (on the other side of a relation). All
291
+
built-in filters support nested properties using the dot (`.`) syntax, e.g.:
291
292
292
293
```yaml
293
294
# app/config/services.yml
@@ -309,10 +310,9 @@ or order offers by the product's release date: `http://localhost:8000/api/offers
309
310
310
311
## Enabling a filter for all properties of a resource
311
312
312
-
As we have seen in previous examples, properties where filters can be applied must be
313
-
explicitly declared. But if you don't care about security and performance (ex:
314
-
an API with restricted access), it's also possible to enable builtin filters for
315
-
all properties:
313
+
As we have seen in previous examples, properties where filters can be applied must be explicitly declared. If you don't
314
+
care about security and performance (e.g. an API with restricted access), it is also possible to enable built-in filters
315
+
for all properties:
316
316
317
317
```yaml
318
318
# app/config/services.yml
@@ -343,8 +343,8 @@ It means that the filter will be **silently** ignored if the property:
343
343
Custom filters can be written by implementing the `ApiPlatform\Core\Api\FilterInterface`
344
344
interface.
345
345
346
-
If you use [custom data providers](data-providers.md), they must support filtering and be aware of active filters to
347
-
work properly.
346
+
If you use [custom data providers](data-providers.md), they must support filtering and be aware of active filters to work
347
+
properly.
348
348
349
349
### Creating custom Doctrine ORM filters
350
350
@@ -355,12 +355,10 @@ A convenient abstract class is also shipped with the bundle: `ApiPlatform\Core\B
355
355
356
356
### Overriding extraction of properties from the request
357
357
358
-
You can change the way the filter parameters are extracted from the request. This can be done by extending the parent
359
-
filter class and overriding the `extractProperties(\Symfony\Component\HttpFoundation\Request $request)`
358
+
You can change the way the filter parameters are extracted from the request. This can be done by overriding the `extractProperties(\Symfony\Component\HttpFoundation\Request $request)`
360
359
method.
361
360
362
-
In the following example, we will completely change the syntax of the order filter
363
-
to be the following: `?filter[order][property]`
361
+
In the following example, we will completely change the syntax of the order filter to be the following: `?filter[order][property]`
Beware: in [some cases](https://github.com/dunglas/DunglasApiBundle/issues/157#issuecomment-119576010) you may have to use double slashes in the class path to make it work:
0 commit comments