コード例 #1
0
 /**
  * Creates a new Menu model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Menu();
     if ($model->load(Yii::$app->request->post())) {
         //post
         $post = Yii::$app->request->post();
         $model->created_at = time();
         $model->created_by = Yii::$app->user->id;
         $transaction = \Yii::$app->db->beginTransaction();
         try {
             if ($flag = $model->save(false)) {
                 $title = $post['Menu']['items'];
                 if ($title) {
                     MenuAuth::deleteAll(['menu_id' => $model->id]);
                     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($articleTag->getErrors());
                         }
                     }
                 }
             } else {
                 print_r($model->getError());
                 exit;
             }
             if ($flag) {
                 $transaction->commit();
                 return $this->redirect(['view', 'id' => $model->id]);
             }
         } catch (Exception $e) {
             $transaction->rollBack();
         }
     }
     return $this->render('create', ['model' => $model]);
 }