Bookmark and Share

Lee's 程序人生

HTML CSS Javascript XML AJAX ATLAS C# C++ 数据结构 软件工程 设计模式 asp.net Java 数字图象处理 Sql 数据库
  博客园  :: 首页  :: 新随笔  :: 联系 :: 管理

YiiFrameworkBlog开发向导:最新评论模块

Posted on 2010-02-24 21:51  analyzer  阅读(281)  评论(0编辑  收藏  举报

本节中我们建立最新评论模块,显示最新发布的评论列表

建立RecentComments类

我们在文件blog/protected/components/RecentComments.php中建立RecentComments类。内容如下:

 
  1. <?php
  2. class RecentComments extends Portlet
  3. {
  4.     public $title='Recent Comments';
  5.  
  6.     public function getRecentComments()
  7.     {
  8.         return Comment::model()->findRecentComments();
  9.     }
  10.  
  11.     protected function renderContent()
  12.     {
  13.         $this->render('recentComments');
  14.     }
  15. }

 

在上面的代码中我们调用Comment类的findRecentComments方法,该方法代码如下:

 
  1. class Comment extends CActiveRecord
  2. {
  3.     ......
  4.  
  5.     public function findRecentComments($limit=10)
  6.     {
  7.         $criteria=array(
  8.             'condition'=>'Comment.status='.self::STATUS_APPROVED,
  9.             'order'=>'Comment.createTime DESC',
  10.             'limit'=>$limit,
  11.         );
  12.         return $this->with('post')->findAll($criteria);
  13.     }
  14. }

 

建立最新评论的视图

最新评论的视图建立在blog/protected/components/views/recentComments.php文件中。该视图文件对RecentComments::getRecentComments() 返回每条的评论进行了简单的显示

使用最新评论模块

我们如下修改布局文件blog/protected/views/layouts/main.php,以添加这最后的一个模块。

 
  1. ......
  2. <div id="sidebar">
  3.  
  4. <?php $this->widget('UserLogin',array('visible'=>Yii::app()->user->isGuest)); ?>
  5.  
  6. <?php $this->widget('UserMenu',array('visible'=>!Yii::app()->user->isGuest)); ?>
  7.  
  8. <?php $this->widget('TagCloud'); ?>
  9.  
  10. <?php $this->widget('RecentComments'); ?>
  11.  
  12. </div>
  13. ......
我要啦免费统计