CakePHP Search between 2 Date Records

2019-01-20 01:14发布

问题:

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));

回答1:

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
     ),


回答2:

Here is CakePHP BETWEEN query example.

I'm defining my arrays as variables, and then using those variables in my CakePHP find function call:

// just return these two fields
$fields = array('uri', 'page_views');

// use this "between" range
$conditions = array('Event.date BETWEEN ? and ?' => array($start_date, $end_date));

// run the "select between" query
$results = $this->Event->find('all', 
         array('fields'=>$fields, 
               'conditions'=>$conditions));

Ref from



回答3:

$start = date('Y-m-d');
$end = date('Y-m-d', strtotime('+1 month'));

$conditions = array('Event.start <=' => $end, 'Event.end >=' => $start);

$this->Event->find('all', array('conditions' => $conditions));


回答4:

If you are on cakephp 2.0 this is the answer http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::daysAsSql

If you are on cakephp 1.3 you can use TimeHelper, just import it and use the same function as per example in documentation here http://book.cakephp.org/1.3/en/view/1471/Formatting



回答5:

In case if you have one date and you want to use BETWEEN

$date_start = start date;
$date_end = date_end;

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

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

This is the case if you have From AND To

in your case

$date_start = start date;
$date_end = date_end;

$conditions = array(
    'conditions' => array(
        'Equipment.start_date >=' => array($date_start), 
        'Equipment.end_date <=' => array($date_end)
    ));

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


回答6:

You cannot use database columns with this BETWEEN-syntax. If you assign strings in the array, CakePHP will quote them. Same for numeric values depending on your database setup.

CakePHP will quote the numeric values depending on the field type in your DB.
– see http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions

If you still want to use the BETWEEN, you can write your queries into the array key because CakePHP will not escape the keys, but only the values.

CakePHP only escapes the array values. You should never put user data into the keys. Doing so will make you vulnerable to SQL injections.
– see http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions

Concerning your problem:

$this->Model->find('all', array(
    'conditions' => array(
        '"YYYY-MM-DD" BETWEEN Model.date_start AND Model.date_end',
    ),
));

You can even work with MySQL date and time functions:

$this->Model->find('all', array(
    'conditions' => array(
        'CURDATE() BETWEEN Model.date_start AND Model.date_end',
    ),
));

If you use date/time functions, do not quote them. If you use a specific date put it in quotes.