Hey,
Need some help here, i have an issue with a search.
I need the code below only to return results where StartDate > CURDATE(), but i have no idea what i need to do.
Thanks in advance :o)
From my Page_Controller
public function Find() {
$context = singleton('Course')->getCustomSearchContext();
$actions = new FieldSet(new FormAction('FindKursus', 'Kursus søgning'));
$form = new Form(
$this, 'Find', $context->getSearchFields(), $actions
);
$form->setFormMethod('GET');
$form->disableSecurityToken();
return $form;
}
public function FindKursus($data, $form) {
$context = singleton('Course')->getDefaultSearchContext();
$results = $this->getResults($data);
return $this->customise(array(
'Title' => 'Kurser der matcher din søgning',
'Results' => $results,
'Find' => $form
))->renderWith(array('Kursus_results', 'Page'));
}
function getResults($searchCriteria = array()) {
$start = ($this->request->getVar('start')) ? (int)$this->request->getVar('start') : 0;
$limit = 10;
//$filter = "StartDate > CURDATE()",
//$sort = "StartDate ASC",
$context = singleton('Course')->getCustomSearchContext();
$query = $context->getQuery($searchCriteria, array(), array('start'=>$start,'limit'=>$limit));
$records = $context->getResults($searchCriteria, "StartDate ASC", array('start'=>$start,'limit'=>$limit));
if($records) {
$records->setPageLimits($start, $limit, $query->unlimitedRowCount());
}
return $records;
}
The relevant code from the Course.php file
static $searchable_fields = array(
'Title' => array(
'field' => 'TextField',
'filter' => 'PartialMatchFilter',
'title' => 'Kursus navn'
),
'City' => array(
'field' => 'TextField',
'filter' => 'PartialMatchFilter',
'title' => 'By'
),
'Location' => array(
'field' => 'TextField',
'filter' => 'PartialMatchFilter',
'title' => 'Sted'
),
'StartDate' => array(
'field' => 'TextField',
'filter' => 'PartialMatchFilter',
'title' => 'Start dato'
),
'EndDate' => array(
'field' => 'TextField',
'filter' => 'PartialMatchFilter',
'title' => 'Slut dato'
)
);
public function getCustomSearchContext() {
$fields = $this->scaffoldSearchFields(array(
'restrictFields' => array(
//'Title',
'City'
//'Location',
//'StartDate',
//'EndDate'
),
'FieldClasses' => array(
//'ExpiryDate' => 'HiddenField'
)
));
$filters = array(
'Title' => new PartialMatchFilter('Title'),
'City' => new PartialMatchFilter('City'),
'Location' => new PartialMatchFilter('Location'),
'StartDate' => new GreaterThanFilter('StartDate'),
'EndDate' => new LessThanFilter('EndDate'),
);
// Create a dropdown of all available cities
$cities = DB::query('SELECT DISTINCT "City" FROM "Course" ORDER BY "CITY" ASC')->column();
$newCities = array_combine($cities, $cities);
$CityDropDownField = new DropdownField('City', 'By', $newCities);
$CityDropDownField->sethasEmptyDefault(true);
$fields->replaceField("City", $CityDropDownField);
// Create a dropdown of all available locations
$locations = DB::query('SELECT DISTINCT "Location" FROM "Course"')->column();
$newLocations = array_combine($locations, $locations);
$LocationsDropDownField = new DropdownField('Location', 'Sted', $newLocations);
$LocationsDropDownField->sethasEmptyDefault(true);
$fields->replaceField("Location", $LocationsDropDownField);
return new SearchContext(
$this->class,
$fields,
$filters
);
}