/** * Searching menu * @param array $params * @return \yii\data\ActiveDataProvider */ public function search($params) { $query = MenuModel::find()->from(MenuModel::tableName() . ' t')->joinWith(['menuParent' => function ($q) { $q->from(MenuModel::tableName() . ' parent'); }]); $dataProvider = new ActiveDataProvider(['query' => $query]); $sort = $dataProvider->getSort(); $sort->attributes['menuParent.name'] = ['asc' => ['parent.name' => SORT_ASC], 'desc' => ['parent.name' => SORT_DESC], 'label' => 'parent']; $sort->attributes['order'] = ['asc' => ['parent.order' => SORT_ASC, 't.order' => SORT_ASC], 'desc' => ['parent.order' => SORT_DESC, 't.order' => SORT_DESC], 'label' => 'order']; $sort->defaultOrder = ['menuParent.name' => SORT_ASC]; if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['t.id' => $this->id, 't.parent' => $this->parent]); $query->andFilterWhere(['like', 'lower(t.name)', strtolower($this->name)])->andFilterWhere(['like', 't.route', $this->route])->andFilterWhere(['like', 'lower(parent.name)', strtolower($this->parent_name)]); return $dataProvider; }
/** * Get menu children * @return \yii\db\ActiveQuery */ public function getMenus() { return $this->hasMany(Menu::className(), ['parent' => 'id']); }
/** * Finds the Menu model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Menu the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Menu::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; use hornetalcala\admin\models\Menu; use yii\helpers\Json; use hornetalcala\admin\AutocompleteAsset; /* @var $this yii\web\View */ /* @var $model hornetalcala\admin\models\Menu */ /* @var $form yii\widgets\ActiveForm */ AutocompleteAsset::register($this); $opts = Json::htmlEncode(['menus' => Menu::getMenuSource(), 'routes' => Menu::getSavedRoutes()]); $this->registerJs("var _opts = {$opts};"); $this->registerJs($this->render('_script.js')); ?> <div class="menu-form"> <?php $form = ActiveForm::begin(); ?> <?php echo Html::activeHiddenInput($model, 'parent', ['id' => 'parent_id']); ?> <div class="row"> <div class="col-sm-6"> <?php echo $form->field($model, 'name')->textInput(['maxlength' => 128]); ?> <?php echo $form->field($model, 'parent_name')->textInput(['id' => 'parent_name']);