2013-03-18 66 views
0

當我運行在where子句中,以下錯誤顯示與日期查詢dateformate ...查詢在其中Symfony2的條款

[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(); 

回答

0

學說沒有按給定沒有DATE_FORMAT函數默認定義。有可能Register Custom DQL Function

但是你可以很容易地比較不同的日期(假設a.paymentDatedate類型):

$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(); 
+0

感謝您的回答。但在我的數據庫中,a.pymentDate格式是日期(Y-m-d h:m:s)。它如何與DateTime('2013-03-11')格式進行比較 – Tushar 2013-03-19 00:20:16

+0

Doctrine在執行DQL查詢時將'\ DateTime'轉換爲數據庫日期類型。 – 2013-03-19 16:44:10

+0

在表中有一個條目paymentDate是'2013-03-11 16:37:01'。 wehen - > setParameter('paymentDate',new \ DateTime('2013-03-11 16:37:01'))它的工作正常,但 - > setParameter('paymentDate',new \ DateTime('2013-03-11' ))..查詢沒有得到任何記錄 – Tushar 2013-03-19 17:29:02