如何按照“教条2”进行排序? [英] How to order by count in Doctrine 2?

查看:122
本文介绍了如何按照“教条2”进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



代码:

  public function countYear()
{
$ qb = $ this-> getEntityManager() - > createQueryBuilder();
$ qb-> select('b.year,COUNT(b.id)')
- > from('\My\Entity\Album','b')
- > where('b.year IS NOT NULL')
- > addOrderBy('sclr1','DESC')
- > addGroupBy('b.year');
$ query = $ qb-> getQuery();
die($ query-> getSQL());
$ result = $ query-> execute();
// die(print_r($ result));
return $ result;
}

我似乎不能说 COUNT(b .id)AS计数,因为它给出一个错误,而
我不知道该用什么作为 addOrderby(???,'DESC') value?

解决方案

使用 COUNT(b。 id)AS计数?这可能是因为 count 是保留字。尝试 COUNT(b.id)AS idCount 或类似的。



或者,尝试 $ qb-> addOrderby('COUNT(b.id)','DESC');



你的数据库系统是什么mysql,postgresql,...)?


I'm trying to group my entity by a field (year) and do a count of it.

Code:

public function countYear()
{
    $qb = $this->getEntityManager()->createQueryBuilder();
    $qb->select('b.year, COUNT(b.id)')
        ->from('\My\Entity\Album', 'b')
        ->where('b.year IS NOT NULL')
        ->addOrderBy('sclr1', 'DESC')
        ->addGroupBy('b.year');
    $query = $qb->getQuery();
    die($query->getSQL());
    $result = $query->execute();
    //die(print_r($result));
    return $result;
}

I can't seem to say COUNT(b.id) AS count as it gives an error, and I do not know what to use as the addOrderby(???, 'DESC') value?

解决方案

what is the error you get when using COUNT(b.id) AS count? it might be because count is a reserved word. try COUNT(b.id) AS idCount, or similar.

alternatively, try $qb->addOrderby('COUNT(b.id)', 'DESC');.

what is your database system (mysql, postgresql, ...)?

这篇关于如何按照“教条2”进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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