0

当我在 where 子句中运行带有日期的查询时,会显示以下错误...

[Syntax Error] line 0, col 129: Error: Expected known function, got 'DATE_FORMAT'

查询如下

$query = $this->getEntityManager()->createQuery(
  "SELECT a.id, a.amont, a.paymentDescrip, a.paymentType, a.paymentDate
   FROM RegalSmsBundle:DailyTransaction a 
   WHERE DATE_FORMAT(a.paymentDate,'%Y-%m-%d') = :paymentDate
        and a.students = :studentId" 

    )->setParameter('studentId', $studentId)
    ->setParameter('paymentDate','2013-03-11');


 return $query->getResult();
4

1 回答 1

0

Doctrine 默认没有DATE_FORMAT定义函数。可以注册自定义 DQL 函数

但是您可以轻松比较日期(假设a.paymentDate是 type date):

$query = $this->getEntityManager()->createQuery("
        SELECT a.id, a.amont, a.paymentDescrip, a.paymentType, a.paymentDate
        FROM RegalSmsBundle:DailyTransaction a 
        WHERE a.paymentDate = :paymentDate AND a.students = :studentId
    ")
    ->setParameter('studentId', $studentId)
    ->setParameter('paymentDate', new \DateTime('2013-03-11'))
;

return $query->getResult();

编辑:我更喜欢使用 querybuider 来编写 DQL。它看起来像这样:

$qb = $this->getEntityManager()->getRepository('RegalSmsBundle:DailyTransaction')->createQueryBuilder('a');
$qb
    ->select('a') // select whole entity
    ->where($qb->expr()->andX(
        $qb->expr()->eq('a.paymentDate', ':paymentDate')
        $qb->expr()->eq('a.students', ':studentId')
    ))
    ->setParameter('studentId', $studentId)
    ->setParameter('paymentDate', new \DateTime('2013-03-11'))
;

return $qb->getQuery()->getResult();
于 2013-03-18T17:54:43.153 回答