Count Rows In Doctrine QueryBuilder
Answer : Something like: $qb = $entityManager->createQueryBuilder(); $qb->select('count(account.id)'); $qb->from('ZaysoCoreBundle:Account','account'); $count = $qb->getQuery()->getSingleScalarResult(); Some folks feel that expressions are somehow better than just using straight DQL. One even went so far as to edit a four year old answer. I rolled his edit back. Go figure. Here is another way to format the query: return $repository->createQueryBuilder('u') ->select('count(u.id)') ->getQuery() ->getSingleScalarResult(); It's better to move all logic of working with database to repositores. So in controller you write /* you can also inject "FooRepository $repository" using autowire */ $repository = $this->getDoctrine()->getRepository(Foo::class); $count = $repository->count(); And in Repository/FooRepository.php public function count() { $qb = $repository-...