【问题标题】:INTERVAL 1 MONTH not working With symfony2 doctrine?INTERVAL 1 MONTH 不使用 symfony2 原则?
【发布时间】:2015-10-26 11:23:41
【问题描述】:

我被困在这里,我花了最后 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)')

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

[Syntax Error] line 0, col 133: Error: Expected Doctrine\ORM\Query\Lexer::T_COMMA, got '1'

即使我尝试了this thing,但没有帮助我。

【问题讨论】:

    标签: symfony doctrine doctrine-orm


    【解决方案1】:

    你应该使用参数绑定:

    $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'));
    

    【讨论】:

    • 非常感谢 jkucharovic。它非常有用。你能告诉我如何用 INTERVAL 1 YEAR 做同样的事情吗
    • $query->setParameter('date', new \DateTime('-1 year'));
    • 你能告诉我现在怎么用吗
    • :- 你能告诉我这两个......现在()和今天
    • 现在很简单——$query->setParameter('date', new \DateTime('now'));。但它不可用,因为它的意思是“此时此刻”。
    【解决方案2】:

    您必须记住,DQL 不是 SQL。该错误来自 Doctrine 的 Lexer,而不是来自 MySQL。 DQL 不支持 INTERVAL(请参阅 list of supported functions)。

    adding your own functions 上阅读更多信息,特别是在此处添加带有 INTERVAL 支持的 DATE_ADD:http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/cookbook/dql-user-defined-functions.html#date-add

    【讨论】:

      猜你喜欢
      • 2015-01-04
      • 2019-09-15
      • 2021-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-12
      • 2012-04-16
      相关资源
      最近更新 更多