silverstripe-framework/docs/en/02_Developer_Guides/00_Model/06_SearchFilters.md
Aaron Carlino 6888901468
NEW: Update docs to be compliant with Gatsby site (#9314)
* First cut

* Temporarily disable composer.json for netlify build

* POC

* New recursive directory query, various refinements

* Fix flexbox

* new styled components plugin

* Apply frontmatter delimiters

* Mobile styles, animation

* Search

* Redesign, clean up

* Nuke the cache, try again

* fix file casing

* Remove production env file

* ID headers

* Move app to new repo

* Add frontmatter universally

* Hide children changelogs

* Add how to title

* New callout tags

* Revert inline code block change

* Replace note callouts

* Fix icons

* Repalce images

* Fix icon

* Fix image links

* Use proper SQL icon
2019-11-18 17:58:33 +13:00

2.7 KiB

title summary icon
SearchFilter Modifiers Use suffixes on your ORM queries. search

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.

An example of a SearchFilter in use:

// fetch any player that starts with a S
$players = Player::get()->filter([
    'FirstName:StartsWith' => 'S',
    'PlayerNumber:GreaterThan' => '10'
]);

// to fetch any player that's name contains the letter 'z'
$players = Player::get()->filterAny([
    'FirstName:PartialMatch' => 'z',
    'LastName:PartialMatch' => 'z'
]);

Developers can define their own SearchFilter if needing to extend the ORM filter and exclude behaviors.

These suffixes can also take modifiers themselves. The modifiers currently supported are ":not", ":nocase" and ":case". These negate the filter, make it case-insensitive and make it case-sensitive, respectively. The default 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 within the DataListFilter. prefixed namespace. New filters can be registered using the below yml config:

SilverStripe\Core\Injector\Injector:
  DataListFilter.CustomMatch:
    class: MyVendor\Search\CustomMatchFilter

The following is a query which will return everyone whose first name starts with "S", either lowercase or uppercase:

$players = Player::get()->filter([
    'FirstName:StartsWith:nocase' => 'S'
]);

// use :not to perform a converse operation to filter anything but a 'W'
$players = Player::get()->filter([
    'FirstName:StartsWith:not' => 'W'
]);

API Documentation