hi, this is like post and comments, in blog demo is some example
post Model
.............
/**
* Adds a new comment to this post.
* This method will set status and post_id of the comment accordingly.
* @param Comment the comment to be added
* @return boolean whether the comment is saved successfully
*/
public function addComment($comment)
{
if(Yii::app()->params['commentNeedApproval'])
$comment->status=Comment::STATUS_PENDING;
else
$comment->status=Comment::STATUS_APPROVED;
$comment->post_id=$this->id;
return $comment->save();
}
..........
post controller
.................
/**
* Displays a particular model.
*/
public function actionView()
{
$post=$this->loadModel();
$comment=$this->newComment($post);
$this->render('view',array(
'model'=>$post,
'comment'=>$comment,
));
}
.................
/**
* Creates a new comment.
* This method attempts to create a new comment based on the user input.
* If the comment is successfully created, the browser will be redirected
* to show the created comment.
* @param Post the post that the new comment belongs to
* @return Comment the comment instance
*/
protected function newComment($post)
{
$comment=new Comment;
if(isset($_POST['ajax']) && $_POST['ajax']==='comment-form')
{
echo CActiveForm::validate($comment);
Yii::app()->end();
}
if(isset($_POST['Comment']))
{
$comment->attributes=$_POST['Comment'];
if($post->addComment($comment))
{
if($comment->status==Comment::STATUS_PENDING)
Yii::app()->user->setFlash('commentSubmitted','Thank you for your comment. Your comment will be posted once it is approved.');
$this->refresh();
}
}
return $comment;
}