public function actionDeleteMenuItem() { $id = \Yii::$app->request->post('id'); $db = \Yii::$app->getDb(); $menuItem = MenuItems::find()->where(['id' => $id])->one(); if ($menuItem != null) { $menuItem->delete(); return []; } else { throw new BusinessException("Menu item does not exist"); } }
/** * Finds the MenuItems model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return MenuItems the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = MenuItems::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
echo $form->field($model, 'name')->textInput(['maxlength' => 255]); ?> <?php echo $form->field($model, 'url')->textInput(['maxlength' => 255]); ?> <?php echo $form->field($model, 'menu_id')->hiddenInput(['value' => $menu_id])->label(''); ?> <div class="form-group"> <label>Родительский пункт: </label> <?php echo Html::activeDropDownList($model, 'parent_id', \yii\helpers\ArrayHelper::map(\app\models\MenuItems::find(['active' => true])->where('id != :model_id', [':model_id' => $model->id ? $model->id : 0])->andWhere('menu_id = :menu_id', [':menu_id' => $menu_id])->all(), 'id', 'name'), ['class' => 'form-control', 'prompt' => 'Выберите родительский пункт меню...']); ?> </div> <div class="form-group"> <?php echo Html::submitButton($model->isNewRecord ? Yii::t('admin', 'Create') : Yii::t('admin', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']); ?> </div> <?php ActiveForm::end(); ?> </div>
/** * @return \yii\db\ActiveQuery */ public function getMenuItems() { return $this->hasMany(MenuItems::className(), ['menu_id' => 'id']); }