- Getting Started
- Initial Prototyping
- Post Management
- Comment Management
- Portlets
- Final Work
Comment management includes updating, deleting and approving comments. These operations are implemented as actions in the CommentController class.
The code generated by yiic for updating and deleting comments remains largely unchanged.
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.
We also modify the actionIndex() method of Comment to show all comments. We would like to see comments pending approved to 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.
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.
after changing all the updation i am getting this error.. give me solution
The method approve() should be:
public function approve()
{
$this->status = Comment::STATUS_APPROVED;
$this->save();
}
"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."
Is there any instructions about how this approve() method should be instructed?