Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine count records of table

I would like to know how I can count all records of an entity in a doctrine repository

I found this solution but am not sure if this is good so:

public function findAllCounted()
{
    return $this->getEntityManager()
        ->createQuery('SELECT COUNT(a.id) FROM KSRArticleBundle:Article a')
        ->getSingleScalarResult();
}

Best Regards, Bodo

like image 343
bodokaiser Avatar asked Mar 30 '12 16:03

bodokaiser


2 Answers

You don't need to count on a specific field, so this will do:

SELECT COUNT(a) FROM KSRArticleBundle:Article a
like image 166
Elnur Abdurrakhimov Avatar answered Nov 02 '22 02:11

Elnur Abdurrakhimov


Just for the record, it is usually better to count on the id :

SELECT COUNT(a.id) FROM KSRArticleBundle:Article a

is a little better

like image 5
Julien Deniau Avatar answered Nov 02 '22 04:11

Julien Deniau