开发者

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

开发者 https://www.devze.com 2023-02-06 04:21 出处:网络
I am quite sure that DQL will be t开发者_JAVA百科he 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 wan

I am quite sure that DQL will be t开发者_JAVA百科he 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.


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


Here is another interesting point of view about using aggregated functions in DQL

http://doctrine-orm.readthedocs.org/en/latest/cookbook/aggregate-fields.html

Maybe you would avoid the creation of an specific query to obtain an aggregate value. In this case, aggregate fields are a good alternative.

0

精彩评论

暂无评论...
验证码 换一张
取 消