我很确定 DQL 将是要走的路,但我想知道我正在使用 Doctrine 2 的 Doctrine 是否有办法返回行数。我不会使用行本身,我只想要计数。
问问题
12972 次
2 回答
30
我是 Doctrine2 的新手,但看起来你可以简单地这样做:
$query = $em->createQuery('SELECT COUNT(u.id) FROM Entities\User u');
$count = $query->getSingleScalarResult();
来源(使用聚合函数): http: //www.doctrine-project.org/docs/orm/2.0/en/reference/dql-doctrine-query-language.html#dql-select-examples
允许的聚合函数: http: //www.doctrine-project.org/docs/orm/2.0/en/reference/dql-doctrine-query-language.html#aggregate-functions
于 2011-01-22T09:41:52.360 回答
0
这是关于在 DQL 中使用聚合函数的另一个有趣的观点
http://doctrine-orm.readthedocs.org/en/latest/cookbook/aggregate-fields.html
也许您会避免创建特定查询来获取聚合值。在这种情况下,聚合字段是一个不错的选择。
于 2015-03-03T16:10:56.430 回答