Skip to content

Commit 8dbbc00

Browse files
committed
This PR documents the new ExistFilter behavior proposed in the pull request api-platform/core#2243.
1 parent 4403515 commit 8dbbc00

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

core/configuration.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,13 @@ api_platform:
8989
api_keys: []
9090

9191
collection:
92+
# The name of the query parameter to filter nullable results (with the ExistsFilter).
93+
notnull_parameter_name: 'exists'
94+
9295
# The default order of results.
9396
order: 'ASC'
9497

95-
# The name of the query parameter to order results.
98+
# The name of the query parameter to order results (with the OrderFilter).
9699
order_parameter_name: 'order'
97100

98101
pagination:

core/filters.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ You can filter offers by joining two values, for example: `/offers?price[gt]=12.
361361

362362
The exists filter allows you to select items based on nullable field value.
363363

364-
Syntax: `?property[exists]=<true|false|1|0>`
364+
Syntax: `?exists[property]=<true|false|1|0>`
365365

366366
Enable the filter:
367367

@@ -385,10 +385,22 @@ class Offer
385385
}
386386
```
387387

388-
Given that the collection endpoint is `/offers`, you can filter offers on nullable field with the following query: `/offers?transportFees[exists]=true`.
388+
Given that the collection endpoint is `/offers`, you can filter offers on nullable field with the following query: `/offers?exists[transportFees]=true`.
389389

390390
It will return all offers where `transportFees` is not `null`.
391391

392+
#### Using a Custom NotNull Query Parameter Name
393+
394+
A conflict will occur if `exists` is also the name of a property with the search filter enabled.
395+
Luckily, the query parameter name to use is configurable:
396+
397+
```yaml
398+
# api/config/packages/api_platform.yaml
399+
api_platform:
400+
collection:
401+
notnull_parameter_name: 'notempty' # the URL query parameter to use is now "notempty"
402+
```
403+
392404
### Order Filter (Sorting)
393405

394406
The order filter allows to sort a collection against the given properties.
@@ -680,7 +692,7 @@ final class RegexpFilter extends AbstractContextAwareFilter
680692
) {
681693
return;
682694
}
683-
695+
684696
$parameterName = $queryNameGenerator->generateParameterName($property); // Generate a unique parameter name to avoid collisions with other filters
685697
$queryBuilder
686698
->andWhere(sprintf('REGEXP(o.%s, :%s) = 1', $property, $parameterName))
@@ -1074,7 +1086,7 @@ class DummyCar
10741086
* @ApiFilter(SearchFilter::class, properties={"colors.prop": "ipartial"})
10751087
*/
10761088
private $colors;
1077-
1089+
10781090
// ...
10791091
}
10801092

0 commit comments

Comments
 (0)