-1

I am fairly new to Yii and while trying to make a blog application of my own I made this function to add a comment to my post.

However, I have done everything according to theory but still am getting a :

Fatal error: Call to a member function addComment() on a non-object in /htdocs/blog/protected/controllers/PostController.php on line 63

My Post.php model class has this function :

public function addComment($comment){
        $comment->tbl_post_id = $this->id;
        return $comment->save();
    }

And my PostController.php has these two function, one for creating the comment and the other one for altering the view file.

public function actionView($id)
   {
       $post=$this->loadModel($id);
       $comment=$this->createComment($post);
       $this->render('view',array(
         'model'=>$post,
            'comment'=>$comment,
)); }

    protected function createComment($post)
   {
     $comment=new Comment;
     if(isset($_POST['Comment']))
     {
       $comment->attributes=$_POST['Comment'];
       if($post->addComment($comment)) // **This is line 63**
       {
         Yii::app()->user->setFlash('commentSubmitted',"Your comment has been added." );
         $this->refresh();
       }
}
     return $comment;
   }

So logically I am calling the member function addComment inside the Post Model class using $post->addComment, and all the member functions of a model are initialized right? And logicaly shouldn't this be correct right? However, I am getting the above fatal error.

What am I doing wrong?

Any help would be appreciated.

Regards,

P.S - I am sorry if I am doing something really stupid.

tereško
  • 58,060
  • 25
  • 98
  • 150
Sankalp Singha
  • 4,461
  • 5
  • 39
  • 58

2 Answers2

2

$post isn't an object since you don't declare it anywhere

 protected function createComment($issue)
 {
   $comment=new Comment;
   if(isset($_POST['Comment']))
   {
     $comment->attributes=$_POST['Comment'];
     if($post->addComment($comment)) // **This is line 63**
     {
       Yii::app()->user->setFlash('commentSubmitted',"Your comment has been added." );
       $this->refresh();
     }
   }
   return $comment;
 }

on line 63 he's searching for a variable named $post that does not exist. You have to create it or load from db like you're doing into your actionView()

$post=$this->loadModel($id);

Obviously for load it, you need $id that has to be passed to your createComment() function

DonCallisto
  • 29,419
  • 9
  • 72
  • 100
0

You have addComment() method in Post Model Class. So, You can handle this method by the Object of Post Model class from any of your controller.

Your declaration

 $post->addComment($comment) 

is Correct but there is no $post object. So Just create instance of Post Model as

$post=new Post(); 

Finally your code should be like

 if(isset($_POST['Comment']))
 {
    $comment->attributes=$_POST['Comment'];
    $post=new Post(); 

    if($post->addComment($comment)) // **This is line 63**
    {
       Yii::app()->user->setFlash('commentSubmitted',"Your comment has been added." );
       $this->refresh();
    }
 }
Hearaman
  • 8,466
  • 13
  • 41
  • 58