CakePHP Search between 2 Date Records

后端 未结 6 1912
再見小時候
再見小時候 2020-12-11 10:20

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 o

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-11 10:37

    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.

提交回复
热议问题