From 40eab59c3949ef58f47871b796bad07e560f0758 Mon Sep 17 00:00:00 2001 From: zhaoxiang Date: Wed, 7 Feb 2018 17:04:52 +0800 Subject: [PATCH] =?UTF-8?q?modified=20TP=E7=89=88=E6=9C=AC=E5=8D=87?= =?UTF-8?q?=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- thinkphp/base.php | 2 +- thinkphp/library/think/Model.php | 17 +++- thinkphp/library/think/Request.php | 6 +- thinkphp/library/think/Route.php | 80 +++++++++---------- thinkphp/library/think/Template.php | 14 ++-- thinkphp/library/think/Url.php | 14 ++-- thinkphp/library/think/View.php | 2 +- thinkphp/library/think/cache/driver/File.php | 36 ++++++--- .../think/console/command/optimize/Config.php | 2 +- .../think/console/command/optimize/Schema.php | 6 +- thinkphp/library/think/db/Builder.php | 5 +- thinkphp/library/think/db/Query.php | 48 +++++------ thinkphp/library/think/log/driver/File.php | 5 +- thinkphp/library/think/model/Relation.php | 28 ++++++- .../library/think/model/relation/MorphTo.php | 12 +++ .../library/think/model/relation/OneToOne.php | 12 +-- thinkphp/library/think/template/TagLib.php | 4 +- thinkphp/library/think/template/taglib/Cx.php | 2 +- thinkphp/library/traits/model/SoftDelete.php | 34 +++++--- 19 files changed, 203 insertions(+), 126 deletions(-) diff --git a/thinkphp/base.php b/thinkphp/base.php index 1d7116b..f88935d 100755 --- a/thinkphp/base.php +++ b/thinkphp/base.php @@ -9,7 +9,7 @@ // | Author: liu21st // +---------------------------------------------------------------------- -define('THINK_VERSION', '5.0.14'); +define('THINK_VERSION', '5.0.15'); define('THINK_START_TIME', microtime(true)); define('THINK_START_MEM', memory_get_usage()); define('EXT', '.php'); diff --git a/thinkphp/library/think/Model.php b/thinkphp/library/think/Model.php index e617b64..a42eafa 100755 --- a/thinkphp/library/think/Model.php +++ b/thinkphp/library/think/Model.php @@ -192,7 +192,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $con = Db::connect($connection); // 设置当前模型 确保查询返回模型对象 $queryClass = $this->query ?: $con->getConfig('query'); - $query = new $queryClass($con, $this->class); + $query = new $queryClass($con, $this); // 设置当前数据表和模型名 if (!empty($this->table)) { @@ -208,6 +208,19 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return $query; } + /** + * 创建新的模型实例 + * @access public + * @param array|object $data 数据 + * @param bool $isUpdate 是否为更新 + * @param mixed $where 更新条件 + * @return Model + */ + public function newInstance($data = [], $isUpdate = false, $where = null) + { + return (new static($data))->isUpdate($isUpdate, $where); + } + /** * 获取当前模型的查询对象 * @access public @@ -599,7 +612,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ protected function getRelationData(Relation $modelRelation) { - if ($this->parent && get_class($this->parent) == $modelRelation->getModel()) { + if ($this->parent && !$modelRelation->isSelfRelation() && get_class($modelRelation->getModel()) == get_class($this->parent)) { $value = $this->parent; } else { // 首先获取关联数据 diff --git a/thinkphp/library/think/Request.php b/thinkphp/library/think/Request.php index 55ec1a2..4a40d22 100755 --- a/thinkphp/library/think/Request.php +++ b/thinkphp/library/think/Request.php @@ -692,10 +692,10 @@ class Request { if (empty($this->post)) { $content = $this->input; - if(false !== strpos($this->contentType(), 'multipart/form-data')) { - $this->post = $_POST; - } else { + if (empty($_POST) && false !== strpos($this->contentType(), 'application/json')) { $this->post = (array) json_decode($content, true); + } else { + $this->post = $_POST; } } if (is_array($name)) { diff --git a/thinkphp/library/think/Route.php b/thinkphp/library/think/Route.php index 730d31b..2dbd61d 100755 --- a/thinkphp/library/think/Route.php +++ b/thinkphp/library/think/Route.php @@ -222,11 +222,11 @@ class Route /** * 注册路由规则 * @access public - * @param string $rule 路由规则 - * @param string $route 路由地址 - * @param string $type 请求类型 - * @param array $option 路由参数 - * @param array $pattern 变量规则 + * @param string|array $rule 路由规则 + * @param string $route 路由地址 + * @param string $type 请求类型 + * @param array $option 路由参数 + * @param array $pattern 变量规则 * @return void */ public static function rule($rule, $route = '', $type = '*', $option = [], $pattern = []) @@ -270,12 +270,12 @@ class Route /** * 设置路由规则 * @access public - * @param string $rule 路由规则 - * @param string $route 路由地址 - * @param string $type 请求类型 - * @param array $option 路由参数 - * @param array $pattern 变量规则 - * @param string $group 所属分组 + * @param string|array $rule 路由规则 + * @param string $route 路由地址 + * @param string $type 请求类型 + * @param array $option 路由参数 + * @param array $pattern 变量规则 + * @param string $group 所属分组 * @return void */ protected static function setRule($rule, $route, $type = '*', $option = [], $pattern = [], $group = '') @@ -487,10 +487,10 @@ class Route /** * 注册路由 * @access public - * @param string $rule 路由规则 - * @param string $route 路由地址 - * @param array $option 路由参数 - * @param array $pattern 变量规则 + * @param string|array $rule 路由规则 + * @param string $route 路由地址 + * @param array $option 路由参数 + * @param array $pattern 变量规则 * @return void */ public static function any($rule, $route = '', $option = [], $pattern = []) @@ -501,10 +501,10 @@ class Route /** * 注册GET路由 * @access public - * @param string $rule 路由规则 - * @param string $route 路由地址 - * @param array $option 路由参数 - * @param array $pattern 变量规则 + * @param string|array $rule 路由规则 + * @param string $route 路由地址 + * @param array $option 路由参数 + * @param array $pattern 变量规则 * @return void */ public static function get($rule, $route = '', $option = [], $pattern = []) @@ -515,10 +515,10 @@ class Route /** * 注册POST路由 * @access public - * @param string $rule 路由规则 - * @param string $route 路由地址 - * @param array $option 路由参数 - * @param array $pattern 变量规则 + * @param string|array $rule 路由规则 + * @param string $route 路由地址 + * @param array $option 路由参数 + * @param array $pattern 变量规则 * @return void */ public static function post($rule, $route = '', $option = [], $pattern = []) @@ -529,10 +529,10 @@ class Route /** * 注册PUT路由 * @access public - * @param string $rule 路由规则 - * @param string $route 路由地址 - * @param array $option 路由参数 - * @param array $pattern 变量规则 + * @param string|array $rule 路由规则 + * @param string $route 路由地址 + * @param array $option 路由参数 + * @param array $pattern 变量规则 * @return void */ public static function put($rule, $route = '', $option = [], $pattern = []) @@ -543,10 +543,10 @@ class Route /** * 注册DELETE路由 * @access public - * @param string $rule 路由规则 - * @param string $route 路由地址 - * @param array $option 路由参数 - * @param array $pattern 变量规则 + * @param string|array $rule 路由规则 + * @param string $route 路由地址 + * @param array $option 路由参数 + * @param array $pattern 变量规则 * @return void */ public static function delete($rule, $route = '', $option = [], $pattern = []) @@ -557,10 +557,10 @@ class Route /** * 注册PATCH路由 * @access public - * @param string $rule 路由规则 - * @param string $route 路由地址 - * @param array $option 路由参数 - * @param array $pattern 变量规则 + * @param string|array $rule 路由规则 + * @param string $route 路由地址 + * @param array $option 路由参数 + * @param array $pattern 变量规则 * @return void */ public static function patch($rule, $route = '', $option = [], $pattern = []) @@ -571,10 +571,10 @@ class Route /** * 注册资源路由 * @access public - * @param string $rule 路由规则 - * @param string $route 路由地址 - * @param array $option 路由参数 - * @param array $pattern 变量规则 + * @param string|array $rule 路由规则 + * @param string $route 路由地址 + * @param array $option 路由参数 + * @param array $pattern 变量规则 * @return void */ public static function resource($rule, $route = '', $option = [], $pattern = []) @@ -667,7 +667,7 @@ class Route /** * rest方法定义和修改 * @access public - * @param string $name 方法名称 + * @param string|array $name 方法名称 * @param array|bool $resource 资源 * @return void */ diff --git a/thinkphp/library/think/Template.php b/thinkphp/library/think/Template.php index 8fe5190..02cd504 100755 --- a/thinkphp/library/think/Template.php +++ b/thinkphp/library/think/Template.php @@ -64,12 +64,14 @@ class Template */ public function __construct(array $config = []) { - $this->config['cache_path'] = TEMP_PATH; - $this->config = array_merge($this->config, $config); - $this->config['taglib_begin'] = $this->stripPreg($this->config['taglib_begin']); - $this->config['taglib_end'] = $this->stripPreg($this->config['taglib_end']); - $this->config['tpl_begin'] = $this->stripPreg($this->config['tpl_begin']); - $this->config['tpl_end'] = $this->stripPreg($this->config['tpl_end']); + $this->config['cache_path'] = TEMP_PATH; + $this->config = array_merge($this->config, $config); + $this->config['taglib_begin_origin'] = $this->config['taglib_begin']; + $this->config['taglib_end_origin'] = $this->config['taglib_end']; + $this->config['taglib_begin'] = $this->stripPreg($this->config['taglib_begin']); + $this->config['taglib_end'] = $this->stripPreg($this->config['taglib_end']); + $this->config['tpl_begin'] = $this->stripPreg($this->config['tpl_begin']); + $this->config['tpl_end'] = $this->stripPreg($this->config['tpl_end']); // 初始化模板编译存储器 $type = $this->config['compile_type'] ? $this->config['compile_type'] : 'File'; diff --git a/thinkphp/library/think/Url.php b/thinkphp/library/think/Url.php index 86fb264..2b5fd7f 100755 --- a/thinkphp/library/think/Url.php +++ b/thinkphp/library/think/Url.php @@ -210,17 +210,21 @@ class Url } $module = $module ? $module . '/' : ''; - $controller = Loader::parseName($request->controller()); + $controller = $request->controller(); if ('' == $url) { // 空字符串输出当前的 模块/控制器/操作 - $url = $module . $controller . '/' . $request->action(); + $action = $request->action(); } else { $path = explode('/', $url); - $action = Config::get('url_convert') ? strtolower(array_pop($path)) : array_pop($path); - $controller = empty($path) ? $controller : (Config::get('url_convert') ? Loader::parseName(array_pop($path)) : array_pop($path)); + $action = array_pop($path); + $controller = empty($path) ? $controller : array_pop($path); $module = empty($path) ? $module : array_pop($path) . '/'; - $url = $module . $controller . '/' . $action; } + if (Config::get('url_convert')) { + $action = strtolower($action); + $controller = Loader::parseName($controller); + } + $url = $module . $controller . '/' . $action; } return $url; } diff --git a/thinkphp/library/think/View.php b/thinkphp/library/think/View.php index 023eaf5..ca2dadb 100755 --- a/thinkphp/library/think/View.php +++ b/thinkphp/library/think/View.php @@ -158,7 +158,7 @@ class View try { $method = $renderContent ? 'display' : 'fetch'; // 允许用户自定义模板的字符串替换 - $replace = array_merge($this->replace, $replace, $this->engine->config('tpl_replace_string')); + $replace = array_merge($this->replace, $replace, (array) $this->engine->config('tpl_replace_string')); $this->engine->config('tpl_replace_string', $replace); $this->engine->$method($template, $vars, $config); } catch (\Exception $e) { diff --git a/thinkphp/library/think/cache/driver/File.php b/thinkphp/library/think/cache/driver/File.php index cafa0c0..6b1abd1 100755 --- a/thinkphp/library/think/cache/driver/File.php +++ b/thinkphp/library/think/cache/driver/File.php @@ -27,6 +27,8 @@ class File extends Driver 'data_compress' => false, ]; + protected $expire; + /** * 构造函数 * @param array $options @@ -61,10 +63,11 @@ class File extends Driver /** * 取得变量的存储文件名 * @access protected - * @param string $name 缓存变量名 + * @param string $name 缓存变量名 + * @param bool $auto 是否自动创建目录 * @return string */ - protected function getCacheKey($name) + protected function getCacheKey($name, $auto = false) { $name = md5($name); if ($this->options['cache_subdir']) { @@ -76,7 +79,8 @@ class File extends Driver } $filename = $this->options['path'] . $name . '.php'; $dir = dirname($filename); - if (!is_dir($dir)) { + + if ($auto && !is_dir($dir)) { mkdir($dir, 0755, true); } return $filename; @@ -106,13 +110,15 @@ class File extends Driver if (!is_file($filename)) { return $default; } - $content = file_get_contents($filename); + $content = file_get_contents($filename); + $this->expire = null; if (false !== $content) { $expire = (int) substr($content, 8, 12); if (0 != $expire && time() > filemtime($filename) + $expire) { return $default; } - $content = substr($content, 32); + $this->expire = $expire; + $content = substr($content, 32); if ($this->options['data_compress'] && function_exists('gzcompress')) { //启用数据压缩 $content = gzuncompress($content); @@ -140,7 +146,7 @@ class File extends Driver if ($expire instanceof \DateTime) { $expire = $expire->getTimestamp() - time(); } - $filename = $this->getCacheKey($name); + $filename = $this->getCacheKey($name, true); if ($this->tag && !is_file($filename)) { $first = true; } @@ -170,11 +176,14 @@ class File extends Driver public function inc($name, $step = 1) { if ($this->has($name)) { - $value = $this->get($name) + $step; + $value = $this->get($name) + $step; + $expire = $this->expire; } else { - $value = $step; + $value = $step; + $expire = 0; } - return $this->set($name, $value, 0) ? $value : false; + + return $this->set($name, $value, $expire) ? $value : false; } /** @@ -187,11 +196,14 @@ class File extends Driver public function dec($name, $step = 1) { if ($this->has($name)) { - $value = $this->get($name) - $step; + $value = $this->get($name) - $step; + $expire = $this->expire; } else { - $value = -$step; + $value = -$step; + $expire = 0; } - return $this->set($name, $value, 0) ? $value : false; + + return $this->set($name, $value, $expire) ? $value : false; } /** diff --git a/thinkphp/library/think/console/command/optimize/Config.php b/thinkphp/library/think/console/command/optimize/Config.php index cadfe5e..59c69a8 100755 --- a/thinkphp/library/think/console/command/optimize/Config.php +++ b/thinkphp/library/think/console/command/optimize/Config.php @@ -30,7 +30,7 @@ class Config extends Command protected function execute(Input $input, Output $output) { - if ($input->hasArgument('module')) { + if ($input->getArgument('module')) { $module = $input->getArgument('module') . DS; } else { $module = ''; diff --git a/thinkphp/library/think/console/command/optimize/Schema.php b/thinkphp/library/think/console/command/optimize/Schema.php index 27eb9db..3353424 100755 --- a/thinkphp/library/think/console/command/optimize/Schema.php +++ b/thinkphp/library/think/console/command/optimize/Schema.php @@ -44,7 +44,8 @@ class Schema extends Command if ($input->hasOption('module')) { $module = $input->getOption('module'); // 读取模型 - $list = scandir(APP_PATH . $module . DS . 'model'); + $path = APP_PATH . $module . DS . 'model'; + $list = is_dir($path) ? scandir($path) : []; $app = App::$namespace; foreach ($list as $file) { if (0 === strpos($file, '.')) { @@ -66,7 +67,8 @@ class Schema extends Command $tables = Db::connect($config)->getTables($dbName); } elseif (!\think\Config::get('app_multi_module')) { $app = App::$namespace; - $list = scandir(APP_PATH . 'model'); + $path = APP_PATH . 'model'; + $list = is_dir($path) ? scandir($path) : []; foreach ($list as $file) { if (0 === strpos($file, '.')) { continue; diff --git a/thinkphp/library/think/db/Builder.php b/thinkphp/library/think/db/Builder.php index 66c5b38..bbc2037 100755 --- a/thinkphp/library/think/db/Builder.php +++ b/thinkphp/library/think/db/Builder.php @@ -26,7 +26,7 @@ abstract class Builder protected $exp = ['eq' => '=', 'neq' => '<>', 'gt' => '>', 'egt' => '>=', 'lt' => '<', 'elt' => '<=', 'notlike' => 'NOT LIKE', 'not like' => 'NOT LIKE', 'like' => 'LIKE', 'in' => 'IN', 'exp' => 'EXP', 'notin' => 'NOT IN', 'not in' => 'NOT IN', 'between' => 'BETWEEN', 'not between' => 'NOT BETWEEN', 'notbetween' => 'NOT BETWEEN', 'exists' => 'EXISTS', 'notexists' => 'NOT EXISTS', 'not exists' => 'NOT EXISTS', 'null' => 'NULL', 'notnull' => 'NOT NULL', 'not null' => 'NOT NULL', '> time' => '> TIME', '< time' => '< TIME', '>= time' => '>= TIME', '<= time' => '<= TIME', 'between time' => 'BETWEEN TIME', 'not between time' => 'NOT BETWEEN TIME', 'notbetween time' => 'NOT BETWEEN TIME']; // SQL表达式 - protected $selectSql = 'SELECT%DISTINCT% %FIELD% FROM %TABLE%%FORCE%%UNION%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT%%LOCK%%COMMENT%'; + protected $selectSql = 'SELECT%DISTINCT% %FIELD% FROM %TABLE%%FORCE%%JOIN%%WHERE%%GROUP%%HAVING%%UNION%%ORDER%%LIMIT%%LOCK%%COMMENT%'; protected $insertSql = '%INSERT% INTO %TABLE% (%FIELD%) VALUES (%DATA%) %COMMENT%'; protected $insertAllSql = '%INSERT% INTO %TABLE% (%FIELD%) %DATA% %COMMENT%'; protected $updateSql = 'UPDATE %TABLE% SET %SET% %JOIN% %WHERE% %ORDER%%LIMIT% %LOCK%%COMMENT%'; @@ -207,9 +207,6 @@ abstract class Builder $item = []; foreach ((array) $tables as $key => $table) { if (!is_numeric($key)) { - if (strpos($key, '@think')) { - $key = strstr($key, '@think', true); - } $key = $this->parseSqlTable($key); $item[] = $this->parseKey($key) . ' ' . (isset($options['alias'][$table]) ? $this->parseKey($options['alias'][$table]) : $this->parseKey($table)); } else { diff --git a/thinkphp/library/think/db/Query.php b/thinkphp/library/think/db/Query.php index c22cf78..d425d78 100755 --- a/thinkphp/library/think/db/Query.php +++ b/thinkphp/library/think/db/Query.php @@ -58,9 +58,9 @@ class Query * 构造函数 * @access public * @param Connection $connection 数据库对象实例 - * @param string $model 模型名 + * @param Model $model 模型对象 */ - public function __construct(Connection $connection = null, $model = '') + public function __construct(Connection $connection = null, $model = null) { $this->connection = $connection ?: Db::connect([], true); $this->prefix = $this->connection->getConfig('prefix'); @@ -133,7 +133,7 @@ class Query /** * 获取当前的模型对象名 * @access public - * @return string + * @return Model|null */ public function getModel() { @@ -707,8 +707,7 @@ class Query { // 传入的表名为数组 if (is_array($join)) { - $table = key($join); - $alias = current($join); + $table = $join; } else { $join = trim($join); if (false !== strpos($join, '(')) { @@ -729,16 +728,9 @@ class Query $table = $this->getTable($table); } } - } - if (isset($alias) && $table != $alias) { - if (isset($this->options['alias'][$table])) { - $table = $table . '@think' . uniqid(); - } elseif ($this->gettable() == $table) { - $table = $table . '@think' . uniqid(); + if (isset($alias) && $table != $alias) { + $table = [$table => $alias]; } - - $table = [$table => $alias]; - $this->alias($table); } return $table; } @@ -1910,11 +1902,10 @@ class Query $with = explode(',', $with); } - $first = true; - $currentModel = $this->model; + $first = true; /** @var Model $class */ - $class = new $currentModel; + $class = $this->model; foreach ($with as $key => $relation) { $subRelation = ''; $closure = false; @@ -1973,7 +1964,7 @@ class Query $relation = $key; } $relation = Loader::parseName($relation, 1, false); - $count = '(' . (new $this->model)->$relation()->getRelationCountQuery($closure) . ')'; + $count = '(' . $this->model->$relation()->getRelationCountQuery($closure) . ')'; $this->field([$count => Loader::parseName($relation) . '_count']); } } @@ -2365,11 +2356,10 @@ class Query // 数据列表读取后的处理 if (!empty($this->model)) { // 生成模型对象 - $modelName = $this->model; if (count($resultSet) > 0) { foreach ($resultSet as $key => $result) { /** @var Model $model */ - $model = new $modelName($result); + $model = $this->model->newInstance($result); $model->isUpdate(true); // 关联查询 @@ -2389,7 +2379,7 @@ class Query // 模型数据集转换 $resultSet = $model->toCollection($resultSet); } else { - $resultSet = (new $modelName)->toCollection($resultSet); + $resultSet = $this->model->toCollection($resultSet); } } elseif ('collection' == $this->connection->getConfig('resultset_type')) { // 返回Collection对象 @@ -2515,7 +2505,7 @@ class Query $result = isset($resultSet[0]) ? $resultSet[0] : null; } - if (isset($cache) && false !== $result) { + if (isset($cache) && $result) { // 缓存数据 $this->cacheData($key, $result, $cache); } @@ -2525,8 +2515,7 @@ class Query if (!empty($result)) { if (!empty($this->model)) { // 返回模型对象 - $model = $this->model; - $result = new $model($result); + $result = $this->model->newInstance($result); $result->isUpdate(true, isset($options['where']['AND']) ? $options['where']['AND'] : null); // 关联查询 if (!empty($options['relation'])) { @@ -2557,7 +2546,8 @@ class Query protected function throwNotFound($options = []) { if (!empty($this->model)) { - throw new ModelNotFoundException('model data Not Found:' . $this->model, $this->model, $options); + $class = get_class($this->model); + throw new ModelNotFoundException('model data Not Found:' . $class, $class, $options); } else { $table = is_array($options['table']) ? key($options['table']) : $options['table']; throw new DataNotFoundException('table data not Found:' . $table, $table, $options); @@ -2605,7 +2595,9 @@ class Query public function chunk($count, $callback, $column = null, $order = 'asc') { $options = $this->getOptions(); - + if (empty($options['table'])) { + $options['table'] = $this->getTable(); + } $column = $column ?: $this->getPk($options); if (isset($options['order'])) { @@ -2628,7 +2620,7 @@ class Query } $resultSet = $query->order($column, $order)->select(); - while (!empty($resultSet)) { + while (count($resultSet) > 0) { if ($resultSet instanceof Collection) { $resultSet = $resultSet->all(); } @@ -2649,8 +2641,8 @@ class Query } $resultSet = $query->bind($bind)->order($column, $order)->select(); - } + return true; } diff --git a/thinkphp/library/think/log/driver/File.php b/thinkphp/library/think/log/driver/File.php index 236e21b..4036cc4 100755 --- a/thinkphp/library/think/log/driver/File.php +++ b/thinkphp/library/think/log/driver/File.php @@ -86,7 +86,10 @@ class File { //检测日志文件大小,超过配置大小则备份日志文件重新生成 if (is_file($destination) && floor($this->config['file_size']) <= filesize($destination)) { - rename($destination, dirname($destination) . DS . time() . '-' . basename($destination)); + try { + rename($destination, dirname($destination) . DS . time() . '-' . basename($destination)); + } catch (\Exception $e) { + } $this->writed[$destination] = false; } diff --git a/thinkphp/library/think/model/Relation.php b/thinkphp/library/think/model/Relation.php index 21cbdfd..b67bbf5 100755 --- a/thinkphp/library/think/model/Relation.php +++ b/thinkphp/library/think/model/Relation.php @@ -35,6 +35,8 @@ abstract class Relation protected $localKey; // 基础查询 protected $baseQuery; + // 是否为自关联 + protected $selfRelation; /** * 获取关联的所属模型 @@ -49,11 +51,11 @@ abstract class Relation /** * 获取当前的关联模型类 * @access public - * @return string + * @return Model */ public function getModel() { - return $this->model; + return $this->query->getModel(); } /** @@ -66,6 +68,28 @@ abstract class Relation return $this->query; } + /** + * 设置当前关联为自关联 + * @access public + * @param bool $self 是否自关联 + * @return $this + */ + public function selfRelation($self = true) + { + $this->selfRelation = $self; + return $this; + } + + /** + * 当前关联是否为自关联 + * @access public + * @return bool + */ + public function isSelfRelation() + { + return $this->selfRelation; + } + /** * 封装关联数据集 * @access public diff --git a/thinkphp/library/think/model/relation/MorphTo.php b/thinkphp/library/think/model/relation/MorphTo.php index 2990f75..504130a 100755 --- a/thinkphp/library/think/model/relation/MorphTo.php +++ b/thinkphp/library/think/model/relation/MorphTo.php @@ -43,6 +43,18 @@ class MorphTo extends Relation $this->relation = $relation; } + /** + * 获取当前的关联模型类的实例 + * @access public + * @return Model + */ + public function getModel() + { + $morphType = $this->morphType; + $model = $this->parseModel($this->parent->$morphType); + return (new $model); + } + /** * 延迟获取关联数据 * @param string $subRelation 子关联名 diff --git a/thinkphp/library/think/model/relation/OneToOne.php b/thinkphp/library/think/model/relation/OneToOne.php index 647fa8e..02d7d56 100755 --- a/thinkphp/library/think/model/relation/OneToOne.php +++ b/thinkphp/library/think/model/relation/OneToOne.php @@ -57,18 +57,18 @@ abstract class OneToOne extends Relation */ public function eagerly(Query $query, $relation, $subRelation, $closure, $first) { - $name = Loader::parseName(basename(str_replace('\\', '/', $query->getModel()))); - $alias = $name; + $name = Loader::parseName(basename(str_replace('\\', '/', get_class($query->getModel())))); + if ($first) { $table = $query->getTable(); - $query->table([$table => $alias]); + $query->table([$table => $name]); if ($query->getOptions('field')) { $field = $query->getOptions('field'); $query->removeOption('field'); } else { $field = true; } - $query->field($field, false, $table, $alias); + $query->field($field, false, $table, $name); $field = null; } @@ -78,9 +78,9 @@ abstract class OneToOne extends Relation $query->via($joinAlias); if ($this instanceof BelongsTo) { - $query->join([$joinTable => $joinAlias], $alias . '.' . $this->foreignKey . '=' . $joinAlias . '.' . $this->localKey, $this->joinType); + $query->join([$joinTable => $joinAlias], $name . '.' . $this->foreignKey . '=' . $joinAlias . '.' . $this->localKey, $this->joinType); } else { - $query->join([$joinTable => $joinAlias], $alias . '.' . $this->localKey . '=' . $joinAlias . '.' . $this->foreignKey, $this->joinType); + $query->join([$joinTable => $joinAlias], $name . '.' . $this->localKey . '=' . $joinAlias . '.' . $this->foreignKey, $this->joinType); } if ($closure) { diff --git a/thinkphp/library/think/template/TagLib.php b/thinkphp/library/think/template/TagLib.php index f6fa4a2..c5b72f9 100755 --- a/thinkphp/library/think/template/TagLib.php +++ b/thinkphp/library/think/template/TagLib.php @@ -264,8 +264,8 @@ class TagLib if (!empty($this->tags[$name]['expression'])) { static $_taglibs; if (!isset($_taglibs[$name])) { - $_taglibs[$name][0] = strlen(ltrim($this->tpl->config('taglib_begin'), '\\') . $name); - $_taglibs[$name][1] = strlen(ltrim($this->tpl->config('taglib_end'), '\\')); + $_taglibs[$name][0] = strlen($this->tpl->config('taglib_begin_origin') . $name); + $_taglibs[$name][1] = strlen($this->tpl->config('taglib_end_origin')); } $result['expression'] = substr($str, $_taglibs[$name][0], -$_taglibs[$name][1]); // 清除自闭合标签尾部/ diff --git a/thinkphp/library/think/template/taglib/Cx.php b/thinkphp/library/think/template/taglib/Cx.php index af7a54c..31e0698 100755 --- a/thinkphp/library/think/template/taglib/Cx.php +++ b/thinkphp/library/think/template/taglib/Cx.php @@ -278,7 +278,7 @@ class Cx extends Taglib */ public function tagCase($tag, $content) { - $value = !empty($tag['expression']) ? $tag['expression'] : $tag['value']; + $value = isset($tag['expression']) ? $tag['expression'] : $tag['value']; $flag = substr($value, 0, 1); if ('$' == $flag || ':' == $flag) { $value = $this->autoBuildVar($value); diff --git a/thinkphp/library/traits/model/SoftDelete.php b/thinkphp/library/traits/model/SoftDelete.php index e33402d..4c8a6b8 100755 --- a/thinkphp/library/traits/model/SoftDelete.php +++ b/thinkphp/library/traits/model/SoftDelete.php @@ -19,7 +19,10 @@ trait SoftDelete { $field = $this->getDeleteTimeField(); - return !empty($this->data[$field]); + if ($field && !empty($this->data[$field])) { + return true; + } + return false; } /** @@ -42,7 +45,11 @@ trait SoftDelete $model = new static(); $field = $model->getDeleteTimeField(true); - return $model->getQuery()->useSoftDelete($field, ['not null', '']); + if ($field) { + return $model->getQuery()->useSoftDelete($field, ['not null', '']); + } else { + return $model->getQuery(); + } } /** @@ -58,7 +65,7 @@ trait SoftDelete } $name = $this->getDeleteTimeField(); - if (!$force) { + if ($name && !$force) { // 软删除 $this->data[$name] = $this->autoWriteTimestamp($name); $result = $this->isUpdate()->save(); @@ -139,11 +146,15 @@ trait SoftDelete $name = $this->getDeleteTimeField(); - // 恢复删除 - return $this->getQuery() - ->useSoftDelete($name, ['not null', '']) - ->where($where) - ->update([$name => null]); + if ($name) { + // 恢复删除 + return $this->getQuery() + ->useSoftDelete($name, ['not null', '']) + ->where($where) + ->update([$name => null]); + } else { + return 0; + } } /** @@ -154,7 +165,8 @@ trait SoftDelete */ protected function base($query) { - return $query->useSoftDelete($this->getDeleteTimeField(true)); + $field = $this->getDeleteTimeField(true); + return $field ? $query->useSoftDelete($field) : $query; } /** @@ -169,6 +181,10 @@ trait SoftDelete $this->deleteTime : 'delete_time'; + if (false === $field) { + return false; + } + if (!strpos($field, '.')) { $field = '__TABLE__.' . $field; }