2012-04-06 49 views
6

我被困在這裏,我花了最後2天解決這個問題,但失敗了。我正在編寫一個查詢在我的存儲庫中獲取當前月份的條目。這裏是我的查詢: -INTERVAL 1個月不符合symfony2原則?

$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,但沒有幫助我。

回答

24

你應該使用參數綁定:

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

非常感謝jkucharovic。它非常有用。 u能普萊舍告訴我,我該怎麼做同樣的間隔1年 – ScoRpion 2012-04-06 12:23:36

+0

'$查詢 - >的setParameter(「日期」,新\日期時間(' - 1年));' – jkucharovic 2012-04-07 09:18:43

+0

和u能告訴我怎麼能現在使用這裏 – ScoRpion 2012-04-07 15:49:33