Esempio n. 1
0
 public function actionTabcreate($menuId)
 {
     $model = new QueryTableForm();
     $jsonData = Yii::$app->request->post()['json'];
     $attributes = json_decode($jsonData, true);
     //TODO 检查表,字段名的正确性
     $transaction = Yii::$app->db->beginTransaction();
     try {
         foreach ($attributes as $object) {
             //TODO 下拉的默认值bug,要找到修复方法
             //默认副表
             if ($object['isMain'] == "") {
                 $object['isMain'] = IS_MAIN_FALSE;
             }
             $object['menuId'] = $menuId;
             if (isset($object['addFlag'])) {
                 $model->scenario = 'create';
                 $model->setAttributes($object);
                 $model->isNewRecord = true;
                 if (!$model->save()) {
                     throw new Exception();
                 }
             } else {
                 $model->scenario = 'create';
                 $model->setAttributes($object);
                 if (QueryTableForm::updateAll($model->attributes, ['id' => $object['id']]) == false) {
                     throw new Exception("更新失败");
                 }
             }
         }
         $transaction->commit();
         $data = ['statusCode' => 200, 'closeCurrent' => true, 'message' => '保存成功'];
     } catch (Exception $e) {
         $transaction->rollBack();
         YII::error($e->getMessage());
         $data = ['statusCode' => 300, 'message' => '保存失败'];
     }
     return Json::encode($data);
 }