Blog tutorial - cannot approve comments

Hi there,

I am working through the blog tutorial, and everything working fine, up until the point where I am trying to approve the comments (http://www.yiiframework.com/doc/blog/1.1/en/comment.admin).

I have the following code in the view:


		<?php if($data->status==Comment::STATUS_PENDING): ?>

			<span class="pending">Pending approval</span> |

			<?php echo CHtml::linkButton('Approve', array(

				'submit'=>array('comment/approve','id'=>$data->id),

			)); ?> |

		<?php endif; ?>

The following in my model Comment:


     const STATUS_PENDING=1;

     const STATUS_APPROVED=2;

and


     /**

      * Approves a comment.

      */

	public function approve(){

		$this->status=Comment::STATUS_APPROVED;

		$this->update(array('status'));

	}



and


     /**

      * This is invoked before the record is saved.

      * @return boolean whether the record should be saved.

      */

     protected function beforeSave() {

          if (parent::beforeSave()) {

               if ($this->isNewRecord){

                    $this->create_time = time();

                    $this->status = self::STATUS_PENDING;

               }


               return true;

          }else{

               return false;

          }

          return true;

     }



and then in my CommentController I have:


     /**

      * Approves a particular comment.

      * If approval is successful, the browser will be redirected to the comment index page.

      */

     public function actionApprove() {

          if (Yii::app()->request->isPostRequest) {

               $comment = $this->loadModel();

               if($comment->approve()){

                    echo 'saved';

               }else{

                    echo 'not saved';

               }

               $this->redirect(array('index'));

          }

          else

               throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');

     }



No, whenever I click the approve link, it just says "not saved" and gives the expected "Cannot modify header information".

Please can someone advise as to why it will not save.

Thanks in advance.

Dave

The approve method doesn’t return anything.


 /**

      * Approves a comment.

      */

        public function approve(){

                $this->status=Comment::STATUS_APPROVED;

                $this->update(array('status'));

        }

So when you call in the controller


 if($comment->approve())

you probably get a false return

Try changing


$this->update(array('status'));

To:


return $this->update(array('status'));

According to the documentation update() returns true if it was successful.

CActiveRecord update()

Ahh, that’s brilliant.

Thanks very much :slight_smile:

I am glad it helped.