В этом разделе мы создаем последний портлет, который отображает список недавно опубликованных комментариев.
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'); ?>
$this->widget('TagCloud', array(
'maxTags'=>Yii::app()->params['tagCloudCount'],
)); ?>
$this->widget('RecentComments', array(
'maxComments'=>Yii::app()->params['recentCommentCount'],
)); ?>
</div>
…