INTERVAL 1个月没有使用symfony2学说?

Sco*_*ion 8 doctrine symfony doctrine-orm

我被困在这里,我花了最后2天解决这个问题,但失败了.我正在我的存储库中编写一个查询来获取当月的条目.这是我的查询: -

$this->getEntityManager()
 ->createQuery('SELECT count(a) FROM CollegeStudentBundle:StudentAttendance a where a.student_id='.$id.'
 and a.date > DATE_SUB(CURRENT_TIMESTAMP(),INTERVAL 1 MONTH)')
Run Code Online (Sandbox Code Playgroud)

当我尝试运行它时,它给了我一个错误

[Syntax Error] line 0, col 133: Error: Expected Doctrine\ORM\Query\Lexer::T_COMMA, got '1'
Run Code Online (Sandbox Code Playgroud)

即使我尝试过这件事,但没有帮助我.

jku*_*vic 28

你应该使用参数绑定:

$query = $em->createQuery('SELECT count(a) FROM CollegeStudentBundle:StudentAttendance a where a.student_id = :id and a.date > :date');
$query->setParameter('id', $id);
$query->setParameter('date', new \DateTime('-1 month'));
Run Code Online (Sandbox Code Playgroud)


Jak*_*las 9

你必须记住DQL不是SQL.错误来自Doctrine的Lexer而不是来自MySQL.DQL不支持INTERVAL(请参阅支持的函数列表).

阅读有关添加自己的函数的更多信息,特别是在此处添加带有INTERVAL支持的DATE_ADD:http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/cookbook/dql-user-defined-functions.html#日期相加