Expression mysql NOW() in Doctrine QueryBuilder

2019-04-03 08:59发布

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

2条回答
姐就是有狂的资本
2楼-- · 2019-04-03 09:19

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()')
    );
查看更多
爷、活的狠高调
3楼-- · 2019-04-03 09:23

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.

查看更多
登录 后发表回答