From effe5c2e6fc2113a1f59b12d4e04bd19a1f40d0f Mon Sep 17 00:00:00 2001 From: Dylan Grech Date: Sat, 26 Sep 2020 00:02:21 +0200 Subject: [PATCH] #9706: Updated doc for case-sensitive filter (#9710) * #9706: Updated doc for case-sensitive filter * #9706: Updated doc for case-sensitive filter Co-authored-by: Dylan Grech --- .../00_Model/06_SearchFilters.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/docs/en/02_Developer_Guides/00_Model/06_SearchFilters.md b/docs/en/02_Developer_Guides/00_Model/06_SearchFilters.md index f12b52132..fc70be15f 100644 --- a/docs/en/02_Developer_Guides/00_Model/06_SearchFilters.md +++ b/docs/en/02_Developer_Guides/00_Model/06_SearchFilters.md @@ -9,6 +9,7 @@ icon: search The `filter` and `exclude` operations specify exact matches by default. However, when filtering `DataList`s, 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. + * [ExactMatchFilter](api:SilverStripe\ORM\Filters\ExactMatchFilter) * [StartsWithFilter](api:SilverStripe\ORM\Filters\StartsWithFilter) * [EndsWithFilter](api:SilverStripe\ORM\Filters\EndsWithFilter) * [PartialMatchFilter](api:SilverStripe\ORM\Filters\PartialMatchFilter) @@ -40,6 +41,31 @@ These suffixes can also take modifiers themselves. The modifiers currently suppo comparison uses the database's default. For MySQL and MSSQL, this is case-insensitive. For PostgreSQL, this is case-sensitive. +```php +// Fetch players that their FirstName is 'Sam' +// Caution: This might be case in-sensitive if MySQL or MSSQL is used +$players = Player::get()->filter([ + 'FirstName:ExactMatch' => 'Sam' +]); + +// Fetch players that their FirstName is 'Sam' (force case-sensitive) +$players = Player::get()->filter([ + 'FirstName:ExactMatch:case' => 'Sam' +]); + +// Fetch players that their FirstName is 'Sam' (force NOT case-sensitive) +$players = Player::get()->filter([ + 'FirstName:ExactMatch:nocase' => 'Sam' +]); +``` + +By default the `:ExactMatch` filter is applied, hence why we can shorthand the above to: +```php +$players = Player::get()->filter('FirstName', 'Sam'); // Default DB engine behaviour +$players = Player::get()->filter('FirstName:case', 'Sam'); // case-sensitive +$players = Player::get()->filter('FirstName:nocase', 'Sam'); // NOT 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: