public function actionCreate() { $model = new Category(); if ($model->load(Yii::$app->request->post()) && $model->validate()) { if ($model->parent == 0) { $model->saveNode(); } elseif ($model->parent) { $root = Category::find($model->parent); $model->appendTo($root); } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Creates a new Category model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Category(); if ($model->load(Yii::$app->request->post()) && $model->validate()) { if ($model->parent == 0) { $model->saveNode(); } else { if ($model->parent) { $root = $this->findModel($model->parent); $model->appendTo($root); } } return $this->render('tree'); } else { return $this->render('create', ['model' => $model]); } }
/** * @param $nodes * @param null $parent */ protected static function buildCategoryTree($nodes, $parent = null) { foreach ($nodes as $node) { $category = new Category(['name' => $node['name'], 'announce' => 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.<br>Sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.']); if ($parent === null) { $category->makeRoot(); } else { $category->appendTo($parent); } $category->attachImage(__DIR__ . '/images/categories/' . $category->id . '.jpg'); if (isset($node['nodes'])) { self::buildCategoryTree($node['nodes'], $category); } } }