开发者

order by and count in Doctrine Symfony

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

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 $开发者_Python百科c) {
  echo $c;
}

this show me only first data in table.

how can i make it?


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜