Cakephp Search Plugin: DateTime Range

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2010-12-24T09:46:30Z Indexed on 2010/12/24 9:54 UTC
Read the original article Hit count: 272

Filed under:

I am trying to search a model by dates using the CakePHP Search plugin

The idea is: The user enters a date of a flight and the engine returns all flights within +- one day of that.

My form:

<?php
echo $form->create('Flight', array(
   'url' => array_merge(array('action' => 'find'), $this->params['pass'])
));
echo $form->input('departure', array('div' => false,  'dateFormat' => 'DMY','timeFormat' => 'NONE'));    
echo $form->submit(__('Search', true), array('div' => false));
echo $form->end();
?>

My flight model:

public $filterArgs = array(
  array('name' => 'departure', 'type' => 'expression', 'method' => 'makeRangeCondition', 'field' => 'ABS((TO_Days(Flight.departure))-( TO_Days(?))) < 2')
);

The controller:

public $presetVars = array(
  array('field' => 'departure', 'type' => 'expression')

);

public function find() {
  $this->Prg->commonProcess();
  $this->paginate['conditions'] = $this->Flight->parseCriteria($this->passedArgs);
  $this->set('flights', $this->paginate());
}

When I try to search, the values for departure are packed in an array;

Array ([departure] => Array )

And the filterArgs(...) function doesn't seem to understand this. What is the correct way to do this?

© Stack Overflow or respective owner

Related posts about cakephp