Skip to content

Update the ExistsFilter behavior #613

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

Merged
Merged
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
5 changes: 4 additions & 1 deletion core/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,13 @@ api_platform:
api_keys: []

collection:
# The name of the query parameter to filter nullable results (with the ExistsFilter).
exists_parameter_name: 'exists'

# The default order of results.
order: 'ASC'

# The name of the query parameter to order results.
# The name of the query parameter to order results (with the OrderFilter).
order_parameter_name: 'order'

pagination:
Expand Down
18 changes: 16 additions & 2 deletions core/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,9 @@ You can filter offers by joining two values, for example: `/offers?price[gt]=12.

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

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

Previous syntax (deprecated): `?property[exists]=<true|false|1|0>`

Enable the filter:

Expand All @@ -393,10 +395,22 @@ class Offer
}
```

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

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

#### Using a Custom Exists Query Parameter Name

A conflict will occur if `exists` is also the name of a property with the search filter enabled.
Luckily, the query parameter name to use is configurable:

```yaml
# api/config/packages/api_platform.yaml
api_platform:
collection:
exists_parameter_name: 'not_null' # the URL query parameter to use is now "not_null"
```

### Order Filter (Sorting)

The order filter allows to sort a collection against the given properties.
Expand Down