Version: current

# Class SearchContext

Manages searching of properties on one or more `DataObject` types, based on a given set of input parameters. SearchContext is intentionally decoupled from any controller-logic, it just receives a set of search parameters and an object class it acts on.

The default output of a SearchContext is either a `SQLQuery` object for further refinement, or a `DataObjectSet` that can be used to display search results, e.g. in a `TableListField` instance.

In case you need multiple contexts, consider namespacing your request parameters by using FieldSet->namespace() on the $fields constructor parameter.

Each DataObject subclass can have multiple search contexts for different cases, e.g. for a limited frontend search and a fully featured backend search. By default, you can use DataObject->getDefaultSearchContext() which is automatically scaffolded. It uses `DataObject::$searchable_fields` to determine which fields to include.

  Object
  SearchContext

Package: sapphire\search
See: http://doc.silverstripe.com/doku.php?id=searchcontext
Used by: ``BookingOrderAdmin``
Used by: ``ImportCatalog1C_ProductProp_Admin``
Used by: ``ModelAdmin``
Used by: ``ModelAdmin::getModelForms()``
Used by: ``MonumentAdmin``
Used by: ``PaymentAdmin``
Used by: ``ProductImport1CAdmin``
Used by: ``ProductImportAdmin``
Used by: ``RealtyImportAdmin``
Used by: ``RedirectEntry_Admin``
Used by: ``RoomServiceAdmin``
Used by: ``VAT_Admin``
Used by: ``VKNotificationQueueAdmin``
Located at sapphire/search/SearchContext.php

## Methods summary

`public` | # `__construct( string $modelClass, `FieldSet` $fields = null, array $filters = null )` A key value pair of values that should be searched for. The keys should match the field names specified in `SearchContext::$fields`. Usually these values come from a submitted searchform in the form of a $_REQUEST object. CAUTION: All values should be treated as insecure client input. A key value pair of values that should be searched for. The keys should match the field names specified in `SearchContext::$fields`. Usually these values come from a submitted searchform in the form of a $_REQUEST object. CAUTION: All values should be treated as insecure client input. #### Parameters $modelClass string $modelClass The base `DataObject` class that search properties related to. Also used to generate a set of result objects based on this class. $fields FieldSet $fields Optional. FormFields mapping to `DataObject::$db` properties which are to be searched. Derived from modelclass using `DataObject::scaffoldSearchFields()` if left blank. $filters array $filters Optional. Derived from modelclass if left blank #### Overrides `Object::__construct`
`public `FieldSet`` | # `getSearchFields( )` Returns scaffolded search fields for UI. Returns scaffolded search fields for UI. #### Returns ``FieldSet`` FieldSet
`protected` | # `applyBaseTableFields( )`
`public `SQLQuery`` | # `getQuery( array $searchParams, string|array $sort = false, string|array $limit = false, `SQLQuery` $existingQuery = null )` Returns a SQL object representing the search context for the given list of query parameters. Returns a SQL object representing the search context for the given list of query parameters. #### Parameters $searchParams array $searchParams Map of search criteria, mostly taked from $_REQUEST. If a filter is applied to a relationship in dot notation, the parameter name should have the dots replaced with double underscores, for example "Comments__Name" instead of the filter name "Comments.Name". $sort string|array $sort Database column to sort on. Falls back to `DataObject::$default_sort` if not provided. $limit string|array $limit $existingQuery SQLQuery $existingQuery #### Returns ``SQLQuery`` SQLQuery
`public `DataObjectSet`` | # `getResults( array $searchParams, string|array $sort = false, string|array $limit = false )` Returns a result set from the given search parameters. Returns a result set from the given search parameters. #### Parameters $searchParams array $searchParams $sort string|array $sort $limit string|array $limit #### Returns ``DataObjectSet`` DataObjectSet
`public boolean` | # `clearEmptySearchFields( unknown_type $value )` Callback map function to filter fields with empty values from being included in the search expression. Callback map function to filter fields with empty values from being included in the search expression. #### Parameters $value unknown_type $value #### Returns `boolean` boolean
`public `SearchFilter`` | # `getFilter( string $name )` Accessor for the filter attached to a named field. Accessor for the filter attached to a named field. #### Parameters $name string $name #### Returns ``SearchFilter`` SearchFilter
`public array` | # `getFilters( )` Get the map of filters in the current search context. Get the map of filters in the current search context. #### Returns `array` array
`public` | # `setFilters( array $filters )` Overwrite the current search context filter map. Overwrite the current search context filter map. #### Parameters $filters array $filters
`public` | # `addFilter( `SearchFilter` $filter )` Adds a instance of `SearchFilter`. Adds a instance of `SearchFilter`. #### Parameters $filter SearchFilter $filter
`public` | # `removeFilterByName( string $name )` Removes a filter by name. Removes a filter by name. #### Parameters $name string $name
`public `FieldSet`` | # `getFields( )` Get the list of searchable fields in the current search context. Get the list of searchable fields in the current search context. #### Returns ``FieldSet`` FieldSet
`public` | # `setFields( `FieldSet` $fields )` Apply a list of searchable fields to the current search context. Apply a list of searchable fields to the current search context. #### Parameters $fields FieldSet $fields
`public` | # `addField( `FormField` $field )` Adds a new `FormField` instance. Adds a new `FormField` instance. #### Parameters $field FormField $field
`public` | # `removeFieldByName( string $fieldName )` Removes an existing formfield instance by its name. Removes an existing formfield instance by its name. #### Parameters $fieldName string $fieldName

### Methods inherited from Object

`__call()`, `__toString()`, `__wakeup()`, `addMethodsFrom()`, `addStaticVars()`, `addWrapperMethod()`, `add_extension()`, `add_static_var()`, `allMethodNames()`, `cacheToFile()`, `cacheToFileWithArgs()`, `clearCache()`, `combined_static()`, `create()`, `createMethod()`, `create_from_string()`, `defineMethods()`, `exists()`, `extInstance()`, `extend()`, `getCustomClass()`, `getExtensionInstance()`, `getExtensionInstances()`, `get_extensions()`, `get_static()`, `hasExtension()`, `hasMethod()`, `has_extension()`, `invokeWithExtensions()`, `is_a()`, `loadCache()`, `parentClass()`, `parse_class_spec()`, `remove_extension()`, `sanitiseCachename()`, `saveCache()`, `set_stat()`, `set_static()`, `set_uninherited()`, `stat()`, `strong_create()`, `uninherited()`, `uninherited_static()`, `useCustomClass()`

### Magic methods summary

## Properties summary

`protected string` | $modelClass | # DataObject subclass to which search parameters relate to. Also determines as which object each result is provided. DataObject subclass to which search parameters relate to. Also determines as which object each result is provided.
`protected `FieldSet`` | $fields | # FormFields mapping to `DataObject::$db` properties which are supposed to be searchable. FormFields mapping to `DataObject::$db` properties which are supposed to be searchable.
`protected array` | $filters | # Array of `SearchFilter` subclasses. Array of `SearchFilter` subclasses.
`public string` | $connective | # The logical connective used to join WHERE clauses. Defaults to AND. The logical connective used to join WHERE clauses. Defaults to AND.

### Properties inherited from Object

`$class`, `$extension_instances`, `$extensions`

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -