Arabic | English | Spanish | Indonesian | Japanese | Polish | Portuguese | Russian | Ukrainian | Simplified Chinese
Створення портлета останніх коментарів
У цьому розділі ми створюємо останній портлет, який відображає список нещодавно опублікованих коментарів.
Створення класу 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> …