我想統計從DQL查詢返回的行數,我該怎麼做? 這裏是我的DQL查詢:如何計算DQL查詢返回的行數?
$items=$em->createQuery("select i.unitPrice,i.quantity,i.linetotal,i.description from InvoicesInvoicesBundle:Invoiceshasitems i where i.invoiceid='".$id."'");
$itemdata=$items->getResult();
我想統計從DQL查詢返回的行數,我該怎麼做? 這裏是我的DQL查詢:如何計算DQL查詢返回的行數?
$items=$em->createQuery("select i.unitPrice,i.quantity,i.linetotal,i.description from InvoicesInvoicesBundle:Invoiceshasitems i where i.invoiceid='".$id."'");
$itemdata=$items->getResult();
使用count()
$items=$em->createQuery("select COUNT(i.unitPrice)
from InvoicesInvoicesBundle:Invoiceshasitems i
where i.invoiceid='".$id."'");
$itemdata=$items->getResult();
http://docs.doctrine-project.org/en/2.1/reference/dql-doctrine-query-language.html
你應該書籤上面的鏈接,如果你是在DQL定期工作。
我用你的代碼,但這顯示像這樣的錯誤[語法錯誤]行0,欄24:錯誤:預期Doctrine \ ORM \ Query \ Lexer :: T_CLOSE_PARENTHESIS,得到',' –
嘗試更新後的查詢。可能是你的分貝只允許1列計數,使用總是有價值的列,我認爲單價是正確的 – Tauseef
試試這個代碼
$qbTotal = $this->em->createQueryBuilder();
$qbTotal->select('count(i)')
->from('InvoicesInvoicesBundle:Invoiceshasitems', 'i')
->where('i.invoiceid = :id')
->setParameter('id', $id)
;
$qeuryTotal = $qbTotal->getQuery();
$totalItems = $qeuryTotal->getSingleScalarResult();
我用你的代碼,但它給出了錯誤未定義的財產 –
好,@Praveesh有答案,雖然其間也有輕微錯字。應該是:id而不是?id。
但這個鏈接已經贏得了我680名代表,所以我敢肯定它是正確的:Count Rows in Doctrine QueryBuilder
赫赫幸運拳 - 其他人正在努力工作他們的代表:-) 680並計數......但老實說,我遇到了你的答案,並在幾個月前將它提升了。問候 – nifr
你想顯示沿結果的序列號?作爲1,2,3 ..或只是記錄的總數爲34 65等沒有任何其他數據? – Tauseef
使用計數http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html –