0

我想计算从 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();
4

3 回答 3

1

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.

于 2013-10-08T12:49:14.840 回答
1

试试这个代码

 $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();
于 2013-10-08T13:15:30.207 回答
0

好吧,@Praveesh 有答案,尽管其中有一个轻微的错字。应该是:id 而不是 ?id。

但是这个链接为我赢得了 680 次重复,所以我很确定它是正确的:Count Rows in Doctrine QueryBuilder

于 2013-10-08T13:29:21.630 回答