Files
yii/docs/blog/comment.admin.txt
qiang.xue b23c7c6556
2010-02-18 00:24:54 +00:00

60 lines
2.5 KiB
Plaintext

Managing Comments
=================
Comment management includes updating, deleting and approving comments. These operations are implemented as actions in the `CommentController` class.
Updating and Deleting Comments
------------------------------
The code generated by `yiic` for updating and deleting comments remains largely unchanged.
Approving Comments
------------------
When comments are newly created, they are in pending approval status and need to be approved in order to be visible to guest users. Approving a comment is mainly about changing the status column of the comment.
We create an `actionApprove()` method in `CommentController` as follows,
~~~
[php]
public function actionApprove()
{
if(Yii::app()->request->isPostRequest)
{
$comment=$this->loadModel();
$comment->approve();
$this->redirect(array('index'));
}
else
throw new CHttpException(400,'Invalid request...');
}
~~~
In the above, when the `approve` action is invoked via a POST request, we call the `approve()` method defined in the `Comment` model to change the status. We then redirect the user browser to the page displaying the post that this comment belongs to.
We also modify the `actionIndex()` method of `Comment` to show all comments. We would like to see comments pending approved to show up first.
~~~
[php]
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('Comment', array(
'criteria'=>array(
'with'=>'post',
'order'=>'t.status, t.create_time DESC',
),
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
~~~
Notice that in the above code, because both `tbl_post` and `tbl_comment` have columns `status` and `create_time`, we need to disambiguate the corresponding column reference by prefixing them with table alias names. As described in [the guide](http://www.yiiframework.com/doc/guide/database.arr#disambiguating-column-names), the alias for the primary table in a relational query is always `t`. Therefore, we are prefixing `t` to the `status` and `create_time` columns in the above code.
Like the post index view, the `index` view for `CommentController` uses [CListView] to display the comment list which in turn uses the partial view `/wwwroot/blog/protected/views/comment/_view.php` to display the detail of each individual comment. We will not go into details here. Interested readers may refer to the corresponding file in the blog demo `/wwwroot/yii/demos/blog/protected/views/comment/_view.php`.
<div class="revision">$Id$</div>