Exemplo n.º 1
0
 /**
  * Updates an existing Menu model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $model->items = $model->itemAll;
     if ($model->load(Yii::$app->request->post())) {
         $post = Yii::$app->request->post();
         $model->created_at = time();
         $model->created_by = Yii::$app->user->id;
         $model->params = $post['Menu']['params'] ? Json::decode($post['Menu']['params']) : null;
         $transaction = \Yii::$app->db->beginTransaction();
         try {
             if ($flag = $model->save(false)) {
                 $title = $post['Menu']['items'];
                 MenuAuth::deleteAll(['menu_id' => $model->id]);
                 if ($title) {
                     foreach ($title as $key => $val) {
                         $menuAuth = new MenuAuth();
                         $menuAuth->menu_id = $model->id;
                         $menuAuth->item_name = $val;
                         if (($flag = $menuAuth->save(false)) === false) {
                             $transaction->rollBack();
                             break;
                         } else {
                             print_r($menuAuth->getErrors());
                         }
                     }
                 }
             } else {
                 print_r($model->getError());
                 exit;
             }
             if ($flag) {
                 $transaction->commit();
                 return $this->redirect(['view', 'id' => $model->id]);
             }
         } catch (Exception $e) {
             $transaction->rollBack();
         }
     }
     $model->params = $model->params ? Json::encode($model->params) : null;
     return $this->render('update', ['model' => $model]);
 }
Exemplo n.º 2
0
 public static function getItemsListDistinct()
 {
     //$model = $this->menuAuth->distinct;
     $model = MenuAuth::find()->select('item_name')->distinct()->all();
     return ArrayHelper::map($model, 'item_name', 'item_name');
 }