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.