Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression mysql NOW() in Doctrine QueryBuilder

How to use the expression mysql NOW() in doctrine querybuilder?

like image 523
Ricardo Viana Avatar asked Dec 16 '12 21:12

Ricardo Viana


2 Answers

In Doctrine2 you have to use one of the following instead of NOW().
This:

CURRENT_TIMESTAMP() 

Or:

... createQuery(...'WHERE x.date = :now') ->setParameter('now', new \DateTime('now')) ... 

If you want only time or only date use one of those: CURRENT_TIME() and CURRENT_DATE()

Documentation can be found here.

like image 85
Mats Rietdijk Avatar answered Sep 21 '22 01:09

Mats Rietdijk


Using query builder it would look like this:

$qb     ->select('B')     ->from('RandomBundle:Banana', 'B')     ->where(         $qb->expr()->gt('B.expiresAt', ':now')     )     ->setParameter('now', '\'CURRENT_TIMESTAMP()\''); 

Note: extra quotes on parameter set is required to get CURRENT_TIMESTAMP() function working.

Or simply

$qb     ->select('B')     ->from('RandomBundle:Banana', 'B')     ->where(         $qb->expr()->gt('B.expiresAt', 'CURRENT_TIMESTAMP()')     ); 
like image 35
Aistis Avatar answered Sep 21 '22 01:09

Aistis