Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakephp: how to make NOW() work in search condition?

Tags:

mysql

cakephp

I'm trying to get all records with date greater then now. I tried this but it doeasnt work:

$all_dates = $this->TourDate->find('all', array('conditions' => array('TourDate.date >=' => 'NOW()'), 'order' => array('TourDate.date ASC')));

If I replace NOW() with the current date it works. Why's that?

like image 677
kaklon Avatar asked Apr 20 '10 21:04

kaklon


3 Answers

I do not use CakePHP but I am pretty sure that your 'NOW()' is parsing to string and finally you got something like

TourDate.date >= 'NOW()'

Maybe you should just try

array('TourDate.date >= NOW()')

as a value only instead of spliting it to key => value style ?

like image 151
hsz Avatar answered Nov 01 '22 06:11

hsz


If you are looking for a way to do it with PHP-function istead of MySQL-functions you don't have to use the verbous format $conditions = array('begin >' => date('Y-m-d H:i:s')), MySQL understands the shorter 'c' preset of PHPs date() for ISO 8601 dates with $conditions = array('begin >' => date('c')).

I like this for doing queries like 'begin >' => date('c', strtotime("+4 weeks")), which is very readable code and great to debug when you look at the SQL-Queries.

like image 20
Michael Probst Avatar answered Nov 01 '22 07:11

Michael Probst


'conditions' => array('TourDate.date >= NOW()')

Otherwise, cakephp will quote the NOW() function, and mysql will think of it as a string.

like image 2
AntonioHS Avatar answered Nov 01 '22 06:11

AntonioHS