/** * Creates a new Comment model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Comment(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
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; }