in the classic example 1 author has many posts…
How can I create a Cdbcriteria to find authors WITHOUT posts ?
in the classic example 1 author has many posts…
How can I create a Cdbcriteria to find authors WITHOUT posts ?
I would add an relation to the Post model in the Author model.
Then get al the authors and then in a foreach check if(count($authors->posts) == 0) then add to authorswithzeroposts array.
and if I need a cdataprovider to use with a cgridview?
Hi,
I think you will find the exact answer in this wiki: http://www.yiiframework.com/wiki/319/searching-and-sorting-by-count-of-related-items-in-cgridview
Create a named scope in Author model:
public function scopes()
{
return array(
'withNoPosts' => array(
'condition' => 'NOT EXISTS (SELECT * FROM post WHERE post.author_id = ' . $this->tableAlias . '.id)',
),
);
}
And use it with CDbCriteria, when needed.
Note: I know that it can be optimized using JOIN instead of NOT EXISTS. I just wanted to keep the example simple.