Search code examples
phpsymfonydoctrine-ormdql

Doctrine2 count result returns wrong number


$em = $this->getDoctrine()->getEntityManager();
$qb = $em->createQueryBuilder();

$tickets = $qb->select('COUNT(ticket.id)')->from('MyBundle:Ticket', 'ticket');
var_dump($tickets->getQuery()->getSingleScalarResult());

returns

array(1) {
  [0]=>
  array(1) {
    [1]=>
    string(3) "125"
  }
}

But in database there are just 5 entries. Any ideas what is wrong?

var_dump($tickets->getQuery()->getSql());

gives

SELECT COUNT(t0_.id) AS sclr0 FROM ticket t1_, ticket t2_, ticket t0_ GROUP BY t0_.id ORDER BY t2_.created_at ASC LIMIT 1


Solution

  • Actually before this query there were two more queries using the same query builder instance. This might be the problem. I remove the queries and now the count query works just fine.