2012-06-14 44 views
5

我試圖從表中選擇所選持續時間除以輸入持續時間等於的餘數。如何在doctrine2查詢生成器中添加運算符,其中語句

$qb = $em->createQueryBuilder() 
->from('AcmeBlogBundle:Entry', 'e') 
->andWhere(":duration % e.duration = 0") 
->setParameter('duration', $duration); 

這將返回錯誤:

[Syntax Error] line 0, col 226: Error: Expected =, <, <=, <>, >, >=, !=, got '%' 

這在普通的SQL工作。有人知道如何用Doctrine的查詢構建器做到這一點?

+0

正確答案? – sensorario

回答

9

符號%不是DQL運算符。試試這個:

$qb = $em->createQueryBuilder() 
->from('AcmeBlogBundle:Entry', 'e') 
->andWhere("mod(:duration,e.duration) = 0") 
->setParameter('duration', $duration); 

或者閱讀:http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/dql-doctrine-query-language.html 12.5.1款。

MOD(a, b) - Return a MOD b. 
+0

感謝您的建議,但mod在查詢中不起作用。我正在查看文檔,它沒有列出模數,甚至沒有在where語句中指明任何計算。我將不得不繼續尋找,但好主意。 – Apot

+1

你是一個天才。 MOS(a,b)像魅力一樣工作。非常感謝你! – Apot

+0

如果它是正確的,你能簽署這個答案嗎? – sensorario

相關問題