Compare DateTime in CakePHP

I have a request in CakePHP that has a saved "datetime" field called DropIn.drop_in_time . I would only like to β€œfind” the entries where DropIn.drop_in_time is > NOW() , but I had trouble getting it.

Condition DropIn.drop_in_time >' => 'NOW() did not get the correct results in the query below. Is there a better way to do this?

 $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'))); 
+4
source share
3 answers

If you separate the value as 'DropIn.drop_in_time' => 'NOW()' , 'NOW()' will mean a literal string of values "NOW()" . Just write it as one piece of SQL: 'DropIn.drop_in_time > NOW()' . Alternatively, use 'DropIn.drop_in_time >' => date('Ymd H:i:s') .

+9
source

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

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

However, it would be like losing the point of database abstraction that the infrastructure provides, so do as suggested by deceze and compare it to date('Ymd H:i:s')

0
source

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

0
source

Source: https://habr.com/ru/post/1341721/


All Articles