我想计算从 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 查询:
$items=$em->createQuery("select i.unitPrice,i.quantity,i.linetotal,i.description from InvoicesInvoicesBundle:Invoiceshasitems i where i.invoiceid='".$id."'");
$itemdata=$items->getResult();
Use 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
you should bookmark the above link if you are working regularly on DQL.
试试这个代码
$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