[更新]基础代码整理

This commit is contained in:
Anyon 2017-05-18 15:06:12 +08:00
parent 45cfb78ed7
commit 31777376d8

View File

@ -52,42 +52,32 @@ class BasicAdmin extends Controller {
/** /**
* 表单默认操作 * 表单默认操作
* @param Query $db 数据库查询对象 * @param Query $dbQuery 数据库查询对象
* @param string $tpl 显示模板名字 * @param string $tplFile 显示模板名字
* @param string $pk 更新主键规则 * @param string $pkField 更新主键规则
* @param array $where 查询规则 * @param array $where 查询规则
* @param array $data 扩展数据 * @param array $data 扩展数据
* @return array|string * @return array|string
*/ */
protected function _form($db = null, $tpl = null, $pk = null, $where = [], $data = []) { protected function _form($dbQuery = null, $tplFile = '', $pkField = null, $where = [], $data = []) {
if (is_null($db)) { $db = is_null($dbQuery) ? Db::name($this->table) : (is_string($dbQuery) ? Db::name($dbQuery) : $dbQuery);
$db = Db::name($this->table); $pk = is_null($pkField) ? ($db->getPk() ? $db->getPk() : 'id') : $pkField;
} elseif (is_string($db)) { $pkValue = $this->request->request($pk, isset($where[$pk]) ? $where[$pk] : (isset($data[$pk]) ? $data[$pk] : null));
$db = Db::name($db); // POST请求, 数据自动存库
} if ($this->request->isPost()) {
if (is_null($pk)) { $data = array_merge($this->request->post(), $data);
$pk = $db->getPk(); if (false !== $this->_callback('_form_filter', $data)) {
} $result = DataService::save($db, $data, $pk, $where);
$pk_value = input($pk, isset($where[$pk]) ? $where[$pk] : (isset($data[$pk]) ? $data[$pk] : '')); (false !== $this->_callback('_form_result', $result)) && ($result !== false ? $this->success('恭喜, 数据保存成功!', '') : $this->error('数据保存失败, 请稍候再试!'));
$vo = $data;
if ($this->request->isPost()) { // Save Options
$vo = array_merge(input('post.'), $data);
$this->_callback('_form_filter', $vo);
$result = DataService::save($db, $vo, $pk, $where);
if (false !== $this->_callback('_form_result', $result)) {
$result !== false ? $this->success('恭喜,保存成功哦!', '') : $this->error('保存失败,请稍候再试!');
} }
return $result; return $result;
} }
if ($pk_value !== '') { // Edit Options // GET请求, 获取并显示表单页面
!empty($pk_value) && $db->where($pk, $pk_value); $vo = ($pkValue !== null) ? array_merge((array) $db->where($pk, $pkValue)->where($where)->find(), $data) : $data;
!empty($where) && $db->where($where); if (false !== $this->_callback('_form_filter', $vo)) {
$vo = array_merge($data, (array)$db->find()); return $this->fetch($tplFile, ['title' => $this->title, 'vo' => $vo]);
} }
$this->_callback('_form_filter', $vo); return $vo;
$this->assign('vo', $vo);
empty($this->title) or $this->assign('title', $this->title);
return is_null($tpl) ? $vo : $this->fetch($tpl);
} }
/** /**