このセクションでは、最近投稿されたコメントのリストを表示する、最後のポートレットを作成します。
RecentComments クラスの作成 ¶/wwwroot/blog/protected/components/RecentComments.php ファイルに RecentComments クラスを作成します。このファイルは以下の内容です。
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 クラスで以下のように定義されます。
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 ポートレットの使用 ¶レイアウトファイル /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> ......
Be the first person to leave a comment
Please login to leave your comment.