このセクションでは、最近公開されたコメントのリストを表示する最後のポートレットを作成します。
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');
}
}
上記では、Comment
クラスで定義されている findRecentComments
メソッドを呼び出します。
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"> 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> ......
タイプミスを見つけた場合、またはこのページを改善する必要があると思われる場合は?
githubで編集してください !
ビュー recentComments の内容
/protected/components/views/recentComments.php のデモブログの内容は次のようになります。
<ul> <?php foreach($this->getRecentComments() as $comment): ?> <li><?php echo $comment->authorLink; ?> on <?php echo CHtml::link(CHtml::encode($comment->post->title), $comment->getUrl()); ?> </li> <?php endforeach; ?> </ul>
コメントするにはサインアップまたはログインしてください。