在Doctrine Symfony中排序和统计

时间:2022-11-29 20:17:36
id  | name 
1   | aaa  
2   | bbb  
3   | ccc  
4   | ccc  
5   | aaa  
6   | ccc  
7   | ccc  
8   | aaa  
9   | bbb  
10  | ccc  
11  | aaa

i would like become:

我想成为:

name | count
ccc  | 5
aaa  | 4
bbb  | 2

orderby count DESC

按计数DESC

i made:

我做了:

public function getCount() 
{        
        $q = $this->createQuery('q')
            ->select('*')
            ->addSelect('count(q.name) as count')
            ->groupBy('q.name')
            ->orderBy('count DESC');

        return $q->execute();        
}

but if :

但如果:

foreach ($count as $c) {
  echo $c;
}

this show me only first data in table.

这只显示表中的第一个数据。

how can i make it?

我该怎么做?

1 个解决方案

#1


3  

Change your loop to this:

将你的循环改为:

foreach ($count as $c) {
  echo $c->count . "\n";
}

edit: a good way to debug this is to change your return to

编辑:调试此方法的一个好方法是更改​​您的返回

$q->fetchArray();

and then in your loop

然后在你的循环中

print_r($c);

#1


3  

Change your loop to this:

将你的循环改为:

foreach ($count as $c) {
  echo $c->count . "\n";
}

edit: a good way to debug this is to change your return to

编辑:调试此方法的一个好方法是更改​​您的返回

$q->fetchArray();

and then in your loop

然后在你的循环中

print_r($c);