user = auth_user(); } public function add() { $result = false; $params = $this->request->param(); Db::startTrans(); try { $result = $this->model->allowField(true)->save($params); Db::commit(); } catch (ValidateException | PDOException | Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result === false) { $this->error('操作失败'); } $this->success('Success'); } public function update() { $result = false; $params = $this->request->param(); $model = $this->model->get($params['id']); if (empty($model)) { $this->error(__('No rows were found')); } Db::startTrans(); try { $result = $model->allowField(true)->save($params); Db::commit(); } catch (ValidateException | PDOException | Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result === false) { $this->error('操作失败'); } $this->success('Success'); } public function view() { $model = $this->model->get($this->request->param('id')); if (empty($model)) { $this->error(__('No rows were found')); } $this->success('Success', $model); } }