Doctrine QueryBuilder 中的表达式 mysql NOW()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13905788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:47:42  来源:igfitidea点击:

Expression mysql NOW() in Doctrine QueryBuilder

mysqldoctrinedoctrine-ormdoctrine-query

提问by user1906245

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

如何在学说查询生成器中使用表达式 mysql NOW()?

回答by Mats Rietdijk

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

在 Doctrine2 中,您必须使用以下之一而不是NOW().
这个:

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()

如果您只需要时间或日期,请使用其中之一: CURRENT_TIME()CURRENT_DATE()

Documentation can be found here.

文档可以在这里找到

回答by Aistis

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 requiredto get CURRENT_TIMESTAMP()function working.

注意:参数集需要额外的引号才能使CURRENT_TIMESTAMP()函数正常工作。

Or simply

或者干脆

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