在Doctrine QueryBuilder中计数行 [英] Count Rows in Doctrine QueryBuilder

查看:169
本文介绍了在Doctrine QueryBuilder中计数行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Doctrine的QueryBuilder来构建一个查询,我想从查询中获取结果的总计数。

I'm using Doctrine's QueryBuilder to build a query, and I want to get the total count of results from the query.

$repository = $em->getRepository('FooBundle:Foo');

$qb = $repository->createQueryBuilder('n')
        ->where('n.bar = :bar')
        ->setParameter('bar', $bar);

$query = $qb->getQuery();

//this doesn't work
$totalrows = $query->getResult()->count();

我只想运行这个查询的计数来获取总行,但不返回实际结果。 (在这个计数查询之后,我将使用maxResults进一步修改查询以进行分页。)

I just want to run a count on this query to get the total rows, but not return the actual results. (After this count query, I'm going to further modify the query with maxResults for pagination.)

推荐答案

/ p>

Something like:

$qb = $entityManager->createQueryBuilder();
$qb->select('count(account.id)');
$qb->from('ZaysoCoreBundle:Account','account');

$count = $qb->getQuery()->getSingleScalarResult();

编辑

有些人觉得表达式不仅仅是使用直接的DQL,一个甚至甚至编辑一个四年的答案。我把他的编辑翻回来。
Go figure。

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.

这篇关于在Doctrine QueryBuilder中计数行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆