silverstripe-framework/docs/en/02_Developer_Guides/00_Model/06_SearchFilters.md

70 lines
2.5 KiB
Markdown
Raw Normal View History

2014-10-27 04:40:02 +01:00
title: SearchFilter Modifiers
summary: Use suffixes on your ORM queries.
# SearchFilter Modifiers
The `filter` and `exclude` operations specify exact matches by default. However, there are a number of suffixes that
you can put on field names to change this behavior. These are represented as `SearchFilter` subclasses and include.
* [StartsWithFilter](api:SilverStripe\ORM\Filters\StartsWithFilter)
* [EndsWithFilter](api:SilverStripe\ORM\Filters\EndsWithFilter)
* [PartialMatchFilter](api:SilverStripe\ORM\Filters\PartialMatchFilter)
* [GreaterThanFilter](api:SilverStripe\ORM\Filters\GreaterThanFilter)
* [GreaterThanOrEqualFilter](api:SilverStripe\ORM\Filters\GreaterThanOrEqualFilter)
* [LessThanFilter](api:SilverStripe\ORM\Filters\LessThanFilter)
* [LessThanOrEqualFilter](api:SilverStripe\ORM\Filters\LessThanOrEqualFilter)
2014-10-27 04:40:02 +01:00
An example of a `SearchFilter` in use:
```php
2014-10-27 04:40:02 +01:00
// fetch any player that starts with a S
2017-08-03 05:35:09 +02:00
$players = Player::get()->filter([
'FirstName:StartsWith' => 'S',
2014-10-27 04:40:02 +01:00
'PlayerNumber:GreaterThan' => '10'
2017-08-03 05:35:09 +02:00
]);
2014-10-27 04:40:02 +01:00
// to fetch any player that's name contains the letter 'z'
2017-08-03 05:35:09 +02:00
$players = Player::get()->filterAny([
'FirstName:PartialMatch' => 'z',
2014-10-27 04:40:02 +01:00
'LastName:PartialMatch' => 'z'
2017-08-03 05:35:09 +02:00
]);
```
2014-10-27 04:40:02 +01:00
Developers can define their own [SearchFilter](api:SilverStripe\ORM\Filters\SearchFilter) if needing to extend the ORM filter and exclude behaviors.
2014-10-27 04:40:02 +01:00
These suffixes can also take modifiers themselves. The modifiers currently supported are `":not"`, `":nocase"` and
2014-10-28 04:45:46 +01:00
`":case"`. These negate the filter, make it case-insensitive and make it case-sensitive, respectively. The default
2014-10-27 04:40:02 +01:00
comparison uses the database's default. For MySQL and MSSQL, this is case-insensitive. For PostgreSQL, this is
case-sensitive.
Note that all search filters (e.g. `:PartialMatch`) refer to services registered with [Injector](api:SilverStripe\Core\Injector\Injector)
within the `DataListFilter.` prefixed namespace. New filters can be registered using the below yml
config:
```yaml
2017-02-22 23:42:36 +01:00
SilverStripe\Core\Injector\Injector:
DataListFilter.CustomMatch:
class: MyVendor/Search/CustomMatchFilter
```
2014-10-28 04:45:46 +01:00
The following is a query which will return everyone whose first name starts with "S", either lowercase or uppercase:
2014-10-27 04:40:02 +01:00
```php
2017-08-03 05:35:09 +02:00
$players = Player::get()->filter([
2014-10-27 04:40:02 +01:00
'FirstName:StartsWith:nocase' => 'S'
2017-08-03 05:35:09 +02:00
]);
2014-10-27 04:40:02 +01:00
// use :not to perform a converse operation to filter anything but a 'W'
2017-08-03 05:35:09 +02:00
$players = Player::get()->filter([
2014-10-27 04:40:02 +01:00
'FirstName:StartsWith:not' => 'W'
2017-08-03 05:35:09 +02:00
]);
```
2014-10-27 04:40:02 +01:00
## API Documentation
* [SearchFilter](api:SilverStripe\ORM\Filters\SearchFilter)