model = new StockConfigModel(); } /** * 添加 */ #[Route('GET,POST','add')] public function add() { if (false === $this->request->isPost()) { return $this->fetch(); } $params = array_merge($this->request->post("row/a"),$this->postParams); if (empty($params)) { $this->error(__('提交的参数不能为空')); } if(!$this->request->checkToken('__token__',['__token__'=>$this->request->post('__token__')])){ $this->error(__('token错误,请刷新页面重试')); } if($this->model::where('field_name', $params['field_name'])->count() >0) $this->error(__('库存唯一属性已存在')); foreach ($params as &$value){ if(is_array($value)){ $value=implode(',',$value); } if($value===''){ $value=null; } } $result = false; Db::startTrans(); try { $result = $this->model->save($params); if($this->callback){ $callback=$this->callback; $callback($this->model); } Db::commit(); } catch (\Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result === false) { $this->error(__('没有新增任何数据')); } $this->success(); } /** * 编辑 */ #[Route('GET,POST','edit')] public function edit(mixed $row=null) { $ids = $this->request->get('ids'); if(!$row || is_array($row)){ $row = $this->model->find($ids); } if (!$row) { $this->error(__('没有找到记录')); } if(count($this->volidateFields)>0){ foreach ($this->volidateFields as $field=>$value){ if($row[$field]!=$value){ $this->error(__('没有操作权限')); } } } if (false === $this->request->isPost()) { $this->assign('row', $row); return $this->fetch(); } $params = array_merge($this->request->post("row/a"),$this->postParams); if (empty($params)) { $this->error(__('提交的参数不能为空')); } if(!$this->request->checkToken('__token__',['__token__'=>$this->request->post('__token__')])){ $this->error(__('token错误,请刷新页面重试')); } if($this->model::where('field_name', $params['field_name'])->count() >1) $this->error(__('库存唯一属性已存在')); foreach ($params as &$value){ if(is_array($value)){ $value=implode(',',$value); } if($value===''){ $value=null; } } $result = false; Db::startTrans(); try { $result = $row->save($params); if($this->callback){ $callback=$this->callback; $callback($row); } Db::commit(); } catch (\Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if (false === $result) { $this->error(__('没有数据被更新')); } $this->success(); } }