0

I want to count number of rows whose return from DQL query,how i do this? here is my DQL query:

$items=$em->createQuery("select i.unitPrice,i.quantity,i.linetotal,i.description from InvoicesInvoicesBundle:Invoiceshasitems i where i.invoiceid='".$id."'");
$itemdata=$items->getResult();
Muhammad Arif
  • 1,014
  • 3
  • 22
  • 56

3 Answers3

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.

Tauseef
  • 2,035
  • 18
  • 17
  • i used your code but this shows error like this [Syntax Error] line 0, col 24: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got ',' – Muhammad Arif Oct 08 '13 at 12:58
  • try the updated query. may be ur db allows only 1 column in count, use the column that always have a value and i think unit price is the right one – Tauseef Oct 08 '13 at 13:00
1

Try this code

 $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
  • 1,257
  • 1
  • 10
  • 23
0

Well, @Praveesh has the answer though there is a slight typo in it. Should be:id instead of ?id.

But this link has earned me 680 reps so I'm pretty sure it is correct: Count Rows in Doctrine QueryBuilder

Community
  • 1
  • 1
Cerad
  • 48,157
  • 8
  • 90
  • 92
  • hehe the lucky punch - others are working hard for their reps :-) 680 and counting ... but honestly i came across your answer and upvoted it months ago, too. greetings – Nicolai Fröhlich Oct 08 '13 at 21:26