/** * Creates a new Category model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate($p = null) { $parent = $p; $model = new Category(); $data = Yii::$app->request->post(); if ($model->load($data)) { $res = null; if (isset($data['Category']['parentid'])) { $parent = $data['Category']['parentid']; } $parent = Category::findOne(intval($parent)); if ($parent) { $res = $model->appendTo($parent); } else { $res = $model->makeRoot(); } Module::checkPathes(); if ($res) { return $this->redirect(['view', 'id' => $model->id]); } } else { $model->parentid = intval($parent); return $this->render('create', ['model' => $model]); } }
/** * Check pathes * @param integer $limit */ public static function checkPathes($limit = 0) { $module = \Yii::$app->getModule('articles'); $count = Category::find()->count(); if ($count < 1) { $category = new Category(); $category->title = $module->title; $category->name = $module->id; $category->genPath(); $category->makeRoot(); \Yii::$app->settings->set('articles.root', $category->id); } $categories = Category::find()->where(['path' => NULL])->all(); foreach ($categories as $category) { $category->scenario = 'check'; $category->save(); } $articles = Article::find()->where(['path' => NULL])->all(); foreach ($articles as $article) { $article->save(); } }