silverstripe-framework/docs/en/02_Developer_Guides/12_Search/01_Searchcontext.md

121 lines
3.0 KiB
Markdown
Raw Normal View History

2014-10-28 06:42:27 +01:00
title: Scaffolding with SearchContext
summary: Configure the search form within ModelAdmin using the SearchContext class.
2014-10-28 06:42:27 +01:00
# SearchContext
2014-10-28 06:42:27 +01:00
[api:SearchContext] manages searching of properties on one or more [api:DataObject] types, based on a given set of
input parameters. [api:SearchContext] is intentionally decoupled from any controller-logic, it just receives a set of
search parameters and an object class it acts on.
2014-10-28 06:42:27 +01:00
The default output of a [api:SearchContext] is either a [api:SQLQuery] object for further refinement, or a
[api:DataObject] instance.
2014-10-28 06:42:27 +01:00
<div class="notice" markdown="1">
[api:SearchContext] is mainly used by [ModelAdmin](../customising_the_admin_interface/modeladmin).
2014-10-28 06:42:27 +01:00
</div>
## Usage
2014-10-28 06:42:27 +01:00
Defining search-able fields on your DataObject.
:::php
2014-10-28 06:42:27 +01:00
<?php
2014-10-28 06:42:27 +01:00
class MyDataObject extends DataObject {
2014-10-28 06:42:27 +01:00
private static $searchable_fields = array(
'Name',
'ProductCode'
);
}
2014-10-28 06:42:27 +01:00
## Customizing fields and filters
In this example we're defining three attributes on our MyDataObject subclass: `PublicProperty`, `HiddenProperty`
and `MyDate`. The attribute `HiddenProperty` should not be searchable, and `MyDate` should only search for dates
2014-10-28 06:42:27 +01:00
*after* the search entry (with a `GreaterThanFilter`).
:::php
2014-10-28 06:42:27 +01:00
<?php
class MyDataObject extends DataObject {
2014-10-28 06:42:27 +01:00
private static $db = array(
'PublicProperty' => 'Text'
'HiddenProperty' => 'Text',
'MyDate' => 'Date'
);
public function getCustomSearchContext() {
$fields = $this->scaffoldSearchFields(array(
'restrictFields' => array('PublicProperty','MyDate')
));
2014-10-28 06:42:27 +01:00
$filters = array(
'PublicProperty' => new PartialMatchFilter('PublicProperty'),
'MyDate' => new GreaterThanFilter('MyDate')
);
2014-10-28 06:42:27 +01:00
return new SearchContext(
$this->class,
$fields,
$filters
);
}
}
2014-10-28 06:42:27 +01:00
<div class="notice" markdown="1">
See the [SearchFilter](../model/searchfilters) documentation for more information about filters to use such as the
`GreaterThanFilter`.
</div>
<div class="notice" markdown="1">
In case you need multiple contexts, consider name-spacing your request parameters by using `FieldList->namespace()` on
the `$fields` constructor parameter.
</div>
### Generating a search form from the context
:::php
2014-10-28 06:42:27 +01:00
<?php
..
class Page_Controller extends ContentController {
2014-10-28 06:42:27 +01:00
public function SearchForm() {
$context = singleton('MyDataObject')->getCustomSearchContext();
$fields = $context->getSearchFields();
2014-10-28 06:42:27 +01:00
$form = new Form($this, "SearchForm",
$fields,
new FieldList(
new FormAction('doSearch')
)
);
2014-10-28 06:42:27 +01:00
return $form;
}
2014-10-28 06:42:27 +01:00
public function doSearch($data, $form) {
$context = singleton('MyDataObject')->getCustomSearchContext();
$results = $context->getResults($data);
2014-10-28 06:42:27 +01:00
return $this->customise(array(
'Results' => $results
))->renderWith('Page_results');
}
}
2014-10-28 06:42:27 +01:00
## Related Documentation
2014-10-28 06:42:27 +01:00
* [ModelAdmin](../customising_the_cms/modeladmin)
* [Tutorial: Site Search](/tutorials/site_search)
## API Documentation
2014-10-28 06:42:27 +01:00
* [api:SearchContext]
* [api:DataObject]