Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better to use DQL for getting Column Count or Get Collection Then Count?

I am quite sure that DQL will be the way to go, but I am wondering if Doctrine, i am using Doctrine 2, has someway to return the row count. I won't be using the rows itself, I just want the count.

like image 513
Jiew Meng Avatar asked Jan 21 '11 13:01

Jiew Meng


1 Answers

I'm new to Doctrine2 but it looks like you can simply do this:

$query = $em->createQuery('SELECT COUNT(u.id) FROM Entities\User u');
$count = $query->getSingleScalarResult();

Source (Using Agregate Functions): http://www.doctrine-project.org/docs/orm/2.0/en/reference/dql-doctrine-query-language.html#dql-select-examples

Allowed aggregate functions: http://www.doctrine-project.org/docs/orm/2.0/en/reference/dql-doctrine-query-language.html#aggregate-functions

like image 148
Jakub Zalas Avatar answered Sep 24 '22 08:09

Jakub Zalas