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]); }
public function getComments() { return $this->hasMany(Comment::className(), ['post_id' => 'id']); }