Trying to figure out how to build a query in CakePHP where I can select all Events that are between X and Y dates (user-entered dates).
The problem lies in that the
You could try the following, assuming Cake 1.3 and using containable behaviour. I assumed that the date fields in your table are called start_date and end_date, so these conditions might need adjusting.
This may, or may not work, and will likely produce a massive query if you have a lot of data, so some further refinement is probably necessary - certainly with the fields selected.
You could post the query generated, if it runs, and we may be able to help adjust it further.
/* in your EventController (the method) for on the fly */
$this->Event->Behaviors->attach('Containable');
/* Your dates */
$x_date = '2011-06-01';
$y_date = '2011-07-01';
$this->paginate = array(
'limit'=>10,
'order'=>'Event.created DESC',
'contain'=>array(
'Schedule',
'Event'=>array(
'conditions'=>array('Event.start_date'=>$x_date,
'Event.end_date'<=$y_date)
)
),
);
$this->set('events',$this->Paginate('Event'));
// print_r($events);