开发者

comparing a DateTime in CakePHP

开发者 https://www.devze.com 2023-02-13 20:24 出处:网络
I have a query in CakePHPthat has a stored \"datetime\" field called DropIn.drop_in_time.I would like to only \"find\" entries where the DropIn.drop_in_time is > NOW() but am having trouble getting

I have a query in CakePHPthat has a stored "datetime" field called DropIn.drop_in_time. I would like to only "find" entries where the DropIn.drop_in_time is > NOW() but am having trouble getting it to do that.

The condition DropIn.drop_in_time >' => 'NOW() didn't get the right results in the query below. Is there a better way to do it?

$requests = $this->DropIn->find('all', array(
            'conditions' => array('DropIn.开发者_运维技巧drop_in_time >' => 'NOW()', 'or' => array(array('DropIn.user_id' => $this->Auth->user('id')), array('DropIn.id' => $drop_in_ids))),
            'order'=>array('DropIn.created'=>'DESC')));


If you separate the value as 'DropIn.drop_in_time' => 'NOW()', 'NOW()' is taken to mean the literal value string "NOW()". Just write it as one SQL fragment instead: 'DropIn.drop_in_time > NOW()'. Alternatively, use 'DropIn.drop_in_time >' => date('Y-m-d H:i:s').


If you really want to put DB expressions into CakePHP finds, you can use the expression method:

'DropIn.drop_in_time.' => $db->expression('CURDATE()'); 

However this is kinda of losing the point of the database abstraction that a framework provides, so do as suggested by deceze and compare it with date('Y-m-d H:i:s')


use DboSource::expression('NOW') instead of only NOW()

0

精彩评论

暂无评论...
验证码 换一张
取 消