如何使用createQueryBuilder在Doctrine和Symfony2中使用DATE_ADD比较范围内的日期


How to use DATE_ADD to compare date in range in Doctrine and Symfony2 using createQueryBuilder

我有两个相互关联的表(main_table OneToMay detail_table)。在主表中有一个deadline_days字段,在detail_table中有一条create_date字段。我想选择基于今天日期传递的create_date+main.deadline_days的所有细节。(这就是场景)

这是正确的MySQL查询,它给了我正确的记录

SELECT  `D`.* FROM `details_table` AS  `D` 
INNER JOIN  `main_table` AS  `M` ON (`D`.`Main_Id` = `M`.`id`) 
WHERE DATE_ADD(`D`.`Create_Date`, INTERVAL  `M`.`Deadline_days` DAY) <= NOW()

现在在Symfony中,当我想使用createQueryBuilder创建查询时,会出现以下错误

[Syntax Error] line 0, col 165: Error: Expected Doctrine'ORM'Query'Lexer::T_COMMA, got 'M'

这就是我现在在我的查询生成器中所拥有的

$result = $this->createQueryBuilder('D')
->join('D.Main', 'M')
->where('DATE_ADD(D.Create_Date, INTERVAL M.DeadLine_Days DAY) <= NOW()')
->getQuery()
->getResult();

知道怎么解决这个问题吗?提前谢谢。

请不要建议使用本机查询

这是我在这个链接上发现的(条令更新DQL函数签名)
Doctrine2有函数DATE_ADD,但没有MySQL的INTERVAL参数,单位参数也应该是字符串'DAY';因此查询应该是:

$result = $this->createQueryBuilder('D')
   ->join('D.Main', 'M')
   ->where('DATE_ADD(D.Create_Date, M.DeadLine_Days, 'DAY') <= CURRENT_DATE()')
   ->getQuery()
   ->getResult();

在条令中,我们应该使用CURRENT_DATE()而不是NOW()