此节中,我们创建最后一个 portlet ,它将显示最新发布的评论列表。
RecentComments 类 ¶我们将 RecentComments类创建在文件 /wwwroot/blog/protected/components/RecentComments.php 中。此文件内容如下:
Yii::import('zii.widgets.CPortlet');
 
class RecentComments extends CPortlet
{
    public $title='Recent Comments';
    public $maxComments=10;
 
    public function getRecentComments()
    {
        return Comment::model()->findRecentComments($this->maxComments);
    }
 
    protected function renderContent()
    {
        $this->render('recentComments');
    }
}如上所示,我们调用了 Comment 类中定义的 findRecentComments 方法。此方法代码如下:
class Comment extends CActiveRecord
{
    ......
    public function findRecentComments($limit=10)
    {
        return $this->with('post')->findAll(array(
            'condition'=>'t.status='.self::STATUS_APPROVED,
            'order'=>'t.create_time DESC',
            'limit'=>$limit,
        ));
    }
}recentComments 视图 ¶recentComments 视图存储在文件 /wwwroot/blog/protected/components/views/recentComments.php 中。它只是简单的显示由 RecentComments::getRecentComments() 方法返回的每一条评论。
RecentComments Portlet ¶我们修改布局文件 /wwwroot/blog/protected/views/layouts/column2.php 插入最后这个 portlet,
...... <div id="sidebar"> if(!Yii::app()->user->isGuest) $this->widget('UserMenu'); $this->widget('TagCloud', array( 'maxTags'=>Yii::app()->params['tagCloudCount'], )); $this->widget('RecentComments', array( 'maxComments'=>Yii::app()->params['recentCommentCount'], )); </div> ......
            Found a typo or you think this page needs improvement?
                Edit it on github !
        
Signup or Login in order to comment.