query = $this->buildQuery($dbQuery); list($this->tpl, $this->where, $this->data) = [$tpl, $where, $data]; $this->pkField = empty($pkField) ? ($this->query->getPk() ? $this->query->getPk() : 'id') : $pkField;; $this->pkValue = input($this->pkField, isset($data[$this->pkField]) ? $data[$this->pkField] : null); } /** * 逻辑器初始化 * @param Controller $controller * @param array $data * @return array|boolean * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public function init(Controller $controller, $data = []) { $this->controller = $controller; // GET请求, 获取数据并显示表单页面 if ($this->controller->request->isGet()) { if ($this->pkValue !== null) { $where = [$this->pkField => $this->pkValue]; $data = (array)$this->query->where($where)->where($this->where)->find(); } $data = array_merge($data, $this->data); if (false !== $this->controller->callback('_form_filter', $data)) { return $this->controller->fetch($this->tpl, ['vo' => $data]); } return $data; } // POST请求, 数据自动存库处理 if ($this->controller->request->isPost()) { $data = array_merge($this->controller->request->post(), $this->data); if (false !== $this->controller->callback('_form_filter', $data, $this->where)) { $result = Data::save($this->query, $data, $this->pkField, $this->where); if (false !== $this->controller->callback('_form_result', $result, $data)) { if ($result !== false) $this->controller->success('恭喜, 数据保存成功!', ''); $this->controller->error('数据保存失败, 请稍候再试!'); } return $result; } } } }