在最近评论列表中显示已发布的帖子和已批准的评论


Yii:Show Published Post And Approved Comment at RecentComments List

我用的是yii blog from Yii Framework

我想在最近的评论列表中只显示已发布的帖子和批准的评论

我在注释模型

中使用了以下代码
public function findRecentComments($limit=10)
{
    return $this->with('post')->findAll(array(
        'condition'=>'t.status='.self::STATUS_APPROVED.'status='.Post::STATUS_PUBLISHED,
        'order'=>'t.create_time DESC',
        'limit'=>$limit,
    ));
}

但是在RecentComments列表中显示所有帖子和批准的评论

我想在RecentComments列表中显示已发布的帖子和已批准的评论

关系模型的设置条件在with()函数内完成。我认为findAllByAttributes()更具可读性所以这里是使用这个函数

的解决方案
public function findRecentComments($limit=10)
{
   return $this->with(array('post' => array(
                    'condition' => 'post.status=:status',
                    'params'    => array(':status' => Post::STATUS_PUBLISHED),
            )))->findAllByAttributes( array( 
                self::getTableAlias(false, false).'.status' => self::STATUS_APPROVED
            ), 
            array(
                'order'=> self::getTableAlias(false, false).'.create_time',
                'limit' => $limit
            ) );
}