public function create() { if (!Yii::$app->user->isGuest) { $this->created_by = Yii::$app->user->id; } if ($this->validate()) { return Yii::$app->db->transaction(function () { $comment = new Comment(); $comment->setAttributes($this->attributes, false); if (empty($this->parent_id)) { $comment->level = 0; } else { $comment->level = Comment::findOne(['id' => $this->parent_id])->level; $comment->level++; } $comment->status = 1; $comment->created_at = (new \DateTime())->format('Y-m-d H:i:s'); $comment->save(false); return true; }); } return false; }
/** * Finds the Comment model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Comment the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Comment::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }