From bce968f5a393770f905327052ac71f1f9e50fb85 Mon Sep 17 00:00:00 2001 From: molong Date: Fri, 5 Aug 2016 17:32:10 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E6=9B=B4=E6=96=B0tp5=E5=86=85?= =?UTF-8?q?=E6=A0=B8=202=E3=80=81=E4=BF=AE=E5=A4=8D=E5=90=8E=E5=8F=B0?= =?UTF-8?q?=E6=96=87=E6=A1=A3=E6=A8=A1=E5=9E=8B=E5=88=9B=E5=BB=BA=E7=9A=84?= =?UTF-8?q?bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/common/model/Content.php | 2 +- application/common/model/Document.php | 2 +- application/common/model/Model.php | 45 ++++---- core/library/think/App.php | 14 +-- core/library/think/Model.php | 21 +++- core/library/think/Url.php | 14 +-- core/library/think/db/Builder.php | 14 ++- core/library/think/db/Connection.php | 2 + core/library/think/db/Query.php | 131 +++++++++++++++++------ core/library/think/model/Relation.php | 8 +- core/library/think/view/driver/Think.php | 3 + public/js/core.js | 4 +- 12 files changed, 165 insertions(+), 95 deletions(-) diff --git a/application/common/model/Content.php b/application/common/model/Content.php index f775adf4..78175911 100644 --- a/application/common/model/Content.php +++ b/application/common/model/Content.php @@ -58,6 +58,6 @@ class Content extends Base{ } public function del($map){ - return $this->db()->where($map)->delete(); + return $this->where($map)->delete(); } } \ No newline at end of file diff --git a/application/common/model/Document.php b/application/common/model/Document.php index 458b8399..eeda771d 100644 --- a/application/common/model/Document.php +++ b/application/common/model/Document.php @@ -104,7 +104,7 @@ class Document extends \think\model\Merge{ } public function del($map){ - return $this->db()->where($map)->delete(); + return $this->where($map)->delete(); } public function detail($id){ diff --git a/application/common/model/Model.php b/application/common/model/Model.php index 48354dd7..81bb67d0 100644 --- a/application/common/model/Model.php +++ b/application/common/model/Model.php @@ -46,37 +46,34 @@ class Model extends Base{ */ public function change() { if(IS_POST){ - $data = input('post.',array()); + $data = \think\Request::instance()->post(); if($data){ if (empty($data['id'])) { /*创建表*/ $db = new \com\Datatable(); - //文档模型 - if($data['extend'] == 1){ - //默认文档前缀 - $tablename = 'document_'.$data['name']; - $is_auto_increment = false; + + if ($data['extend'] == 1) { + //文档模型 + $sql = $db->start_table('document_'.$data['name'])->create_id('doc_id', 11 , '主键' , false)->create_key('doc_id'); }else{ - $tablename = $data['name']; - $is_auto_increment = true; + $sql = $db->start_table($data['name'])->create_id('id', 11 , '主键' , true)->create_uid()->create_key('id'); } - $sql = $db->start_table($tablename) - ->create_id('id', 11 , '主键' , $is_auto_increment); - if ($data['extend'] != 1) { - $sql = $sql->create_uid(); - } - $sql->create_key('id')->end_table($data['title'], $data['engine_type']) - ->create(); - $id = $this->validate('model.add')->save($data); - if (false === $id) { - return array('info'=>$this->getError(), 'status'=>0); + //执行操作数据库,建立数据表 + $result = $sql->end_table($data['title'], $data['engine_type'])->create(); + if ($result) { + $id = $this->validate('model.add')->save($data); + if (false === $id) { + return array('info'=>$this->getError(), 'status'=>0); + }else{ + // 清除模型缓存数据 + cache('document_model_list', null); + + //记录行为 + action_log('update_model', 'model', $id, session('auth_user.uid')); + return $id ? array('info'=>'创建模型成功!','status'=>1) : array('info'=>'创建模型失败!','status'=>1); + } }else{ - // 清除模型缓存数据 - cache('document_model_list', null); - - //记录行为 - action_log('update_model', 'model', $id, session('auth_user.uid')); - return $id ? array('info'=>'创建模型成功!','status'=>1) : array('info'=>'创建模型失败!','status'=>1); + return false; } } else { //修改 diff --git a/core/library/think/App.php b/core/library/think/App.php index 30350e3a..1d272ac3 100644 --- a/core/library/think/App.php +++ b/core/library/think/App.php @@ -78,10 +78,6 @@ class App { is_null($request) && $request = Request::instance(); - if ('ico' == $request->ext()) { - throw new HttpException(404, 'ico file not exists'); - } - $config = self::initCommon(); $request->filter($config['default_filter']); try { @@ -483,10 +479,16 @@ class App // 开启路由 if (is_file(RUNTIME_PATH . 'route.php')) { // 读取路由缓存 - Route::rules(include RUNTIME_PATH . 'route.php' ?: []); + $rules = include RUNTIME_PATH . 'route.php'; + if (is_array($rules)) { + Route::rules($rules); + } } elseif (is_file(CONF_PATH . 'route' . CONF_EXT)) { // 导入路由配置 - Route::import(include CONF_PATH . 'route' . CONF_EXT ?: []); + $rules = include CONF_PATH . 'route' . CONF_EXT; + if (is_array($rules)) { + Route::import($rules); + } } // 路由检测(根据路由定义返回不同的URL调度) $result = Route::check($request, $path, $depr, $config['url_domain_deploy']); diff --git a/core/library/think/Model.php b/core/library/think/Model.php index 16db8797..12e34bf5 100644 --- a/core/library/think/Model.php +++ b/core/library/think/Model.php @@ -52,16 +52,16 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected $class; // 回调事件 private static $event = []; - - // 数据表主键 复合主键使用数组定义 - protected $pk; // 错误信息 protected $error; // 字段验证规则 protected $validate; - + // 数据表主键 复合主键使用数组定义 + protected $pk; // 字段属性 protected $field = []; + // 字段类型 + protected $fieldType = []; // 显示属性 protected $visible = []; // 隐藏属性 @@ -155,6 +155,19 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } else { $query->name($this->name); } + + if (!empty($this->field)) { + $query->allowField($this->field); + } + + if (!empty($this->fieldType)) { + $query->setFieldType($this->fieldType); + } + + if (!empty($this->pk)) { + $query->pk($this->pk); + } + // 全局作用域 if (method_exists($this, 'base')) { call_user_func_array([$this, 'base'], [ & $query]); diff --git a/core/library/think/Url.php b/core/library/think/Url.php index 2d2acb82..fcfd8b48 100644 --- a/core/library/think/Url.php +++ b/core/library/think/Url.php @@ -19,9 +19,6 @@ use think\Route; class Url { - // 生成URL地址的root - protected static $root; - /** * URL生成 支持路由反射 * @param string $url URL表达式, @@ -132,7 +129,7 @@ class Url // 检测域名 $domain = self::parseDomain($url, $domain); // URL组装 - $url = $domain . (self::$root ?: Request::instance()->root()) . '/' . ltrim($url, '/'); + $url = $domain . Request::instance()->root() . '/' . ltrim($url, '/'); return $url; } @@ -357,11 +354,4 @@ class Url { Cache::rm('think_route_map'); } - - // 指定当前生成URL地址的root - public static function root($root) - { - self::$root = $root; - Request::instance()->root($root); - } -} +} \ No newline at end of file diff --git a/core/library/think/db/Builder.php b/core/library/think/db/Builder.php index 1024521c..3ebdb692 100644 --- a/core/library/think/db/Builder.php +++ b/core/library/think/db/Builder.php @@ -83,7 +83,7 @@ abstract class Builder } // 获取绑定信息 - $bind = $this->query->getTableInfo($options['table'], 'bind'); + $bind = $this->query->getFieldsBind($options); if ('*' == $options['field']) { $fields = array_keys($bind); } else { @@ -228,8 +228,8 @@ abstract class Builder $whereStr = ''; // 获取字段信息 - $fields = $this->query->getTableInfo($options['table'], 'fields'); - $binds = $this->query->getTableInfo($options['table'], 'bind'); + $fields = $this->query->getTableFields($options); + $binds = $this->query->getFieldsBind($options); foreach ($where as $key => $val) { $str = []; foreach ($val as $field => $value) { @@ -369,10 +369,8 @@ abstract class Builder protected function parseDateTime($value, $key, $options = []) { // 获取时间字段类型 - $type = $this->query->getTableInfo($options['table'], 'type'); - if (isset($options['field_type'][$key])) { - $info = $options['field_type'][$key]; - } elseif (isset($type[$key])) { + $type = $this->query->getFieldsType($options); + if (isset($type[$key])) { $info = $type[$key]; } if (isset($info)) { @@ -608,7 +606,7 @@ abstract class Builder { // 获取合法的字段 if ('*' == $options['field']) { - $fields = $this->query->getTableInfo($options['table'], 'fields'); + $fields = $this->query->getFieldsType($options); } else { $fields = $options['field']; } diff --git a/core/library/think/db/Connection.php b/core/library/think/db/Connection.php index b92aea6d..6712e0d9 100644 --- a/core/library/think/db/Connection.php +++ b/core/library/think/db/Connection.php @@ -105,6 +105,8 @@ abstract class Connection 'auto_timestamp' => false, // 是否需要进行SQL性能分析 'sql_explain' => false, + // Builder类 + 'builder' => '', ]; // PDO连接参数 diff --git a/core/library/think/db/Query.php b/core/library/think/db/Query.php index 33d2d1f6..d0aa4f1f 100644 --- a/core/library/think/db/Query.php +++ b/core/library/think/db/Query.php @@ -34,7 +34,7 @@ class Query // 数据库Connection对象实例 protected $connection; // 数据库驱动类型 - protected $driver; + protected $builder; // 当前模型类名称 protected $model; // 当前数据表名称(含前缀) @@ -59,7 +59,7 @@ class Query public function __construct(Connection $connection = null, $model = '') { $this->connection = $connection ?: Db::connect([], true); - $this->driver = $this->connection->getConfig('type'); + $this->builder = $this->connection->getConfig('builder') ?: $this->connection->getConfig('type'); $this->prefix = $this->connection->getConfig('prefix'); $this->model = $model; } @@ -357,7 +357,7 @@ class Query protected function builder() { static $builder = []; - $driver = $this->driver; + $driver = $this->builder; if (!isset($builder[$driver])) { $class = false !== strpos($driver, '\\') ? $driver : '\\think\\db\\builder\\' . ucfirst($driver); $builder[$driver] = new $class($this->connection); @@ -720,11 +720,11 @@ class Query } if (true === $field) { // 获取全部字段 - $fields = $this->getTableInfo($tableName, 'fields'); + $fields = isset($this->options['allow_field']) ? $this->options['allow_field'] : $this->getTableInfo($tableName, 'fields'); $field = $fields ?: ['*']; } elseif ($except) { // 字段排除 - $fields = $this->getTableInfo($tableName, 'fields'); + $fields = isset($this->options['allow_field']) ? $this->options['allow_field'] : $this->getTableInfo($tableName, 'fields'); $field = $fields ? array_diff($fields, $field) : $field; } if ($tableName) { @@ -1235,6 +1235,47 @@ class Query return $this; } + /** + * 设置数据表字段 + * @access public + * @param string|array $field 字段信息 + * @return $this + */ + public function allowField($field) + { + if (true === $field) { + $field = $this->getTableInfo('', 'fields'); + } elseif (is_string($field)) { + $field = explode(',', $field); + } + $this->options['allow_field'] = $field; + return $this; + } + + /** + * 设置字段类型 + * @access public + * @param array $fieldType 字段类型信息 + * @return $this + */ + public function setFieldType($fieldType = []) + { + $this->options['field_type'] = $fieldType; + return $this; + } + + /** + * 指定数据表主键 + * @access public + * @param string $pk 主键 + * @return $this + */ + public function pk($pk) + { + $this->options['pk'] = $pk; + return $this; + } + /** * 查询日期或者时间 * @access public @@ -1284,18 +1325,6 @@ class Query return $this; } - /** - * 设置字段类型 - * @access public - * @param array $fieldType 字段类型信息 - * @return $this - */ - public function setFieldType($fieldType = []) - { - $this->options['field_type'] = $fieldType; - return $this; - } - /** * 获取数据表信息 * @access public @@ -1327,13 +1356,7 @@ class Query foreach ($info as $key => $val) { // 记录字段类型 $type[$key] = $val['type']; - if (preg_match('/(int|double|float|decimal|real|numeric|serial)/is', $val['type'])) { - $bind[$key] = PDO::PARAM_INT; - } elseif (preg_match('/bool/is', $val['type'])) { - $bind[$key] = PDO::PARAM_BOOL; - } else { - $bind[$key] = PDO::PARAM_STR; - } + $bind[$key] = $this->getFieldBindType($val['type']); if (!empty($val['primary'])) { $pk[] = $key; } @@ -1352,12 +1375,60 @@ class Query /** * 获取当前数据表的主键 * @access public - * @param string $table 数据表名 + * @param string|array $options 数据表名或者查询参数 * @return string|array */ - public function getPk($table = '') + public function getPk($options = '') { - return $this->getTableInfo($table, 'pk'); + if (!empty($options['pk'])) { + $pk = $options['pk']; + } elseif (isset($this->options['pk'])) { + $pk = $this->options['pk']; + } else { + $pk = $this->getTableInfo(is_array($options) ? $options['table'] : $options, 'pk'); + } + return $pk; + } + + // 获取当前数据表字段信息 + public function getTableFields($options) + { + return !empty($options['allow_field']) ? $options['allow_field'] : $this->getTableInfo($options['table'], 'fields'); + } + + // 获取当前数据表字段类型 + public function getFieldsType($options) + { + return !empty($options['field_type']) ? $options['field_type'] : $this->getTableInfo($options['table'], 'type'); + } + + // 获取当前数据表绑定信息 + public function getFieldsBind($options) + { + $types = $this->getFieldsType($options); + $bind = []; + foreach ($types as $key => $type) { + $bind[$key] = $this->getFieldBindType($type); + } + return $bind; + } + + /** + * 获取字段绑定类型 + * @access public + * @param string $type 字段类型 + * @return integer + */ + protected function getFieldBindType($type) + { + if (preg_match('/(int|double|float|decimal|real|numeric|serial)/is', $type)) { + $bind = PDO::PARAM_INT; + } elseif (preg_match('/bool/is', $type)) { + $bind = PDO::PARAM_BOOL; + } else { + $bind = PDO::PARAM_STR; + } + return $bind; } /** @@ -1546,7 +1617,7 @@ class Query */ protected function parsePkWhere($data, &$options) { - $pk = $this->getPk($options['table']); + $pk = $this->getPk($options); // 获取当前数据表 if (!empty($options['alias'])) { $alias = $options['alias']; @@ -1684,7 +1755,7 @@ class Query { $options = $this->parseExpress(); if (empty($options['where'])) { - $pk = $this->getPk($options['table']); + $pk = $this->getPk($options); // 如果存在主键数据 则自动作为更新条件 if (is_string($pk) && isset($data[$pk])) { $where[$pk] = $data[$pk]; @@ -1951,8 +2022,8 @@ class Query */ public function chunk($count, $callback, $column = null) { - $column = $column ?: $this->getPk(); $options = $this->getOptions(); + $column = $column ?: ($options['pk'] ?: $this->getPk()); $bind = $this->bind; $resultSet = $this->limit($count)->order($column, 'asc')->select(); diff --git a/core/library/think/model/Relation.php b/core/library/think/model/Relation.php index 94525f76..6b1faa32 100644 --- a/core/library/think/model/Relation.php +++ b/core/library/think/model/Relation.php @@ -93,7 +93,7 @@ class Relation $result = $relation->where($localKey, $this->parent->$foreignKey)->find(); break; case self::HAS_MANY: - $result = $relation->select(); + $result = $relation->where($foreignKey, $this->parent->$localKey)->select(); break; case self::HAS_MANY_THROUGH: $result = $relation->select(); @@ -659,12 +659,6 @@ class Relation { if ($this->query) { switch ($this->type) { - case self::HAS_MANY: - if (isset($this->parent->{$this->localKey})) { - // 关联查询带入关联条件 - $this->query->where($this->foreignKey, $this->parent->{$this->localKey}); - } - break; case self::HAS_MANY_THROUGH: $through = $this->middle; $model = $this->model; diff --git a/core/library/think/view/driver/Think.php b/core/library/think/view/driver/Think.php index 264102b2..f550e915 100644 --- a/core/library/think/view/driver/Think.php +++ b/core/library/think/view/driver/Think.php @@ -39,6 +39,9 @@ class Think if (empty($this->config['view_path'])) { $this->config['view_path'] = App::$modulePath . 'view' . DS; } + if (App::$debug) { + $this->config['tpl_cache'] = false; + } $this->template = new Template($this->config); } diff --git a/public/js/core.js b/public/js/core.js index 7bf5d6a4..6a2d270d 100644 --- a/public/js/core.js +++ b/public/js/core.js @@ -222,7 +222,7 @@ $(function() { } else { updateAlert(data.msg); setTimeout(function() { - location.reload(); + //location.reload(); // if (data.url) { // location.href = data.url; // } else { @@ -305,7 +305,7 @@ $(function() { updateAlert(data.msg, 'danger'); setTimeout(function() { $(that).removeClass('disabled').prop('disabled', false); - location.reload(); + //location.reload(); // if (data.url) { // location.href = data.url; // } else {