CakePHP find condition for a query between two dates

China☆狼群 提交于 2019-12-18 12:27:11

问题


I have a start and an end date in my database and a $date variable from a form field. I am now trying to query all the rows where $date is either = start/end date in the db, or ANY date between those two.

It's kind of the opposite of what is described in the docs of how daysAsSql works. I can't figure out how to get it to work. The following line does not work as a find condition in the controller:

'? BETWEEN ? AND ?' => array($date, 'Item.date_start', 'Item.date_end'),

Any help is greatly appreciated. This is driving me crazy.

Here is the complete Query and corresponding SQL:

$conditions = array(
            'conditions' => array(
            'and' => array(
                '? BETWEEN ? AND ?' => array($date, 'Item.date_start', 'Item.date_end'),
                'Item.title LIKE' => "%$title%",
                'Item.status_id =' => '1'
                )));

        $this->set('items', $this->Item->find('all', $conditions));



WHERE (('2012-10-06' BETWEEN 'Item.date_start' AND 'Item.date_end') AND (`Item`.`title` LIKE '%%') AND (`Item`.`status_id` = 1))

回答1:


$conditions = array(
        'conditions' => array(
        'and' => array(
                        array('Item.date_start <= ' => $date,
                              'Item.date_end >= ' => $date
                             ),
            'Item.title LIKE' => "%$title%",
            'Item.status_id =' => '1'
            )));

Try the above code and ask if it not worked for you.

Edit: As per @Aryan request, if we have to find users registered between 1 month:

$start_date = '2013-05-26'; //should be in YYYY-MM-DD format
$this->User->find('all', array('conditions' => array('User.reg_date BETWEEN '.$start_date.' AND DATE_ADD('.$start_date.', INTERVAL 30 DAY)')));



回答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:


General Example For CakePHP 2.x Query

        $_condition = array("TABLENAME.id" => $id, "TABLENAME.user_id" => array_unique($_array), 'date(TABLENAME.created_at) BETWEEN ? AND ?' => array($start_date, $end_date));
        $result_array = $this->TABLENAME->find("all", array(
            'fields' => array("TABLENAME.id", "TABLENAME.user_id", 'TABLENAME.created_at'),
            "conditions" => $_condition,
            "group" => array("TABLENAME.id"), //fields to GROUP BY
            'joins' => array(
                array(
                    'alias' => 'T2',
                    'table' => 'TABLENAME2',
                    'type' => 'LEFT',
                    'conditions' => array('TABLENAME.t_id = TABLENAME2.t_id')
                ),
                array(
                    'alias' => 'T3',
                    'table' => 'TABLENAME3',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'IF(
                           TABLENAME.t3_id > 0,
                                T2.f_id = T3.f_id,
                                TABLENAME.ff_id = T2.ff_id
                            )'
                    )
                ),
            ),
            'recursive' => 0
                )
        );



回答4:


This is more efficient and understandable IN BETWEEN query in cakephp 2.x

$testing_log_device_site_name = $testingLogData['TestingLogDevice']['Siteid'];

$conditions = array('TestingLogDevice.dateee BETWEEN ? and ?' => array($start_date, $end_date));

$results = $this->TestingLogDevice->find('all', 
    array(
         'fields'=>array('dateee','timeee','Siteid'), 
         'conditions'=>array($conditions, 'TestingLogDevice.Siteid'=>$testing_log_device_site_name)
    )
);
pr($results);



回答5:


$data=$this->post->find('all')->where([ 'id'=>$id,
'price between'=>$price1,'and'=>$price2])->toArray();

This query works as following:

select * from post where id=$id and price between $price1 and $price2;

" -- 'price between'=>$price1 --" become "price between $price1"




回答6:


Use this

       $today = new DateTime( date('Y-m-d'));

       $fiveYearsBack = $today->sub(new DateInterval('P5Y'));


来源:https://stackoverflow.com/questions/11931633/cakephp-find-condition-for-a-query-between-two-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!