예제 #1
0
 public function actionPost($key)
 {
     $post = $this->getPost($key);
     $commentForm = null;
     if ($post->comments_enabled) {
         $item = Yii::createObject(['class' => Comment::className()]);
         $commentForm = Yii::createObject(['class' => Comment::className(), 'scenario' => 'create', 'item' => $item]);
         $commentForm->setAttributes(['created_by' => Yii::$app->user->id, 'post_id' => $post->id], false);
         $this->performAjaxValidation($commentForm);
         if ($commentForm->load(Yii::$app->request->post()) && $commentForm->save()) {
             return $this->refresh();
         }
     }
     return $this->render('post', ['post' => $post, 'commentForm' => $commentForm]);
 }
예제 #2
0
파일: Post.php 프로젝트: jarrus90/yii2-blog
 public function getComments()
 {
     return $this->hasMany(Comment::className(), ['post_id' => 'id']);
 }