Thứ Hai, 19 tháng 5, 2014

Count of Related Items YII

Count of Related Items 

Let's say we have two models and relation between them:
class Author extends CActiveRecord 
{
    ...
    function relations() {
        return array(
            'posts' => array(self::HAS_MANY, 'Post', 'author_id'),
        );
    }
    ...
}
 
class Post extends CActiveRecord 
{
    ...
}
When you list Authors in grid you would like to print the count of posts in column, allow sorting on this column and probably filtering by the count.