最近投稿されたコメントのリストを表示する
RecentCommentsクラスの作成
タグを表示処理するための”RecentComments.php”ファイルを作成する
格納先は【/blog/protected/components/】
<?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'); } }
findRecentComments()メソッドをCommentモデルに追加するする
・・・ /** * * 直近のコメントを取得 * **/ 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.php”ファイルを作成する
格納先は【/blog/protected/components/views/】
<ul> <?php foreach($this->getRecentComments() as $comment): ?> <li> <?php echo $comment->getAuthorLink(); ?> on <?php echo CHtml::link(CHtml::encode($comment->post->title), $comment->getUrl()); ?> </li> <?php endforeach; ?> </ul>
recentCommentsポートレットの使用
“colimn2.php”ファイルを修正する
・・・ <div id="sidebar"> <?php // "UserMenu"ポートレットを表示 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><!-- sidebar --> ・・・
■実行画面
【http://loaclhost/index.php?r=post/】にアクセス
※URLの書き換えについては次章で解説する
参考サイト:
http://www.yiiframework.com/doc/blog/1.1/ja/portlet.comments