0 フォロワー

最近のコメントポートレットの作成

このセクションでは、最近公開されたコメントのリストを表示する最後のポートレットを作成します。

1. 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,
        ));
    }
}

2. recentComments ビューの作成

recentComments ビューは、ファイル /wwwroot/blog/protected/components/views/recentComments.php に保存されます。これは、RecentComments::getRecentComments() メソッドによって返されたすべてのコメントを表示するだけです。

3. RecentComments ポートレットの使用

この最後のポートレットを埋め込むために、レイアウトファイル /wwwroot/blog/protected/views/layouts/column2.php を変更します。

......
<div id="sidebar">
 
    <?php if(!Yii::app()->user->isGuest) $this->widget('UserMenu'); ?>
 
    <?php $this->widget('TagCloud', array(
        'maxTags'=>Yii::app()->params['tagCloudCount'],
    )); ?>
 
    <?php $this->widget('RecentComments', array(
        'maxComments'=>Yii::app()->params['recentCommentCount'],
    )); ?>
 
</div>
......

タイプミスを見つけた場合、またはこのページを改善する必要があると思われる場合は?
githubで編集してください !