W części tej utworzymy portlet, który wyświetli listę komentarzy, które zostały ostatnio opublikowane.
RecentComments ¶Tworzymy klasę RecentComments w pliku /wwwroot/blog/protected/components/RecentComments.php. Plik ten ma następującą zawartość:
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');
}
}W powyższym kodzie wywołaliśmy metodę findRecentComments, która jest zdefiniowana w klasie Comment w następujący sposób:
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 ¶Widok recentComments jest zapisany w pliku /wwwroot/blog/protected/components/views/recentComments.php. Widok po prostu wyświetla każdy komentarz zwrócony przez metodę RecentComments::getRecentComments().
RecentComments ¶Zmodyfikujemy plik układu /wwwroot/blog/protected/views/layouts/column2.php by osadzić w nim ten ostatni portlet.
...... <div id="sidebar"> 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> ......
Found a typo, or you think this page needs improvement?
Edit it on GitHub !
Signup or Login in order to comment.