コード例 #1
0
 public function save()
 {
     $id = (int) Input::get('id', 0);
     $params = Input::all();
     $params['sort'] = intval(Input::get('sort', 0));
     unset($params['id']);
     if (empty($params['name'])) {
         $this->_fail('名称必须填写');
     }
     $source = [];
     if (isset($params['source'])) {
         $source = $params['source'];
         unset($params['source']);
     }
     if (isset($params['is_front'])) {
         unset($params['is_front']);
     }
     if (isset($params['front_index'])) {
         $front_index = $params['front_index'];
         unset($params['front_index']);
     }
     try {
         $model = new TemplateModel();
         if ($id == 0) {
             $r = $model->insert($params);
         } else {
             $r = $model->update($id, $params);
         }
         $template_id = $r->id;
         if (!empty($source)) {
             TemplateSourceORM::whereTemplateId($template_id)->delete();
             foreach ($source as $k => $s) {
                 $ins = [];
                 $ins['source'] = $s;
                 $ins['template_id'] = $template_id;
                 if ($k == $front_index) {
                     $ins['is_front'] = BaseORM::ENABLE;
                 }
                 $m = new TemplateSourceModel();
                 $m->insert($ins);
             }
         }
         $this->_succ('保存成功', URL::route('templateLists'));
     } catch (Exception $e) {
         $this->_fail('保存失败');
     }
 }