I trying to search between dates using the CakeDc
plugin. I am trying to use 2 separate form fields in the form/view, 1 for start date and the other for end date.
I need to have a single array so I can use the function CreationDateRangeCondition
in the controller (unless this can be sorted somehow in the controller function) but I don't know how to do this. I am trying to learn PHP
and Cakephp
Controller:
public $presetVars = array(
array('creationDateBetween' => 'created', 'type' => 'value'),
);
public function index() {
$this->Prg->commonProcess();
$this->paginate = array(
'conditions' => $this->Post->parseCriteria($this->passedArgs));
$this->set('posts', $this->paginate());
}
Model: public $actsAs = array('Search.Searchable');
public $filterArgs = array(
'creationDateBetween' => array(
'type' => 'expression',
'method' => 'CreationDateRangeCondition',
'field' => 'Post.created BETWEEN ? AND ?',
),
);
public function CreationDateRangeCondition($data = array()){
if(strpos($data['creationDateBetween'], ' - ') !== false){
$tmp = explode(' - ', $data['creationDateBetween']);
$tmp[0] = $tmp[0]."-01-01";
$tmp[1] = $tmp[1]."-12-31";
return $tmp;
}else{
return array($data['creationDateBetween']."-01-01", $data['creationDateBetween']."-12-31");
}
}
View:
<?php
echo $this->Form->create('Post', array(
'url' => array_merge(array('action' => 'index'), $this->params['pass'])
));
echo $this->Form->input('creationDateBetween', array(
'type' => 'date',
'div' => false,
'dateFormat' => 'DMY',
'minYear' => 2013,
'maxYear' => date('Y'),
'style' => 'margin-right: 2px; margin-left: 2px',
'empty' => true,
'timeFormat' => null,
'selected' => array(
'day' => 1,
'month' => 1,
'year' => 2013
),
'empty' => false
));
echo $this->Form->input('creationDateBetween2', array(
'type' => 'date',
'div' => false,
'dateFormat' => 'DMY',
'minYear' => 2013,
'maxYear' => date('Y'),
'empty' => true,
'style' => 'margin-right: 2px; margin-left: 2px',
'timeFormat' => null,
'selected' => array(
'day' => date('D'),
'month' => date('M'),
'year' => date('Y')
),
'empty' => false
));
?>
<?php
echo $this->Form->submit(__('Search', true), array('div' => false));
echo $this->Form->end();
?>