public function actionView() { $id = Yii::$app->request->get('id'); $game = Game::findOne($id); $comments = Comment::find()->orderBy('id DESC')->where(['game_id' => $id])->all(); $commentForm = new CommentForm(); $commentForm->game_id = $id; if (!Yii::$app->user->isGuest && $commentForm->load(Yii::$app->request->post()) && $commentForm->save()) { $this->refresh(); } return $this->render('view', ['game' => $game, 'commentForm' => $commentForm, 'comments' => $comments]); }
public function save() { if ($this->validate()) { $comment = new Comment(); $comment->game_id = $this->game_id; //if author_id have a value if ($this->author_id) { $comment->author_id = $this->author_id; } else { $comment->author_id = Yii::$app->user->getId(); } $comment->title = $this->title; $comment->content = $this->content; $comment->created_at = time(); $comment->updated_at = time(); if ($comment->save()) { return true; } else { return false; } } else { return false; } }