php - Yii, Fatal error: Call to a member function addComment() on a non-object -
this question has answer here:
i new yii , while trying make blog application of own made function add comment post.
however, have done according theory still getting :
fatal error: call member function addcomment() on non-object in /htdocs/blog/protected/controllers/postcontroller.php on line 63 my post.php model class has function :
public function addcomment($comment){ $comment->tbl_post_id = $this->id; return $comment->save(); } and postcontroller.php has these 2 function, 1 creating comment , other 1 altering 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 line 63** { yii::app()->user->setflash('commentsubmitted',"your comment has been added." ); $this->refresh(); } } return $comment; } so logically calling member function addcomment inside post model class using $post->addcomment, , member functions of model initialized right? , logicaly shouldn't correct right? however, getting above fatal error.
what doing wrong?
any appreciated.
regards,
p.s - sorry if doing stupid.
$post isn't object since don't declare anywhere
protected function createcomment($issue) { $comment=new comment; if(isset($_post['comment'])) { $comment->attributes=$_post['comment']; if($post->addcomment($comment)) // **this line 63** { yii::app()->user->setflash('commentsubmitted',"your comment has been added." ); $this->refresh(); } } return $comment; } on line 63 he's searching variable named $post not exist. have create or load db you're doing actionview()
$post=$this->loadmodel($id); obviously load it, need $id has passed createcomment() function
Comments
Post a Comment