/** * Creates a new Category model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $this->setMenus(30, "添加新分类"); $model = new Category(); $id = LsYii::getGetValue('id', null); if ($id) { $model->pid = $id; } if ($model->load(LsYii::getRequest()->post())) { $pid = $model->pid; if ($pid) { $c = Category::findOne(['`id`' => $pid]); $rgt = $c->rgt; $model->root = $c->root; $model->pid = $c->id; $model->level = $c->level + 1; $model->lft = $rgt; $model->rgt = $rgt + 1; } else { $maxRgt = Category::getMaxRgt(); $model->lft = $maxRgt + 1; $model->rgt = $maxRgt + 2; $model->pid = 0; $model->level = 1; } if ($model->save()) { if (!$pid) { $menu = Category::findOne(['`id`' => $model->id]); $menu->root = $model->id; $menu->save(); } else { Category::updateAllCounters(['rgt' => 2], "`lft`<{$rgt} AND `rgt`>={$rgt}"); Category::updateAllCounters(['rgt' => 2, 'lft' => 2], "`lft`>{$rgt}"); } } return $this->redirect(['index']); } else { $model->if_show = \source\libs\Constants::Category_Status_Show; return $this->render('create', ['model' => $model]); } }