Managing Comments

Comment management includes updating, deleting and approving comments. These operations are implemented as actions in the CommentController class.

1. Updating and Deleting Comments

The code generated by Gii for updating and deleting comments remains largely unchanged.

2. 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,

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.

Of course, we also need to create the approve() method in the Comment model. It is as follows,

public function approve()
{
    $this->status=Comment::STATUS_APPROVED;
    $this->update(array('status'));
}

Here we are simply setting the status property of the comment to approved as defined by the status constants in the Comment class:

class Comment extends CActiveRecord
{
    ...
 
    const STATUS_PENDING=1;
    const STATUS_APPROVED=2;
 
    ..
}

and then calling the update() method to save this newly set property to the database.

We also modify the actionIndex() method of CommentController to show all comments. We would like to see comments pending approval show up first.

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, 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 to indicate we want these values taken from the primary table, tbl_comment.

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.

$Id$

Total 2 comments

#8955 report it
nomeacuerdo at 2012/07/09 03:26pm
CommentController

If at this point you try to Approve any comment, you'll have an error 403.

To fix it:

1 - Edit the accessRules() method to allow the update from authenticated users:

public function accessRules()
    {
        return array(
            array('allow', // allow authenticated users to access all actions
                'users'=>array('@'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }

2 - Last, correct the actionApprove method as follows:

public function actionApprove($id)
    {
        if(Yii::app()->request->isPostRequest)
        {
            $comment=$this->loadModel($id);
            $comment->approve();
            $this->redirect(array('index'));
        }
        else
            throw new CHttpException(400,'Invalid request...');
    }
#1552 report it
juanmjimenezs at 2009/06/26 01:43pm
approve() method

The method approve() should be:

public function approve()
{
    $this->status = Comment::STATUS_APPROVED;
    $this->save();
}

Leave a comment

Please to leave your comment.