1、更新tp5内核

2、修复后台文档模型创建的bug
This commit is contained in:
2016-08-05 17:32:10 +08:00
parent 4b09ebce88
commit bce968f5a3
12 changed files with 165 additions and 95 deletions
+1 -1
View File
@@ -58,6 +58,6 @@ class Content extends Base{
} }
public function del($map){ public function del($map){
return $this->db()->where($map)->delete(); return $this->where($map)->delete();
} }
} }
+1 -1
View File
@@ -104,7 +104,7 @@ class Document extends \think\model\Merge{
} }
public function del($map){ public function del($map){
return $this->db()->where($map)->delete(); return $this->where($map)->delete();
} }
public function detail($id){ public function detail($id){
+23 -26
View File
@@ -46,37 +46,34 @@ class Model extends Base{
*/ */
public function change() { public function change() {
if(IS_POST){ if(IS_POST){
$data = input('post.',array()); $data = \think\Request::instance()->post();
if($data){ if($data){
if (empty($data['id'])) { if (empty($data['id'])) {
/*创建表*/ /*创建表*/
$db = new \com\Datatable(); $db = new \com\Datatable();
//文档模型
if($data['extend'] == 1){
//默认文档前缀
$tablename = 'document_'.$data['name'];
$is_auto_increment = false;
}else{
$tablename = $data['name'];
$is_auto_increment = true;
}
$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);
}else{
// 清除模型缓存数据
cache('document_model_list', null);
//记录行为 if ($data['extend'] == 1) {
action_log('update_model', 'model', $id, session('auth_user.uid')); //文档模型
return $id ? array('info'=>'创建模型成功!','status'=>1) : array('info'=>'创建模型失败!','status'=>1); $sql = $db->start_table('document_'.$data['name'])->create_id('doc_id', 11 , '主键' , false)->create_key('doc_id');
}else{
$sql = $db->start_table($data['name'])->create_id('id', 11 , '主键' , true)->create_uid()->create_key('id');
}
//执行操作数据库,建立数据表
$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{
return false;
} }
} else { } else {
//修改 //修改
+8 -6
View File
@@ -78,10 +78,6 @@ class App
{ {
is_null($request) && $request = Request::instance(); is_null($request) && $request = Request::instance();
if ('ico' == $request->ext()) {
throw new HttpException(404, 'ico file not exists');
}
$config = self::initCommon(); $config = self::initCommon();
$request->filter($config['default_filter']); $request->filter($config['default_filter']);
try { try {
@@ -483,10 +479,16 @@ class App
// 开启路由 // 开启路由
if (is_file(RUNTIME_PATH . 'route.php')) { 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)) { } 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调度) // 路由检测(根据路由定义返回不同的URL调度)
$result = Route::check($request, $path, $depr, $config['url_domain_deploy']); $result = Route::check($request, $path, $depr, $config['url_domain_deploy']);
+17 -4
View File
@@ -52,16 +52,16 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
protected $class; protected $class;
// 回调事件 // 回调事件
private static $event = []; private static $event = [];
// 数据表主键 复合主键使用数组定义
protected $pk;
// 错误信息 // 错误信息
protected $error; protected $error;
// 字段验证规则 // 字段验证规则
protected $validate; protected $validate;
// 数据表主键 复合主键使用数组定义
protected $pk;
// 字段属性 // 字段属性
protected $field = []; protected $field = [];
// 字段类型
protected $fieldType = [];
// 显示属性 // 显示属性
protected $visible = []; protected $visible = [];
// 隐藏属性 // 隐藏属性
@@ -155,6 +155,19 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
} else { } else {
$query->name($this->name); $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')) { if (method_exists($this, 'base')) {
call_user_func_array([$this, 'base'], [ & $query]); call_user_func_array([$this, 'base'], [ & $query]);
+1 -11
View File
@@ -19,9 +19,6 @@ use think\Route;
class Url class Url
{ {
// 生成URL地址的root
protected static $root;
/** /**
* URL生成 支持路由反射 * URL生成 支持路由反射
* @param string $url URL表达式, * @param string $url URL表达式,
@@ -132,7 +129,7 @@ class Url
// 检测域名 // 检测域名
$domain = self::parseDomain($url, $domain); $domain = self::parseDomain($url, $domain);
// URL组装 // URL组装
$url = $domain . (self::$root ?: Request::instance()->root()) . '/' . ltrim($url, '/'); $url = $domain . Request::instance()->root() . '/' . ltrim($url, '/');
return $url; return $url;
} }
@@ -357,11 +354,4 @@ class Url
{ {
Cache::rm('think_route_map'); Cache::rm('think_route_map');
} }
// 指定当前生成URL地址的root
public static function root($root)
{
self::$root = $root;
Request::instance()->root($root);
}
} }
+6 -8
View File
@@ -83,7 +83,7 @@ abstract class Builder
} }
// 获取绑定信息 // 获取绑定信息
$bind = $this->query->getTableInfo($options['table'], 'bind'); $bind = $this->query->getFieldsBind($options);
if ('*' == $options['field']) { if ('*' == $options['field']) {
$fields = array_keys($bind); $fields = array_keys($bind);
} else { } else {
@@ -228,8 +228,8 @@ abstract class Builder
$whereStr = ''; $whereStr = '';
// 获取字段信息 // 获取字段信息
$fields = $this->query->getTableInfo($options['table'], 'fields'); $fields = $this->query->getTableFields($options);
$binds = $this->query->getTableInfo($options['table'], 'bind'); $binds = $this->query->getFieldsBind($options);
foreach ($where as $key => $val) { foreach ($where as $key => $val) {
$str = []; $str = [];
foreach ($val as $field => $value) { foreach ($val as $field => $value) {
@@ -369,10 +369,8 @@ abstract class Builder
protected function parseDateTime($value, $key, $options = []) protected function parseDateTime($value, $key, $options = [])
{ {
// 获取时间字段类型 // 获取时间字段类型
$type = $this->query->getTableInfo($options['table'], 'type'); $type = $this->query->getFieldsType($options);
if (isset($options['field_type'][$key])) { if (isset($type[$key])) {
$info = $options['field_type'][$key];
} elseif (isset($type[$key])) {
$info = $type[$key]; $info = $type[$key];
} }
if (isset($info)) { if (isset($info)) {
@@ -608,7 +606,7 @@ abstract class Builder
{ {
// 获取合法的字段 // 获取合法的字段
if ('*' == $options['field']) { if ('*' == $options['field']) {
$fields = $this->query->getTableInfo($options['table'], 'fields'); $fields = $this->query->getFieldsType($options);
} else { } else {
$fields = $options['field']; $fields = $options['field'];
} }
+2
View File
@@ -105,6 +105,8 @@ abstract class Connection
'auto_timestamp' => false, 'auto_timestamp' => false,
// 是否需要进行SQL性能分析 // 是否需要进行SQL性能分析
'sql_explain' => false, 'sql_explain' => false,
// Builder类
'builder' => '',
]; ];
// PDO连接参数 // PDO连接参数
+101 -30
View File
@@ -34,7 +34,7 @@ class Query
// 数据库Connection对象实例 // 数据库Connection对象实例
protected $connection; protected $connection;
// 数据库驱动类型 // 数据库驱动类型
protected $driver; protected $builder;
// 当前模型类名称 // 当前模型类名称
protected $model; protected $model;
// 当前数据表名称(含前缀) // 当前数据表名称(含前缀)
@@ -59,7 +59,7 @@ class Query
public function __construct(Connection $connection = null, $model = '') public function __construct(Connection $connection = null, $model = '')
{ {
$this->connection = $connection ?: Db::connect([], true); $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->prefix = $this->connection->getConfig('prefix');
$this->model = $model; $this->model = $model;
} }
@@ -357,7 +357,7 @@ class Query
protected function builder() protected function builder()
{ {
static $builder = []; static $builder = [];
$driver = $this->driver; $driver = $this->builder;
if (!isset($builder[$driver])) { if (!isset($builder[$driver])) {
$class = false !== strpos($driver, '\\') ? $driver : '\\think\\db\\builder\\' . ucfirst($driver); $class = false !== strpos($driver, '\\') ? $driver : '\\think\\db\\builder\\' . ucfirst($driver);
$builder[$driver] = new $class($this->connection); $builder[$driver] = new $class($this->connection);
@@ -720,11 +720,11 @@ class Query
} }
if (true === $field) { if (true === $field) {
// 获取全部字段 // 获取全部字段
$fields = $this->getTableInfo($tableName, 'fields'); $fields = isset($this->options['allow_field']) ? $this->options['allow_field'] : $this->getTableInfo($tableName, 'fields');
$field = $fields ?: ['*']; $field = $fields ?: ['*'];
} elseif ($except) { } 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; $field = $fields ? array_diff($fields, $field) : $field;
} }
if ($tableName) { if ($tableName) {
@@ -1235,6 +1235,47 @@ class Query
return $this; 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 * @access public
@@ -1284,18 +1325,6 @@ class Query
return $this; return $this;
} }
/**
* 设置字段类型
* @access public
* @param array $fieldType 字段类型信息
* @return $this
*/
public function setFieldType($fieldType = [])
{
$this->options['field_type'] = $fieldType;
return $this;
}
/** /**
* 获取数据表信息 * 获取数据表信息
* @access public * @access public
@@ -1327,13 +1356,7 @@ class Query
foreach ($info as $key => $val) { foreach ($info as $key => $val) {
// 记录字段类型 // 记录字段类型
$type[$key] = $val['type']; $type[$key] = $val['type'];
if (preg_match('/(int|double|float|decimal|real|numeric|serial)/is', $val['type'])) { $bind[$key] = $this->getFieldBindType($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;
}
if (!empty($val['primary'])) { if (!empty($val['primary'])) {
$pk[] = $key; $pk[] = $key;
} }
@@ -1352,12 +1375,60 @@ class Query
/** /**
* 获取当前数据表的主键 * 获取当前数据表的主键
* @access public * @access public
* @param string $table 数据表名 * @param string|array $options 数据表名或者查询参数
* @return string|array * @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) protected function parsePkWhere($data, &$options)
{ {
$pk = $this->getPk($options['table']); $pk = $this->getPk($options);
// 获取当前数据表 // 获取当前数据表
if (!empty($options['alias'])) { if (!empty($options['alias'])) {
$alias = $options['alias']; $alias = $options['alias'];
@@ -1684,7 +1755,7 @@ class Query
{ {
$options = $this->parseExpress(); $options = $this->parseExpress();
if (empty($options['where'])) { if (empty($options['where'])) {
$pk = $this->getPk($options['table']); $pk = $this->getPk($options);
// 如果存在主键数据 则自动作为更新条件 // 如果存在主键数据 则自动作为更新条件
if (is_string($pk) && isset($data[$pk])) { if (is_string($pk) && isset($data[$pk])) {
$where[$pk] = $data[$pk]; $where[$pk] = $data[$pk];
@@ -1951,8 +2022,8 @@ class Query
*/ */
public function chunk($count, $callback, $column = null) public function chunk($count, $callback, $column = null)
{ {
$column = $column ?: $this->getPk();
$options = $this->getOptions(); $options = $this->getOptions();
$column = $column ?: ($options['pk'] ?: $this->getPk());
$bind = $this->bind; $bind = $this->bind;
$resultSet = $this->limit($count)->order($column, 'asc')->select(); $resultSet = $this->limit($count)->order($column, 'asc')->select();
+1 -7
View File
@@ -93,7 +93,7 @@ class Relation
$result = $relation->where($localKey, $this->parent->$foreignKey)->find(); $result = $relation->where($localKey, $this->parent->$foreignKey)->find();
break; break;
case self::HAS_MANY: case self::HAS_MANY:
$result = $relation->select(); $result = $relation->where($foreignKey, $this->parent->$localKey)->select();
break; break;
case self::HAS_MANY_THROUGH: case self::HAS_MANY_THROUGH:
$result = $relation->select(); $result = $relation->select();
@@ -659,12 +659,6 @@ class Relation
{ {
if ($this->query) { if ($this->query) {
switch ($this->type) { 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: case self::HAS_MANY_THROUGH:
$through = $this->middle; $through = $this->middle;
$model = $this->model; $model = $this->model;
+3
View File
@@ -39,6 +39,9 @@ class Think
if (empty($this->config['view_path'])) { if (empty($this->config['view_path'])) {
$this->config['view_path'] = App::$modulePath . 'view' . DS; $this->config['view_path'] = App::$modulePath . 'view' . DS;
} }
if (App::$debug) {
$this->config['tpl_cache'] = false;
}
$this->template = new Template($this->config); $this->template = new Template($this->config);
} }
+2 -2
View File
@@ -222,7 +222,7 @@ $(function() {
} else { } else {
updateAlert(data.msg); updateAlert(data.msg);
setTimeout(function() { setTimeout(function() {
location.reload(); //location.reload();
// if (data.url) { // if (data.url) {
// location.href = data.url; // location.href = data.url;
// } else { // } else {
@@ -305,7 +305,7 @@ $(function() {
updateAlert(data.msg, 'danger'); updateAlert(data.msg, 'danger');
setTimeout(function() { setTimeout(function() {
$(that).removeClass('disabled').prop('disabled', false); $(that).removeClass('disabled').prop('disabled', false);
location.reload(); //location.reload();
// if (data.url) { // if (data.url) {
// location.href = data.url; // location.href = data.url;
// } else { // } else {