/** * Creates a new AccountCode model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new AccountCode(); $model->isdel = 0; /* $get = Yii::$app->request->get(); if (isset($get['attributes'])) { $attr = $get['attributes']; if (isset($attr["error"])) { $model->addErrors($attr["error"]); } $model->attributes = $attr; } */ if ($model->load(Yii::$app->request->post())) { $transaction = Yii::$app->db->beginTransaction(); try { if ($model->parent_id == null && $model->code != 0) { $parent = AccountCode::findOne(["code" => 0]); if (!$parent) { $res = Yii::$app->db->createCommand("INSERT \n\t\t\t\t\t\t\tINTO " . AccountCode::tableName() . "\n\t\t\t\t\t\t\t(code,name,increaseon,id_left,id_right,id_level)\n\t\t\t\t\t\t\tVALUES (0,'" . Yii::t("app", "Base Account") . "',0,1,2,1)\n\t\t\t\t\t\t\t")->execute(); $parent = AccountCode::findOne(["code" => 0]); } $model->parent_id = $parent->id; } else { $parent = AccountCode::findOne(["id" => $model->parent_id]); } $model->prependTo($parent); if ($model->save()) { $transaction->commit(); return $this->redirect(['view', 'id' => $model->id]); } else { $transaction->rollBack(); } } catch (Exception $e) { $transaction->rollBack(); } } return $this->render('create', ['model' => $model]); }