model = new \app\admin\model\Operation; } /** * 若需重写查看、编辑、删除等方法,请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写 */ /** * 编辑 * @throws Throwable */ public function edit(): void { $pk = $this->model->getPk(); $id = $this->request->param($pk); $row = $this->model->find($id); if (!$row) { $this->error(__('Record not found')); } $dataLimitAdminIds = $this->getDataLimitAdminIds(); if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) { $this->error(__('You have no permission')); } if ($this->request->isPost()) { $data = $this->request->post(); if (!$data) { $this->error(__('Parameter %s can not be empty', [''])); } if( $data['switch'] == 0 && !isset($data['types'])){ $this->error('请确保至少有一种协议正在使用中!'); } $data = $this->excludeFields($data); $result = false; $this->model->startTrans(); try { // 模型验证 if ($this->modelValidate) { $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model)); if (class_exists($validate)) { $validate = new $validate; if ($this->modelSceneValidate) $validate->scene('edit'); $data[$pk] = $row[$pk]; $validate->check($data); } } if( $data['switch'] == 1 && !isset($data['types'])){ $this->model->where('switch',1)->update(['switch' => 0]); } $result = $row->save($data); $this->model->commit(); } catch (Throwable $e) { $this->model->rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(__('Update successful')); } else { $this->error(__('No rows updated')); } } $this->success('', [ 'row' => $row ]); } }