In the tutorial, specifically this page http://www.yiiframework.com/doc/blog/1.1/en/comment.create#displaying-comments
The author writes
Why put the comment code in the post section? I understand the reasoning, but couldn’t one put the actual comment functions addComment() and newComment() in the Comment model/controller and then just reference them accordingly in the Post model/controller classes?
I suppose you’d need to create references to the comment instance properly and it could be a little cumbersome, but at least you’d separate your Post code from your Comment code?
Or, is it common practice to have a mother class handle issues with the child functionality. I.e., mother being "post" and child being "comments"
For reference:
<?php
protected function newComment($post) { // controllers\PostController.php
$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;
}
public function addComment($comment) { // models\Post.php
if (Yii::app()->params['commentNeedApproval'])
$comment->status=Comment::STATUS_PENDING;
else
$comment->status=Comment::STATUS_APPROVED;
$comment->post_id=$this->id;
return $comment->save();
}
?>