Symfony 如何计算DQL查询返回的行数?
我想计算从DQL查询返回的行数,我如何做到这一点? 以下是我的DQL查询:Symfony 如何计算DQL查询返回的行数?,symfony,doctrine-orm,dql,Symfony,Doctrine Orm,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(); 使用计数() 如果您经常使用
$items=$em->createQuery("select i.unitPrice,i.quantity,i.linetotal,i.description from InvoicesInvoicesBundle:Invoiceshasitems i where i.invoiceid='".$id."'");
$itemdata=$items->getResult();
使用计数()
如果您经常使用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次重复,所以我很确定它是正确的:你想在结果中显示序列号吗?如1,2,3。。或者仅记录总数为34 65等,没有任何其他数据??使用计数我使用了您的代码,但这显示了如下错误[语法错误]第0行,第24列:错误:预期条令\ORM\Query\Lexer::T_CLOSE_圆括号,get','尝试更新的查询。也许你的数据库只允许1列计数,使用总是有值的列,我认为单价是正确的Hehe the lucky punch-其他人正在为他们的代表努力:-)680和计数。。。但老实说,我在几个月前也看到了你的答案,并对它投了赞成票。问候语
$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();