CakePHP Search between 2 Date Records

Joshua picture Joshua · Aug 10, 2012 · Viewed 29k times · Source

I am building a small Web App that lets users reserve Office Rooms and Equipment. For the Reservation they enter a Start and an End Date.

When a user tries to find out if any (for example) car is available on 2012-10-23, and the database holds reservation date records of Start: 2012-10-20 and End: 2012-10-25 for (lets say) all the cars, how do I include all the dates between my date entries in the search?

The $date variable gets it's value from the Date Search Form Field.

This, unfortunately does not work, and I can't figure out how to use daysAsSql for this query:

$conditions = array(
    'conditions' => array(
        '? BETWEEN ? AND ?' => array($date,'Equipment.date_start','Equipment.date_end'), 
    )));

$this->set('equipments', $this->Equipment->find('all', $conditions));

Answer

Joshua picture Joshua · Aug 13, 2012

There is a simpler solution to this, but thanks for the help:

(As a condition in the find:)

array('Equipment.date_start <= ' => $date,
      'Equipment.date_end >= ' => $date
     ),