2011-02-07 07:48:44 +01:00
|
|
|
# SearchContext
|
|
|
|
|
|
|
|
## Introduction
|
|
|
|
|
|
|
|
Manages searching of properties on one or more `[api:DataObject]` types, based on a given set of input parameters.
|
2011-03-08 22:05:51 +01:00
|
|
|
`[api:SearchContext]` is intentionally decoupled from any controller-logic,
|
2011-02-07 07:48:44 +01:00
|
|
|
it just receives a set of search parameters and an object class it acts on.
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
The default output of a `[api:SearchContext]` is either a `[api:SQLSelect]` object for further refinement, or a
|
2011-02-07 07:48:44 +01:00
|
|
|
`[api:DataObject]` instance.
|
|
|
|
|
2011-10-28 03:37:27 +02:00
|
|
|
In case you need multiple contexts, consider namespacing your request parameters by using `FieldList->namespace()` on
|
2011-02-07 07:48:44 +01:00
|
|
|
the $fields constructor parameter.
|
|
|
|
|
2012-06-28 11:43:56 +02:00
|
|
|
`[api:SearchContext]` is mainly used by `[ModelAdmin](/reference/modeladmin)`, our generic data administration interface. Another
|
2011-02-07 07:48:44 +01:00
|
|
|
implementation can be found in generic frontend search forms through the [genericviews](http://silverstripe.org/generic-views-module) module.
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
Getting results
|
|
|
|
|
|
|
|
:::php
|
|
|
|
singleton('MyDataObject')->getDefaultSearchContext();
|
|
|
|
|
|
|
|
|
|
|
|
### Defining fields on your DataObject
|
|
|
|
|
|
|
|
See `[api:DataObject::$searchable_fields]`.
|
|
|
|
|
|
|
|
### Customizing fields and filters
|
|
|
|
|
2011-03-08 22:05:51 +01:00
|
|
|
In this example we're defining three attributes on our MyDataObject subclass: `PublicProperty`, `HiddenProperty`
|
2011-02-07 07:48:44 +01:00
|
|
|
and `MyDate`. The attribute `HiddenProperty` should not be searchable, and `MyDate` should only search for dates
|
2011-03-08 22:05:51 +01:00
|
|
|
*after* the search entry (with a `GreaterThanFilter`). Similiar to the built-in `DataObject->getDefaultSearchContext()`
|
|
|
|
method, we're building our own `getCustomSearchContext()` variant.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
|
|
|
class MyDataObject extends DataObject {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2011-02-07 07:48:44 +01:00
|
|
|
'PublicProperty' => 'Text'
|
|
|
|
'HiddenProperty' => 'Text',
|
|
|
|
'MyDate' => 'Date'
|
|
|
|
);
|
|
|
|
|
|
|
|
public function getCustomSearchContext() {
|
|
|
|
$fields = $this->scaffoldSearchFields(array(
|
|
|
|
'restrictFields' => array('PublicProperty','MyDate')
|
|
|
|
));
|
|
|
|
$filters = array(
|
|
|
|
'PublicProperty' => new PartialMatchFilter('PublicProperty'),
|
|
|
|
'MyDate' => new GreaterThanFilter('MyDate')
|
|
|
|
);
|
|
|
|
return new SearchContext(
|
|
|
|
$this->class,
|
|
|
|
$fields,
|
|
|
|
$filters
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
### Generating a search form from the context
|
|
|
|
|
|
|
|
:::php
|
|
|
|
class Page_Controller extends ContentController {
|
|
|
|
public function SearchForm() {
|
|
|
|
$context = singleton('MyDataObject')->getCustomSearchContext();
|
|
|
|
$fields = $context->getSearchFields();
|
|
|
|
$form = new Form($this, "SearchForm",
|
|
|
|
$fields,
|
2011-10-28 03:37:27 +02:00
|
|
|
new FieldList(
|
2011-02-07 07:48:44 +01:00
|
|
|
new FormAction('doSearch')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
public function doSearch($data, $form) {
|
|
|
|
$context = singleton('MyDataObject')->getCustomSearchContext();
|
|
|
|
$results = $context->getResults($data);
|
|
|
|
return $this->customise(array(
|
|
|
|
'Results' => $results
|
|
|
|
))->renderWith('Page_results');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
### Pagination
|
|
|
|
|
2011-03-31 03:01:04 +02:00
|
|
|
For pagination records on multiple pages, you need to wrap the results in a
|
2013-06-21 00:32:08 +02:00
|
|
|
`PaginatedList` object. This object is also passed the generated `SQLSelect`
|
2011-03-31 03:01:04 +02:00
|
|
|
in order to read page limit information. It is also passed the current
|
|
|
|
`SS_HTTPRequest` object so it can read the current page from a GET var.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
2011-03-31 03:01:04 +02:00
|
|
|
public function getResults($searchCriteria = array()) {
|
2011-02-07 07:48:44 +01:00
|
|
|
$start = ($this->request->getVar('start')) ? (int)$this->request->getVar('start') : 0;
|
|
|
|
$limit = 10;
|
|
|
|
|
|
|
|
$context = singleton('MyDataObject')->getCustomSearchContext();
|
|
|
|
$query = $context->getQuery($searchCriteria, null, array('start'=>$start,'limit'=>$limit));
|
|
|
|
$records = $context->getResults($searchCriteria, null, array('start'=>$start,'limit'=>$limit));
|
2011-03-31 03:01:04 +02:00
|
|
|
|
2011-02-07 07:48:44 +01:00
|
|
|
if($records) {
|
2011-03-31 03:01:04 +02:00
|
|
|
$records = new PaginatedList($records, $this->request);
|
|
|
|
$records->setPageStart($start);
|
2013-12-06 03:45:38 +01:00
|
|
|
$records->setPageLength($limit);
|
|
|
|
$records->setTotalItems($query->unlimitedRowCount());
|
2011-02-07 07:48:44 +01:00
|
|
|
}
|
2011-03-31 03:01:04 +02:00
|
|
|
|
2011-02-07 07:48:44 +01:00
|
|
|
return $records;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
notice that if you want to use this getResults function, you need to change the function doSearch for this one:
|
|
|
|
|
|
|
|
:::php
|
|
|
|
public function doSearch($data, $form) {
|
|
|
|
$context = singleton('MyDataObject')->getCustomSearchContext();
|
|
|
|
$results = $this->getResults($data);
|
|
|
|
return $this->customise(array(
|
|
|
|
'Results' => $results
|
|
|
|
))->renderWith(array('Catalogo_results', 'Page'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
The change is in **$results = $this->getResults($data);**, because you are using a custom getResults function.
|
|
|
|
|
|
|
|
Another thing you cant forget is to check the name of the singleton you are using in your project. the example uses
|
|
|
|
**MyDataObject**, you need to change it for the one you are using
|
|
|
|
|
|
|
|
For more information on how to paginate your results within the template, see [Tutorial: Site Search](/tutorials/4-site-search).
|
|
|
|
|
|
|
|
|
|
|
|
### The Pagination Template
|
|
|
|
|
|
|
|
to show the results of your custom search you need at least this content in your template, notice that
|
|
|
|
Results.PaginationSummary(4) defines how many pages the search will show in the search results. something like:
|
|
|
|
|
2014-09-15 21:44:03 +02:00
|
|
|
**Next 1 2 *3* 4 5 … 558**
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
:::ss
|
2013-04-26 11:48:59 +02:00
|
|
|
<% if $Results %>
|
2011-02-07 07:48:44 +01:00
|
|
|
<ul>
|
2013-04-26 11:48:59 +02:00
|
|
|
<% loop $Results %>
|
2012-06-26 17:32:46 +02:00
|
|
|
<li>$Title, $Autor</li>
|
|
|
|
<% end_loop %>
|
2011-02-07 07:48:44 +01:00
|
|
|
</ul>
|
|
|
|
<% else %>
|
|
|
|
<p>Sorry, your search query did not return any results.</p>
|
|
|
|
<% end_if %>
|
|
|
|
|
2013-04-26 11:48:59 +02:00
|
|
|
<% if $Results.MoreThanOnePage %>
|
2011-02-07 07:48:44 +01:00
|
|
|
<div id="PageNumbers">
|
|
|
|
<p>
|
2013-04-26 11:48:59 +02:00
|
|
|
<% if $Results.NotFirstPage %>
|
2011-02-07 07:48:44 +01:00
|
|
|
<a class="prev" href="$Results.PrevLink" title="View the previous page">Prev</a>
|
|
|
|
<% end_if %>
|
|
|
|
|
|
|
|
<span>
|
2013-04-26 11:48:59 +02:00
|
|
|
<% loop $Results.PaginationSummary(4) %>
|
|
|
|
<% if $CurrentBool %>
|
2011-02-07 07:48:44 +01:00
|
|
|
$PageNum
|
|
|
|
<% else %>
|
2013-04-26 11:48:59 +02:00
|
|
|
<% if $Link %>
|
2011-02-07 07:48:44 +01:00
|
|
|
<a href="$Link" title="View page number $PageNum">$PageNum</a>
|
|
|
|
<% else %>
|
|
|
|
…
|
|
|
|
<% end_if %>
|
|
|
|
<% end_if %>
|
2012-06-26 17:32:46 +02:00
|
|
|
<% end_loop %>
|
2011-02-07 07:48:44 +01:00
|
|
|
</span>
|
|
|
|
|
2013-04-26 11:48:59 +02:00
|
|
|
<% if $Results.NotLastPage %>
|
2011-02-07 07:48:44 +01:00
|
|
|
<a class="next" href="$Results.NextLink" title="View the next page">Next</a>
|
|
|
|
<% end_if %>
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<% end_if %>
|
|
|
|
|
|
|
|
|
|
|
|
## Available SearchFilters
|
|
|
|
|
2011-03-08 22:05:51 +01:00
|
|
|
See `[api:SearchFilter]` API Documentation
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
## API Documentation
|
|
|
|
`[api:SearchContext]`
|
|
|
|
|
|
|
|
## Related
|
|
|
|
|
2012-06-28 11:43:56 +02:00
|
|
|
* [ModelAdmin](/reference/modeladmin)
|
2012-06-04 10:21:29 +02:00
|
|
|
* [RestfulServer module](https://github.com/silverstripe/silverstripe-restfulserver)
|
2011-02-07 07:48:44 +01:00
|
|
|
* [Tutorial: Site Search](/tutorials/4-site-search)
|