Yii Framework Forum: Blog tutorial - cannot approve comments - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Blog tutorial - cannot approve comments Rate Topic: -----

#1 User is offline   iamdash 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 17-May 11

Posted 17 May 2011 - 10:30 AM

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.yiiframew...n/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
0

#2 User is offline   leventis 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 12-May 11

Posted 17 May 2011 - 10:49 AM

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()
0

#3 User is offline   iamdash 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 17-May 11

Posted 17 May 2011 - 11:59 AM

Ahh, that's brilliant.

Thanks very much :-)
0

#4 User is offline   leventis 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 12-May 11

Posted 17 May 2011 - 03:07 PM

I am glad it helped.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users