このセクションでは、最近投稿されたコメントのリストを表示する、最後のポートレットを作成します。
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ビューの作成 ¶/wwwroot/blog/protected/components/views/recentComments.phpファイルにrecentCommentsビューは格納されます。
これは単純に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.