php Doctrine querybuilder DATE_FORMAT 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26298175/
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
Doctrine querybuilder DATE_FORMAT not working
提问by Pedro Luz
I'm having some problems with DATE_FORMAT inside a createQueryBuilder
我在 createQueryBuilder 中遇到了 DATE_FORMAT 问题
My code:
我的代码:
$qb7Days = $repo->createQueryBuilder('R')
->select( 'R.createdAt' )
->where( "DATE_FORMAT(R.createdAt, '%Y-%m-%d') = :afterDays" )
->andWhere( 'R.cCurrentReviewState = :state' )
->andWhere( 'R.reminder = :reminder' )
->setParameter( 'afterDays', $after7Days )
->setParameter( 'state', $oReviewStateNotVerified ) // not_verified
->setParameter( 'reminder', 0 ) // never sent any reminder
->orderBy( 'R.id', 'ASC' )
->getQuery();
But im getting
但我得到
[Doctrine\ORM\Query\QueryException]
[Syntax Error] line 0, col 7: Error: Expected known function, got 'DATE_FORMAT'
I've searched some links and find some explain that it should work this way, but for me it looks like im doing something wrong.
我搜索了一些链接并找到了一些解释它应该以这种方式工作的解释,但对我来说似乎我做错了什么。
http://www.uvd.co.uk/blog/labs/using-mysqls-date_format-in-doctrine-2-0/
http://www.uvd.co.uk/blog/labs/using-mysqls-date_format-in-doctrine-2-0/
回答by qooplmao
DATE_FORMAT
along with a bunch of Mysql function are not directly available in Doctrine.
DATE_FORMAT
连同一堆 Mysql 函数在 Doctrine 中不能直接使用。
To use them you can either create your own or add an extension like beberlei/DoctrineExtensions
and then add the respective function to your doctrine bundle config like
要使用它们,您可以创建自己的或添加扩展名beberlei/DoctrineExtensions
,然后将相应的功能添加到您的学说包配置中,例如
doctrine:
dbal:
....
orm:
....
dql:
string_functions:
DATE_FORMAT: DoctrineExtensions\Query\Mysql\DateFormat
回答by Pedro Luz
DATE_FORMAT is not a doctrine recognised function. That link that you pasted is custom code the author wrote for him to get date_formate. You can write custom functions in doctrine and register them , neat ey ?
DATE_FORMAT 不是学说认可的函数。您粘贴的链接是作者为他编写的用于获取 date_format 的自定义代码。您可以在学说中编写自定义函数并注册它们,好吗?
Here is an example of how to do that - http://symfony.com/doc/current/cookbook/doctrine/custom_dql_functions.html
这是一个如何做到这一点的例子 - http://symfony.com/doc/current/cookbook/doctrine/custom_dql_functions.html
Option 1: You write that custom function for DATE_FORMAT so that you can use it again and again. Option 2: Maybe reformat the date in PHP to match the date format in the database , kinda like
选项 1:您为 DATE_FORMAT 编写该自定义函数,以便您可以一次又一次地使用它。选项 2:也许重新格式化 PHP 中的日期以匹配数据库中的日期格式,有点像
$after7Days->format('Y-m-d');
$after7Days->format('Ym-d');
回答by Lauris Kuznecovs
DATE_FORMAT is not string function, its date_function:
DATE_FORMAT 不是字符串函数,它的 date_function:
config.yml
配置文件
doctrine:
orm:
dql:
datetime_functions:
date_format: DoctrineExtensions\Query\Mysql\DateFormat
回答by tom10271
I am not sure but this is correct for me only:
我不确定,但这仅对我来说是正确的:
doctrine:
orm:
entity_managers:
default:
...
dql:
datetime_functions:
DATE_FORMAT: DoctrineExtensions\Query\Mysql\DateFormat