protected function findModel($id) { if (($model = Category::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function search($params) { $query = Category::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['tree' => SORT_ASC, 'lft' => SORT_ASC]]]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['like', 'name', $this->name]); return $dataProvider; }
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; }
<div class="user-form"> <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?> <div class="row"> <div class="col-xs-6 form-group"> <?php echo $form->field($model, 'name')->textInput(['maxlength' => true]); ?> </div> <div class="col-xs-6 form-group"> <?php echo $form->field($model, 'parent_id')->dropDownList(\backend\modules\category\models\db\Category::getTree($model->isNewRecord ? 0 : $model->id), ['encode' => false]); ?> </div> </div> <? //= $form->field($model, 'salt')->textInput(['maxlength' => true]) ?> <? //= $form->field($model, 'time_create')->textInput() ?> <? //= $form->field($model, 'time_update')->textInput() ?> <? //= $form->field($model, 'verification_code')->textInput(['maxlength' => true]) ?> <? //= $form->field($model, 'status')->textInput(['maxlength' => true]) ?> <div class="form-group">