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

查看:25
本文介绍了在 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.)

推荐答案

类似:

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

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

有些人认为表达式比直接使用 DQL 更好.甚至有人编辑了一个四年前的答案.我回滚了他的编辑.去图.

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天全站免登陆