我需要上个月创建的表中的所有数据。我创建了以下代码,但它在DQL中给出了以下错误,但运行时SQL查询是正确的。
错误:
[Syntax Error] line 0, col 138: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got '-'
代码是;
$compCases = $this->getDoctrine()
->getRepository('EFuturesCrmBundle:CasesCompensation')
->createQueryBuilder('c')
->select('c.id')
->where('c.caseStatus =:status')
->andWhere('YEAR(c.caseStatusDate) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)')
->andWhere('MONTH(c.caseStatusDate) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)')
->setParameter('status', 'resolve')
->getQuery()
->getResult();
DQL不支持INTERVAL
所以如何在Doctrine2中实现这一点?
答案 0 :(得分:3)
尝试使用这种方法:
$now = new \DateTime('now');
$delay = new \Datetime('last month');
$compCases = $this->getDoctrine()
->getRepository('EFuturesCrmBundle:CasesCompensation')
->createQueryBuilder('c')
->select('c.id')
->where('c.caseStatus = :status')
->andWhere('c.caseStatusDate <= :now')
->andWhere('c.caseStatusDate >= :delay')
->setParameter('status', 'resolve')
->setParameter('now', $now)
->setParameter('delay', $delay)
->getQuery()
->getResult();
希望它可以帮到你。