Создание портлета последних комментариев
В этом разделе мы создаем последний портлет, который отображает список недавно опубликованных комментариев.
Создание класса 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'); } }
В коде выше мы вызываем метод 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> …