首先,检查你的blog demo里的protectedmodelsComment.php,确保Comment模型有一个search的方法,如果没有,就用gii生成一个,我下载到的blog demo里倒是没有。
然后,写代码的时间到了,我们从 CommentController 开始,我们给它加一个 actionList:
代码如下 | 复制代码 |
public function actionList() { $model=new Comment('search'); $model->unsetAttributes(); if(isset($_GET['Comment'])) $model->attributes=$_GET['Comment']; $this->render('list',array( 'model'=>$model, )); } |
代码如下 | 复制代码 |
breadcrumbs=array( 'Comments', ); ?> Manage Commentswidget('zii.widgets.grid.CGridView', array( 'dataProvider'=>$model->search(), 'filter'=>$model, 'columns' => array( 'content', 'post.title', 'status', 'author' ), )); ?> |
代码如下 | 复制代码 |
'columns'=>array( 'content', 'post.title', 'status', 'author', ), |
代码如下 | 复制代码 |
private $_postTitle = null; public function getPostTitle() { if ($this->_postTitle === null && $this->post !== null) { $this->_postTitle = $this->post->title; } return $this->_postTitle; } public function setPostTitle($value) { $this->_postTitle = $value; } |
代码如下 | 复制代码 |
public function rules() { // NOTE: you should only define rules for those attributes that // will receive user inputs. return array( array('content, author, email', 'required'), array('author, email, url', 'length', 'max'=>128), array('email','email'), array('url','url') array('content, postTitle, status, author', 'safe', 'on'=>'search'), ); } |
代码如下 | 复制代码 |
$criteria=new CDbCriteria; $criteria->with = "post"; // 确保查询 post 表 $criteria->compare('t.content',$this->content,true); $criteria->compare('t.status',$this->status); $criteria->compare('t.author',$this->author,true); $criteria->compare('post.title', $this->postTitle,true); |
代码如下 | 复制代码 |
$sort = new CSort(); $sort->attributes = array( 'defaultOrder'=>'t.create_time DESC', 'content'=>array( 'asc'=>'t.content', 'desc'=>'t.content desc', ), 'status'=>array( 'asc'=>'t.status', 'desc'=>'t.status desc', ), 'author'=>array( 'asc'=>'t.author', 'desc'=>'t.author desc', ), 'postTitle'=>array( 'asc'=>'post.title', 'desc'=>'post.title desc', ), ); |
代码如下 | 复制代码 |
return new CActiveDataProvider('Comment', array( 'criteria'=>$criteria, 'sort'=>$sort )); return new CActiveDataProvider('Comment', array( 'criteria'=>$criteria, 'sort'=>$sort )); |
代码如下 | 复制代码 |
'columns'=>array( 'content', 'postTitle', 'status', 'author', ), |