PHP | Problem - SearchForm & ManyMany Relations
class Geraet extends DataObjectAsPage {
static $db = array(
"Kauf" => "Boolean",
"Occasion" => "Boolean",
"Featured" => "Boolean",
"Hersteller" => "Varchar(255)",
"Miete" => "Boolean",
"Verlinkung" => "Text"
);
static $has_many = array(
"GeraetePics" => "GeraetePic",
"GeraeteDocuments" => "GeraeteDocument"
);
static $many_many = array(
"Kategorien" => "Kategorie"
);
....
public function getCustomSearchContext() {
$fields = $this->scaffoldSearchFields(array(
'restrictFields' => array(
'Miete',
'Kauf',
'Occasion',
'Title',
'Hersteller',
'Kategorien.Name' => array(
"field" => "TextField",
"filter" => "ExactMatchFilter",
"title" => "Kategorie"
)
)
));
$filters = array(
'Miete' => new ExactMatchFilter('Miete'),
'Kauf' => new ExactMatchFilter('Kauf'),
'Occasion' => new ExactMatchFilter('Occasion'),
'Title' => new PartialMatchFilter('Title'),
'Hersteller' => new PartialMatchFilter('Hersteller'),
'Kategorien.Name' => new ExactMatchFilter('Kategorien.Name')
);
return new SearchContext(
$this->class,
$fields,
$filters
);
}
...
}
class Kategorie extends DataObject {
static $db = array(
"Name" => "Varchar(255)"
);
static $belongs_many_many = array(
"Geraete" => "Geraet"
);
...
}
class GeraetListingPage_Controller extends DataObjectAsPageHolder_Controller {
//This needs to know be the Class of the DataObject you want this page to list
static $item_class = 'Geraet';
//Set the sort for the items (defaults to Created DESC)
static $item_sort = 'Title ASC';
static $allowed_actions = array(
'GSearchForm','doSearch'
);
public function GSearchForm() {
$context = singleton('Geraet')->getCustomSearchContext();
$fields = $context->getSearchFields();
$form = new Form($this, "GSearchForm",
$fields,
new FieldSet(
new FormAction('doSearch', 'Geraet suchen')
)
);
$form->setFormMethod('GET');
$form->disableSecurityToken();
return $form;
}
public function doSearch($data, $form) {
$context = singleton('Geraet')->getCustomSearchContext();
$results = $this->getResults($data);
return $this->customise(array(
'Results' => $results
))->renderWith(array('GeraetListingPage_results', 'Page'));
}
function getResults($searchCriteria = array()) {
$start = ($this->request->getVar('start')) ? (int)$this->request->getVar('start') : 0;
$limit = 10;
$context = singleton('Geraet')->getCustomSearchContext();
$records = $context->getResults($searchCriteria, null, array('start'=>$start,'limit'=>$limit));
if($records) {
$records->setPageLimits($start, $limit, $query->unlimitedRowCount());
}
return $records;
}
}