protected function findModel($id) { if (($model = Category::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function saveNode() { if ($this->validate()) { try { if ($this->parent_id == 0) { $this->makeRoot(); } else { $this->prependTo(Category::findOne(['id' => $this->parent_id])); } } catch (Exception $e) { return false; } return true; } return false; }
public static function getTree($id = 0) { $exceptionId = []; if ($id != 0) { $exceptionNode = \backend\modules\category\models\db\Category::findOne(['id' => $id]); $exceptionId[] = $id; $exceptionNodes = $exceptionNode->children()->all(); foreach ($exceptionNodes as $node) { $exceptionId[] = $node->id; } } $models = Category::find()->orderBy(['tree' => SORT_ASC, 'lft' => SORT_ASC])->where(['not in', 'id', $exceptionId])->all(); $arr = [0 => 'Корневая']; foreach ($models as $model) { $arr[$model->id] = str_repeat(" ", $model->lvl) . $model->name; } return $arr; }