Named Scope For Getting All The Categories With At Least One Post

Hi,

I need to filter a categories list by showing only the ones with at least one post.

I’ve created the STAT relation to get the post count :


 'postCount'=>array(self::STAT, 'Post', 'category_id'),

and than the named-scope :


public function scopes() {

  return array(

     'nonempty'=>array

     (

        'with'=>'postCount',

        'condition'=>'postCount>0',

     ) 

   );

}

but that obviously don’t work.

How can I accomplish that without iterating all the elements ? I’ve searched on the forum but I can’t find anything.

Thanks.




public function scopes() {

  return array(

     'nonempty'=>array

     (

        'condition'=>'EXISTS (SELECT id FROM post WHERE category_id = t.id)',

     ) 

   );

}



Thank you, that is working fine, but there is no way of using the STAT relation to do the magic ?

This will run a lot of subqueries if I have a lot of entries in my DB, right?