В этом разделе мы создаем последний портлет, который отображает список недавно опубликованных комментариев.
RecentComments ¶Мы создаем класс RecentComments в файле
/wwwroot/blog/protected/components/RecentComments.php.
Содержимое файла:
class RecentComments extends Portlet { public $title='Recent Comments'; public function getRecentComments() { return Comment::model()->findRecentComments(); } protected function renderContent() { $this->render('recentComments'); } }
В коде выше мы вызываем метод findRecentComments, который определен в классе Comment следующим образом,
class Comment extends CActiveRecord { ...... public function findRecentComments($limit=10) { $criteria=array( 'condition'=>'Comment.status='.self::STATUS_APPROVED, 'order'=>'Comment.createTime DESC', 'limit'=>$limit, ); return $this->with('post')->findAll($criteria); } }
recentComments ¶Представление recentComments сохранено в файле /wwwroot/blog/protected/components/views/recentComments.php.
Представление просто отображает каждый комментарий, возвращенный методом RecentComments::getRecentComments().
RecentComments ¶Мы изменяем файл макета /wwwroot/blog/protected/views/layouts/main.php
для включения нашего последнего портлета:
...... <div id="sidebar"> <?php $this->widget('UserLogin',array('visible'=>Yii::app()->user->isGuest)); <?php $this->widget('UserMenu',array('visible'=>!Yii::app()->user->isGuest)); <?php $this->widget('TagCloud'); <?php $this->widget('RecentComments'); </div> ......
Be the first person to leave a comment
Please login to leave your comment.