From a58d04e7e227adf812f66f2e91be4887f59c097f Mon Sep 17 00:00:00 2001 From: cxdbz Date: Thu, 28 Nov 2019 19:33:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7thinkphp=E5=88=B05.1.39?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.lock | 56 ++-- thinkphp/composer.json | 2 +- thinkphp/helper.php | 8 +- thinkphp/library/think/App.php | 2 +- thinkphp/library/think/Collection.php | 2 +- thinkphp/library/think/Controller.php | 4 +- thinkphp/library/think/Model.php | 15 +- thinkphp/library/think/Request.php | 17 +- thinkphp/library/think/Url.php | 18 +- thinkphp/library/think/Validate.php | 6 +- thinkphp/library/think/View.php | 5 +- thinkphp/library/think/cache/driver/File.php | 6 +- .../library/think/cache/driver/Memcached.php | 6 +- thinkphp/library/think/db/Builder.php | 21 +- thinkphp/library/think/db/Connection.php | 6 +- thinkphp/library/think/db/Query.php | 48 +++- thinkphp/library/think/db/builder/Mysql.php | 4 +- thinkphp/library/think/db/connector/Mysql.php | 22 +- .../think/exception/ValidateException.php | 2 +- thinkphp/library/think/log/driver/File.php | 14 +- .../think/model/concern/Conversion.php | 12 +- .../think/model/relation/BelongsTo.php | 19 +- .../think/model/relation/BelongsToMany.php | 9 +- .../library/think/model/relation/HasMany.php | 25 +- .../think/model/relation/HasManyThrough.php | 269 ++++++++++++++++-- .../library/think/model/relation/HasOne.php | 19 +- .../think/model/relation/MorphMany.php | 9 +- .../library/think/model/relation/MorphOne.php | 5 +- .../library/think/model/relation/MorphTo.php | 1 + .../library/think/model/relation/OneToOne.php | 5 +- thinkphp/library/think/response/View.php | 27 +- thinkphp/library/think/route/Dispatch.php | 9 +- .../library/think/route/dispatch/Module.php | 1 - thinkphp/library/think/route/dispatch/Url.php | 2 +- .../library/think/session/driver/Redis.php | 2 +- vendor/composer/ClassLoader.php | 2 +- vendor/composer/installed.json | 58 ++-- 37 files changed, 546 insertions(+), 192 deletions(-) diff --git a/composer.lock b/composer.lock index 263c4695..3519cdb0 100644 --- a/composer.lock +++ b/composer.lock @@ -21,7 +21,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -73,7 +73,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -124,7 +124,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -190,7 +190,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -246,7 +246,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -347,7 +347,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -397,7 +397,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -440,7 +440,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -482,7 +482,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -543,7 +543,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -636,7 +636,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -751,7 +751,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -805,7 +805,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -869,7 +869,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -929,7 +929,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -993,7 +993,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -1053,20 +1053,20 @@ }, { "name": "topthink/framework", - "version": "v5.1.37.1", + "version": "v5.1.39", "source": { "type": "git", "url": "https://github.com/top-think/framework.git", - "reference": "05eecd121d18d6705aaa10aa44fcdf7c14da4d0b" + "reference": "5762858f3d58faafb3a39427f8788884b2927007" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/top-think/framework/zipball/05eecd121d18d6705aaa10aa44fcdf7c14da4d0b", - "reference": "05eecd121d18d6705aaa10aa44fcdf7c14da4d0b", + "url": "https://api.github.com/repos/top-think/framework/zipball/5762858f3d58faafb3a39427f8788884b2927007", + "reference": "5762858f3d58faafb3a39427f8788884b2927007", "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -1106,7 +1106,7 @@ "orm", "thinkphp" ], - "time": "2019-05-28T06:57:29+00:00" + "time": "2019-11-17T23:22:02+00:00" }, { "name": "topthink/think-captcha", @@ -1123,7 +1123,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -1168,7 +1168,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -1210,7 +1210,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -1257,7 +1257,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -1302,7 +1302,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -1352,7 +1352,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] diff --git a/thinkphp/composer.json b/thinkphp/composer.json index cc4fca91..33477b1d 100644 --- a/thinkphp/composer.json +++ b/thinkphp/composer.json @@ -26,7 +26,7 @@ "require-dev": { "phpunit/phpunit": "^5.0|^6.0", "johnkary/phpunit-speedtrap": "^1.0", - "mikey179/vfsStream": "~1.6", + "mikey179/vfsstream": "~1.6", "phploc/phploc": "2.*", "sebastian/phpcpd": "2.*", "squizlabs/php_codesniffer": "2.*", diff --git a/thinkphp/helper.php b/thinkphp/helper.php index fc2ca8c8..72b9e9fd 100644 --- a/thinkphp/helper.php +++ b/thinkphp/helper.php @@ -686,7 +686,13 @@ if (!function_exists('widget')) { */ function widget($name, $data = []) { - return app()->action($name, $data, 'widget'); + $result = app()->action($name, $data, 'widget'); + + if (is_object($result)) { + $result = $result->getContent(); + } + + return $result; } } diff --git a/thinkphp/library/think/App.php b/thinkphp/library/think/App.php index 1fb9fbda..5cd55a8b 100644 --- a/thinkphp/library/think/App.php +++ b/thinkphp/library/think/App.php @@ -20,7 +20,7 @@ use think\route\Dispatch; */ class App extends Container { - const VERSION = '5.1.37 LTS'; + const VERSION = '5.1.39 LTS'; /** * 当前模块路径 diff --git a/thinkphp/library/think/Collection.php b/thinkphp/library/think/Collection.php index d58c8999..8251fc8f 100644 --- a/thinkphp/library/think/Collection.php +++ b/thinkphp/library/think/Collection.php @@ -353,7 +353,7 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria $result = isset($data[$field]) ? $data[$field] : null; } - switch ($operator) { + switch (strtolower($operator)) { case '===': return $result === $value; case '!==': diff --git a/thinkphp/library/think/Controller.php b/thinkphp/library/think/Controller.php index d16a1ed5..966eaaa8 100644 --- a/thinkphp/library/think/Controller.php +++ b/thinkphp/library/think/Controller.php @@ -158,7 +158,7 @@ class Controller */ protected function fetch($template = '', $vars = [], $config = []) { - return $this->view->fetch($template, $vars, $config); + return Response::create($template, 'view')->assign($vars)->config($config); } /** @@ -171,7 +171,7 @@ class Controller */ protected function display($content = '', $vars = [], $config = []) { - return $this->view->display($content, $vars, $config); + return Response::create($content, 'view')->assign($vars)->config($config)->isContent(true); } /** diff --git a/thinkphp/library/think/Model.php b/thinkphp/library/think/Model.php index 65e543d9..93be3dc0 100644 --- a/thinkphp/library/think/Model.php +++ b/thinkphp/library/think/Model.php @@ -780,12 +780,19 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 删除条件 $pk = $this->getPk(); + $where = []; if (is_string($pk) && isset($this->data[$pk])) { $where[] = [$pk, '=', $this->data[$pk]]; - } elseif (!empty($this->updateWhere)) { - $where = $this->updateWhere; - } else { - $where = null; + } elseif (is_array($pk)) { + foreach ($pk as $field) { + if (isset($this->data[$field])) { + $where[] = [$field, '=', $this->data[$field]]; + } + } + } + + if (empty($where)) { + $where = empty($this->updateWhere) ? null : $this->updateWhere; } return $where; diff --git a/thinkphp/library/think/Request.php b/thinkphp/library/think/Request.php index f6529d03..92a401d7 100644 --- a/thinkphp/library/think/Request.php +++ b/thinkphp/library/think/Request.php @@ -682,6 +682,7 @@ class Request // 判断URL里面是否有兼容模式参数 $pathinfo = $_GET[$this->config['var_pathinfo']]; unset($_GET[$this->config['var_pathinfo']]); + unset($this->get[$this->config['var_pathinfo']]); } elseif ($this->isCli()) { // CLI模式下 index.php module/controller/action/params/... $pathinfo = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : ''; @@ -702,6 +703,10 @@ class Request } } + if (!empty($pathinfo)) { + unset($this->get[$pathinfo], $this->request[$pathinfo]); + } + $this->pathinfo = empty($pathinfo) || '/' == $pathinfo ? '' : ltrim($pathinfo, '/'); } @@ -1039,7 +1044,7 @@ class Request protected function getInputData($content) { - if (false !== strpos($this->contentType(), 'application/json') || 0 === strpos($content, '{"')) { + if (false !== strpos($this->contentType(), 'json')) { return (array) json_decode($content, true); } elseif (strpos($content, '=')) { parse_str($content, $data); @@ -1631,6 +1636,16 @@ class Request return false; } + /** + * 当前是否JSON请求 + * @access public + * @return bool + */ + public function isJson() + { + return false !== strpos($this->type(), 'json'); + } + /** * 当前是否Ajax请求 * @access public diff --git a/thinkphp/library/think/Url.php b/thinkphp/library/think/Url.php index a339f8de..acd510aa 100644 --- a/thinkphp/library/think/Url.php +++ b/thinkphp/library/think/Url.php @@ -130,7 +130,9 @@ class Url // 匹配路由命名标识 $url = $match[0]; - $domain = $match[1]; + if ($domain) { + $domain = $match[1]; + } if (!is_null($match[2])) { $suffix = $match[2]; @@ -347,6 +349,7 @@ class Url // 匹配路由地址 public function getRuleUrl($rule, &$vars = [], $allowDomain = '') { + $port = $this->app['request']->port(); foreach ($rule as $item) { list($url, $pattern, $domain, $suffix, $method) = $item; @@ -354,8 +357,8 @@ class Url continue; } - if (!in_array($this->app['request']->port(), [80, 443])) { - $domain .= ':' . $this->app['request']->port(); + if ($port && !in_array($port, [80, 443])) { + $domain .= ':' . $port; } if (empty($pattern)) { @@ -363,11 +366,12 @@ class Url } $type = $this->config['url_common_param']; + $keys = []; foreach ($pattern as $key => $val) { if (isset($vars[$key])) { - $url = str_replace(['[:' . $key . ']', '<' . $key . '?>', ':' . $key, '<' . $key . '>'], $type ? $vars[$key] : urlencode($vars[$key]), $url); - unset($vars[$key]); + $url = str_replace(['[:' . $key . ']', '<' . $key . '?>', ':' . $key, '<' . $key . '>'], $type ? $vars[$key] : urlencode($vars[$key]), $url); + $keys[] = $key; $url = str_replace(['/?', '-?'], ['/', '-'], $url); $result = [rtrim($url, '?/-'), $domain, $suffix]; } elseif (2 == $val) { @@ -375,10 +379,14 @@ class Url $url = str_replace(['/?', '-?'], ['/', '-'], $url); $result = [rtrim($url, '?/-'), $domain, $suffix]; } else { + $result = null; + $keys = []; break; } } + $vars = array_diff_key($vars, array_flip($keys)); + if (isset($result)) { return $result; } diff --git a/thinkphp/library/think/Validate.php b/thinkphp/library/think/Validate.php index d4f6d236..f58d7a88 100644 --- a/thinkphp/library/think/Validate.php +++ b/thinkphp/library/think/Validate.php @@ -570,7 +570,7 @@ class Validate $result = str_replace(':attribute', $title, $result); if (strpos($result, ':rule') && is_scalar($rule)) { - $msg = str_replace(':rule', (string) $rule, $result); + $result = str_replace(':rule', (string) $rule, $result); } } @@ -934,8 +934,8 @@ class Validate if (isset($rule[2])) { $imageType = strtolower($rule[2]); - if ('jpeg' == $imageType) { - $imageType = 'jpg'; + if ('jpg' == $imageType) { + $imageType = 'jpeg'; } if (image_type_to_extension($type, false) != $imageType) { diff --git a/thinkphp/library/think/View.php b/thinkphp/library/think/View.php index 17860a6b..284dd41a 100644 --- a/thinkphp/library/think/View.php +++ b/thinkphp/library/think/View.php @@ -160,7 +160,10 @@ class View */ public function filter($filter) { - $this->filter = $filter; + if ($filter) { + $this->filter = $filter; + } + return $this; } diff --git a/thinkphp/library/think/cache/driver/File.php b/thinkphp/library/think/cache/driver/File.php index 93d321f2..60be08db 100644 --- a/thinkphp/library/think/cache/driver/File.php +++ b/thinkphp/library/think/cache/driver/File.php @@ -278,11 +278,13 @@ class File extends Driver if (is_dir($path)) { $matches = glob($path . DIRECTORY_SEPARATOR . '*.php'); if (is_array($matches)) { - array_map('unlink', $matches); + array_map(function ($v) { + $this->unlink($v); + }, $matches); } rmdir($path); } else { - unlink($path); + $this->unlink($path); } } diff --git a/thinkphp/library/think/cache/driver/Memcached.php b/thinkphp/library/think/cache/driver/Memcached.php index 6af60d19..4533e78a 100644 --- a/thinkphp/library/think/cache/driver/Memcached.php +++ b/thinkphp/library/think/cache/driver/Memcached.php @@ -67,7 +67,7 @@ class Memcached extends Driver } $this->handler->addServers($servers); - + $this->handler->setOption(\Memcached::OPT_COMPRESSION, false); if ('' != $this->options['username']) { $this->handler->setOption(\Memcached::OPT_BINARY_PROTOCOL, true); $this->handler->setSaslAuthData($this->options['username'], $this->options['password']); @@ -232,7 +232,7 @@ class Memcached extends Driver $this->handler->delete($tagName); } - if (!$this->handler->has($tagName)) { + if (!$this->has($tagName)) { $this->handler->set($tagName, ''); } @@ -255,7 +255,7 @@ class Memcached extends Driver if ($this->tag) { $tagName = $this->getTagKey($this->tag); - if ($this->handler->has($tagName)) { + if ($this->has($tagName)) { $this->handler->append($tagName, ',' . $name); } else { $this->handler->set($tagName, $name); diff --git a/thinkphp/library/think/db/Builder.php b/thinkphp/library/think/db/Builder.php index b742506a..a0faada8 100644 --- a/thinkphp/library/think/db/Builder.php +++ b/thinkphp/library/think/db/Builder.php @@ -313,9 +313,10 @@ abstract class Builder // 使用闭包查询 $newQuery = $query->newQuery()->setConnection($this->connection); $value($newQuery); - $whereClause = $this->buildWhere($query, $newQuery->getOptions('where')); + $whereClause = $this->buildWhere($newQuery, $newQuery->getOptions('where')); if (!empty($whereClause)) { + $query->bind($newQuery->getBind(false)); $str[] = ' ' . $logic . ' ( ' . $whereClause . ' )'; } } elseif (is_array($field)) { @@ -407,7 +408,7 @@ abstract class Builder $jsonType = $query->getJsonFieldType($field); $bindType = $this->connection->getFieldBindType($jsonType); } else { - $bindType = isset($binds[$field]) ? $binds[$field] : PDO::PARAM_STR; + $bindType = isset($binds[$field]) && 'LIKE' != $exp ? $binds[$field] : PDO::PARAM_STR; } if (is_scalar($value) && !in_array($exp, ['EXP', 'NOT NULL', 'NULL', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN']) && strpos($exp, 'TIME') === false) { @@ -450,7 +451,7 @@ abstract class Builder // 模糊匹配 if (is_array($value)) { foreach ($value as $item) { - $name = $query->bind($item, $bindType); + $name = $query->bind($item, PDO::PARAM_STR); $array[] = $key . ' ' . $exp . ' :' . $name; } @@ -604,6 +605,10 @@ abstract class Builder $value = $this->parseClosure($query, $value); } + if ('=' == $exp && is_null($value)) { + return $key . ' IS NULL'; + } + return $key . ' ' . $exp . ' ' . $value; } @@ -651,7 +656,6 @@ abstract class Builder $value = $value->getValue(); } else { $value = array_unique(is_array($value) ? $value : explode(',', $value)); - $array = []; foreach ($value as $k => $v) { @@ -659,9 +663,12 @@ abstract class Builder $array[] = ':' . $name; } - $zone = implode(',', $array); - - $value = empty($zone) ? "''" : $zone; + if (count($array) == 1) { + return $key . ('IN' == $exp ? ' = ' : ' <> ') . $array[0]; + } else { + $zone = implode(',', $array); + $value = empty($zone) ? "''" : $zone; + } } return $key . ' ' . $exp . ' (' . $value . ')'; diff --git a/thinkphp/library/think/db/Connection.php b/thinkphp/library/think/db/Connection.php index d6b3c7ed..18b4885a 100644 --- a/thinkphp/library/think/db/Connection.php +++ b/thinkphp/library/think/db/Connection.php @@ -1467,9 +1467,7 @@ abstract class Connection $value = is_array($val) ? $val[0] : $val; $type = is_array($val) ? $val[1] : PDO::PARAM_STR; - if (self::PARAM_FLOAT == $type) { - $value = (float) $value; - } elseif (PDO::PARAM_STR == $type) { + if ((self::PARAM_FLOAT == $type || PDO::PARAM_STR == $type) && is_string($value)) { $value = '\'' . addslashes($value) . '\''; } elseif (PDO::PARAM_INT == $type && '' === $value) { $value = 0; @@ -1503,7 +1501,7 @@ abstract class Connection if (PDO::PARAM_INT == $val[1] && '' === $val[0]) { $val[0] = 0; } elseif (self::PARAM_FLOAT == $val[1]) { - $val[0] = (float) $val[0]; + $val[0] = is_string($val[0]) ? (float) $val[0] : $val[0]; $val[1] = PDO::PARAM_STR; } diff --git a/thinkphp/library/think/db/Query.php b/thinkphp/library/think/db/Query.php index 5b1785e4..0c3df899 100644 --- a/thinkphp/library/think/db/Query.php +++ b/thinkphp/library/think/db/Query.php @@ -95,14 +95,14 @@ class Query * @var array */ protected $timeRule = [ - 'today' => ['today', 'tomorrow'], - 'yesterday' => ['yesterday', 'today'], - 'week' => ['this week 00:00:00', 'next week 00:00:00'], - 'last week' => ['last week 00:00:00', 'this week 00:00:00'], - 'month' => ['first Day of this month 00:00:00', 'first Day of next month 00:00:00'], - 'last month' => ['first Day of last month 00:00:00', 'first Day of this month 00:00:00'], - 'year' => ['this year 1/1', 'next year 1/1'], - 'last year' => ['last year 1/1', 'this year 1/1'], + 'today' => ['today', 'tomorrow -1second'], + 'yesterday' => ['yesterday', 'today -1second'], + 'week' => ['this week 00:00:00', 'next week 00:00:00 -1second'], + 'last week' => ['last week 00:00:00', 'this week 00:00:00 -1second'], + 'month' => ['first Day of this month 00:00:00', 'first Day of next month 00:00:00 -1second'], + 'last month' => ['first Day of last month 00:00:00', 'first Day of this month 00:00:00 -1second'], + 'year' => ['this year 1/1', 'next year 1/1 -1second'], + 'last year' => ['last year 1/1', 'this year 1/1 -1second'], ]; /** @@ -133,7 +133,27 @@ class Query */ public function newQuery() { - return new static($this->connection); + $query = new static($this->connection); + + if ($this->model) { + $query->model($this->model); + } + + if (isset($this->options['table'])) { + $query->table($this->options['table']); + } else { + $query->name($this->name); + } + + if (isset($this->options['json'])) { + $query->json($this->options['json'], $this->options['json_assoc']); + } + + if (isset($this->options['field_type'])) { + $query->setJsonFieldType($this->options['field_type']); + } + + return $query; } /** @@ -562,12 +582,12 @@ class Query default: if (function_exists($type)) { // 支持指定函数哈希 - $seq = (ord(substr($type($value), 0, 1)) % $rule['num']) + 1; - } else { - // 按照字段的首字母的值分表 - $seq = (ord($value{0}) % $rule['num']) + 1; + $value = $type($value); } + + $seq = (ord(substr($value, 0, 1)) % $rule['num']) + 1; } + return $this->getTable() . '_' . $seq; } // 当设置的分表字段不在查询条件或者数据中 @@ -2470,7 +2490,7 @@ class Query if (is_array($value)) { $this->bind = array_merge($this->bind, $value); } else { - $name = $name ?: 'ThinkBind_' . (count($this->bind) + 1) . '_'; + $name = $name ?: 'ThinkBind_' . (count($this->bind) + 1) . '_' . mt_rand() . '_'; $this->bind[$name] = [$value, $type]; return $name; diff --git a/thinkphp/library/think/db/builder/Mysql.php b/thinkphp/library/think/db/builder/Mysql.php index 22f33900..af364dff 100644 --- a/thinkphp/library/think/db/builder/Mysql.php +++ b/thinkphp/library/think/db/builder/Mysql.php @@ -62,7 +62,7 @@ class Mysql extends Builder $bind = $this->connection->getFieldsBind($options['table']); foreach ($dataSet as $k => $data) { - $data = $this->parseData($query, $data, $allowFields, $bind, '_' . $k); + $data = $this->parseData($query, $data, $allowFields, $bind); $values[] = '( ' . implode(',', array_values($data)) . ' )'; @@ -129,7 +129,7 @@ class Mysql extends Builder // JSON字段支持 list($field, $name) = explode('->', $key, 2); - return 'json_extract(' . $this->parseKey($query, $field, true) . ', \'$.' . str_replace('->', '.', $name) . '\')'; + return 'json_extract(' . $this->parseKey($query, $field, true) . ', \'$' . (strpos($name, '[') === 0 ? '' : '.') . str_replace('->', '.', $name) . '\')'; } elseif (strpos($key, '.') && !preg_match('/[,\'\"\(\)`\s]/', $key)) { list($table, $key) = explode('.', $key, 2); diff --git a/thinkphp/library/think/db/connector/Mysql.php b/thinkphp/library/think/db/connector/Mysql.php index 93b8a182..1713b99b 100644 --- a/thinkphp/library/think/db/connector/Mysql.php +++ b/thinkphp/library/think/db/connector/Mysql.php @@ -136,7 +136,27 @@ class Mysql extends Connection */ protected function getExplain($sql) { - $pdo = $this->linkID->query("EXPLAIN " . $sql); + $pdo = $this->linkID->prepare("EXPLAIN " . $this->queryStr); + + foreach ($this->bind as $key => $val) { + // 占位符 + $param = is_int($key) ? $key + 1 : ':' . $key; + + if (is_array($val)) { + if (PDO::PARAM_INT == $val[1] && '' === $val[0]) { + $val[0] = 0; + } elseif (self::PARAM_FLOAT == $val[1]) { + $val[0] = is_string($val[0]) ? (float) $val[0] : $val[0]; + $val[1] = PDO::PARAM_STR; + } + + $result = $pdo->bindValue($param, $val[0], $val[1]); + } else { + $result = $pdo->bindValue($param, $val); + } + } + + $pdo->execute(); $result = $pdo->fetch(PDO::FETCH_ASSOC); $result = array_change_key_case($result); diff --git a/thinkphp/library/think/exception/ValidateException.php b/thinkphp/library/think/exception/ValidateException.php index e3f84374..81ddfe21 100644 --- a/thinkphp/library/think/exception/ValidateException.php +++ b/thinkphp/library/think/exception/ValidateException.php @@ -18,7 +18,7 @@ class ValidateException extends \RuntimeException public function __construct($error, $code = 0) { $this->error = $error; - $this->message = is_array($error) ? implode("\n\r", $error) : $error; + $this->message = is_array($error) ? implode(PHP_EOL, $error) : $error; $this->code = $code; } diff --git a/thinkphp/library/think/log/driver/File.php b/thinkphp/library/think/log/driver/File.php index c506105f..3f6522d1 100644 --- a/thinkphp/library/think/log/driver/File.php +++ b/thinkphp/library/think/log/driver/File.php @@ -107,7 +107,7 @@ class File $info['timestamp'] = date($this->config['time_format']); foreach ($message as $type => $msg) { - $msg = is_array($msg) ? implode("\r\n", $msg) : $msg; + $msg = is_array($msg) ? implode(PHP_EOL, $msg) : $msg; if (PHP_SAPI == 'cli') { $info['msg'] = $msg; $info['type'] = $type; @@ -212,14 +212,14 @@ class File protected function parseCliLog($info) { if ($this->config['json']) { - $message = json_encode($info, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\r\n"; + $message = json_encode($info, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL; } else { $now = $info['timestamp']; unset($info['timestamp']); - $message = implode("\r\n", $info); + $message = implode(PHP_EOL, $info); - $message = "[{$now}]" . $message . "\r\n"; + $message = "[{$now}]" . $message . PHP_EOL; } return $message; @@ -242,13 +242,13 @@ class File if ($this->config['json']) { $info = $requestInfo + $info; - return json_encode($info, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\r\n"; + return json_encode($info, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL; } - array_unshift($info, "---------------------------------------------------------------\r\n[{$info['timestamp']}] {$requestInfo['ip']} {$requestInfo['method']} {$requestInfo['host']}{$requestInfo['uri']}"); + array_unshift($info, "---------------------------------------------------------------" . PHP_EOL . "\r\n[{$info['timestamp']}] {$requestInfo['ip']} {$requestInfo['method']} {$requestInfo['host']}{$requestInfo['uri']}"); unset($info['timestamp']); - return implode("\r\n", $info) . "\r\n"; + return implode(PHP_EOL, $info) . PHP_EOL; } protected function getDebugLog(&$info, $append, $apart) diff --git a/thinkphp/library/think/model/concern/Conversion.php b/thinkphp/library/think/model/concern/Conversion.php index 922d5b0e..28a6f999 100644 --- a/thinkphp/library/think/model/concern/Conversion.php +++ b/thinkphp/library/think/model/concern/Conversion.php @@ -189,10 +189,12 @@ trait Conversion if (!$relation) { $relation = $this->getAttr($key); - $relation->visible($name); + if ($relation) { + $relation->visible($name); + } } - $item[$key] = $relation->append($name)->toArray(); + $item[$key] = $relation ? $relation->append($name)->toArray() : []; } elseif (strpos($name, '.')) { list($key, $attr) = explode('.', $name); // 追加关联对象属性 @@ -200,10 +202,12 @@ trait Conversion if (!$relation) { $relation = $this->getAttr($key); - $relation->visible([$attr]); + if ($relation) { + $relation->visible([$attr]); + } } - $item[$key] = $relation->append([$attr])->toArray(); + $item[$key] = $relation ? $relation->append([$attr])->toArray() : []; } else { $item[$name] = $this->getAttr($name, $item); } diff --git a/thinkphp/library/think/model/relation/BelongsTo.php b/thinkphp/library/think/model/relation/BelongsTo.php index 98d176e8..056c7d76 100644 --- a/thinkphp/library/think/model/relation/BelongsTo.php +++ b/thinkphp/library/think/model/relation/BelongsTo.php @@ -11,6 +11,7 @@ namespace think\model\relation; +use Closure; use think\Loader; use think\Model; @@ -49,7 +50,7 @@ class BelongsTo extends OneToOne */ public function getRelation($subRelation = '', $closure = null) { - if ($closure) { + if ($closure instanceof Closure) { $closure($this->query); } @@ -79,7 +80,7 @@ class BelongsTo extends OneToOne */ public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*', &$aggregateAlias = '') { - if ($closure) { + if ($closure instanceof Closure) { $return = $closure($this->query); if ($return && is_string($return)) { @@ -111,7 +112,7 @@ class BelongsTo extends OneToOne return 0; } - if ($closure) { + if ($closure instanceof Closure) { $return = $closure($this->query); if ($return && is_string($return)) { @@ -140,13 +141,17 @@ class BelongsTo extends OneToOne $relation = basename(str_replace('\\', '/', $this->model)); $localKey = $this->localKey; $foreignKey = $this->foreignKey; + $softDelete = $this->query->getOptions('soft_delete'); return $this->parent->db() ->alias($model) ->whereExists(function ($query) use ($table, $model, $relation, $localKey, $foreignKey) { $query->table([$table => $relation]) ->field($relation . '.' . $localKey) - ->whereExp($model . '.' . $foreignKey, '=' . $relation . '.' . $localKey); + ->whereExp($model . '.' . $foreignKey, '=' . $relation . '.' . $localKey) + ->when($softDelete, function ($query) use ($softDelete, $relation) { + $query->where($relation . strstr($softDelete[0], '.'), '=' == $softDelete[1][0] ? $softDelete[1][1] : null); + }); }); } @@ -167,12 +172,16 @@ class BelongsTo extends OneToOne $this->getQueryWhere($where, $relation); } - $fields = $this->getRelationQueryFields($fields, $model); + $fields = $this->getRelationQueryFields($fields, $model); + $softDelete = $this->query->getOptions('soft_delete'); return $this->parent->db() ->alias($model) ->field($fields) ->join([$table => $relation], $model . '.' . $this->foreignKey . '=' . $relation . '.' . $this->localKey, $this->joinType) + ->when($softDelete, function ($query) use ($softDelete, $relation) { + $query->where($relation . strstr($softDelete[0], '.'), '=' == $softDelete[1][0] ? $softDelete[1][1] : null); + }) ->where($where); } diff --git a/thinkphp/library/think/model/relation/BelongsToMany.php b/thinkphp/library/think/model/relation/BelongsToMany.php index 54df68b0..2d64f683 100644 --- a/thinkphp/library/think/model/relation/BelongsToMany.php +++ b/thinkphp/library/think/model/relation/BelongsToMany.php @@ -11,6 +11,7 @@ namespace think\model\relation; +use Closure; use think\Collection; use think\db\Query; use think\Exception; @@ -166,7 +167,7 @@ class BelongsToMany extends Relation */ public function getRelation($subRelation = '', $closure = null) { - if ($closure) { + if ($closure instanceof Closure) { $closure($this->query); } @@ -377,7 +378,7 @@ class BelongsToMany extends Relation $pk = $result->$pk; - if ($closure) { + if ($closure instanceof Closure) { $return = $closure($this->query); if ($return && is_string($return)) { @@ -401,7 +402,7 @@ class BelongsToMany extends Relation */ public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*', &$aggregateAlias = '') { - if ($closure) { + if ($closure instanceof Closure) { $return = $closure($this->query); if ($return && is_string($return)) { @@ -428,7 +429,7 @@ class BelongsToMany extends Relation protected function eagerlyManyToMany($where, $relation, $subRelation = '', $closure = null) { // 预载入关联查询 支持嵌套预载入 - if ($closure) { + if ($closure instanceof Closure) { $closure($this->query); } diff --git a/thinkphp/library/think/model/relation/HasMany.php b/thinkphp/library/think/model/relation/HasMany.php index f97623b8..e4df5c4b 100644 --- a/thinkphp/library/think/model/relation/HasMany.php +++ b/thinkphp/library/think/model/relation/HasMany.php @@ -11,6 +11,7 @@ namespace think\model\relation; +use Closure; use think\db\Query; use think\Loader; use think\Model; @@ -48,7 +49,7 @@ class HasMany extends Relation */ public function getRelation($subRelation = '', $closure = null) { - if ($closure) { + if ($closure instanceof Closure) { $closure($this->query); } @@ -163,7 +164,7 @@ class HasMany extends Relation return 0; } - if ($closure) { + if ($closure instanceof Closure) { $return = $closure($this->query); if ($return && is_string($return)) { $name = $return; @@ -186,7 +187,7 @@ class HasMany extends Relation */ public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*', &$aggregateAlias = '') { - if ($closure) { + if ($closure instanceof Closure) { $return = $closure($this->query); if ($return && is_string($return)) { @@ -216,7 +217,7 @@ class HasMany extends Relation $this->query->removeWhereField($this->foreignKey); // 预载入关联查询 支持嵌套预载入 - if ($closure) { + if ($closure instanceof Closure) { $closure($this->query); } @@ -292,14 +293,18 @@ class HasMany extends Relation */ public function has($operator = '>=', $count = 1, $id = '*', $joinType = 'INNER') { - $table = $this->query->getTable(); - $model = basename(str_replace('\\', '/', get_class($this->parent))); - $relation = basename(str_replace('\\', '/', $this->model)); + $table = $this->query->getTable(); + $model = basename(str_replace('\\', '/', get_class($this->parent))); + $relation = basename(str_replace('\\', '/', $this->model)); + $softDelete = $this->query->getOptions('soft_delete'); return $this->parent->db() ->alias($model) ->field($model . '.*') ->join([$table => $relation], $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey, $joinType) + ->when($softDelete, function ($query) use ($softDelete, $relation) { + $query->where($relation . strstr($softDelete[0], '.'), '=' == $softDelete[1][0] ? $softDelete[1][1] : null); + }) ->group($relation . '.' . $this->foreignKey) ->having('count(' . $id . ')' . $operator . $count); } @@ -321,13 +326,17 @@ class HasMany extends Relation $this->getQueryWhere($where, $relation); } - $fields = $this->getRelationQueryFields($fields, $model); + $fields = $this->getRelationQueryFields($fields, $model); + $softDelete = $this->query->getOptions('soft_delete'); return $this->parent->db() ->alias($model) ->group($model . '.' . $this->localKey) ->field($fields) ->join([$table => $relation], $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey) + ->when($softDelete, function ($query) use ($softDelete, $relation) { + $query->where($relation . strstr($softDelete[0], '.'), '=' == $softDelete[1][0] ? $softDelete[1][1] : null); + }) ->where($where); } diff --git a/thinkphp/library/think/model/relation/HasManyThrough.php b/thinkphp/library/think/model/relation/HasManyThrough.php index 7c7acaa0..6e39f2ad 100644 --- a/thinkphp/library/think/model/relation/HasManyThrough.php +++ b/thinkphp/library/think/model/relation/HasManyThrough.php @@ -11,8 +11,8 @@ namespace think\model\relation; +use Closure; use think\db\Query; -use think\Exception; use think\Loader; use think\Model; use think\model\Relation; @@ -24,6 +24,12 @@ class HasManyThrough extends Relation // 中间表模型 protected $through; + /** + * 中间主键 + * @var string + */ + protected $throughPk; + /** * 架构函数 * @access public @@ -38,9 +44,10 @@ class HasManyThrough extends Relation { $this->parent = $parent; $this->model = $model; - $this->through = $through; + $this->through = (new $through)->db(); $this->foreignKey = $foreignKey; $this->throughKey = $throughKey; + $this->throughPk = $this->through->getPk(); $this->localKey = $localKey; $this->query = (new $model)->db(); } @@ -54,7 +61,7 @@ class HasManyThrough extends Relation */ public function getRelation($subRelation = '', $closure = null) { - if ($closure) { + if ($closure instanceof Closure) { $closure($this->query); } @@ -74,7 +81,28 @@ class HasManyThrough extends Relation */ public function has($operator = '>=', $count = 1, $id = '*', $joinType = 'INNER') { - return $this->parent; + $model = App::parseName(App::classBaseName($this->parent)); + $throughTable = $this->through->getTable(); + $pk = $this->throughPk; + $throughKey = $this->throughKey; + $relation = (new $this->model)->db(); + $relationTable = $relation->getTable(); + $softDelete = $this->query->getOptions('soft_delete'); + + if ('*' != $id) { + $id = $relationTable . '.' . $relation->getPk(); + } + + return $this->parent->db() + ->alias($model) + ->field($model . '.*') + ->join($throughTable, $throughTable . '.' . $this->foreignKey . '=' . $model . '.' . $this->localKey) + ->join($relationTable, $relationTable . '.' . $throughKey . '=' . $throughTable . '.' . $this->throughPk) + ->when($softDelete, function ($query) use ($softDelete, $relationTable) { + $query->where($relationTable . strstr($softDelete[0], '.'), '=' == $softDelete[1][0] ? $softDelete[1][1] : null); + }) + ->group($relationTable . '.' . $this->throughKey) + ->having('count(' . $id . ')' . $operator . $count); } /** @@ -86,45 +114,225 @@ class HasManyThrough extends Relation */ public function hasWhere($where = [], $fields = null) { - throw new Exception('relation not support: hasWhere'); + $model = App::parseName(App::classBaseName($this->parent)); + $throughTable = $this->through->getTable(); + $pk = $this->throughPk; + $throughKey = $this->throughKey; + $modelTable = (new $this->model)->db()->getTable(); + + if (is_array($where)) { + $this->getQueryWhere($where, $modelTable); + } + + $fields = $this->getRelationQueryFields($fields, $model); + $softDelete = $this->query->getOptions('soft_delete'); + + return $this->parent->db() + ->alias($model) + ->join($throughTable, $throughTable . '.' . $this->foreignKey . '=' . $model . '.' . $this->localKey) + ->join($modelTable, $modelTable . '.' . $throughKey . '=' . $throughTable . '.' . $this->throughPk) + ->when($softDelete, function ($query) use ($softDelete, $modelTable) { + $query->where($modelTable . strstr($softDelete[0], '.'), '=' == $softDelete[1][0] ? $softDelete[1][1] : null); + }) + ->group($modelTable . '.' . $this->throughKey) + ->where($where) + ->field($fields); } /** - * 预载入关联查询 - * @access public - * @param array $resultSet 数据集 - * @param string $relation 当前关联名 - * @param string $subRelation 子关联名 - * @param \Closure $closure 闭包 + * 预载入关联查询(数据集) + * @access protected + * @param array $resultSet 数据集 + * @param string $relation 当前关联名 + * @param mixed $subRelation 子关联名 + * @param Closure $closure 闭包 * @return void */ - public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure) - {} + public function eagerlyResultSet(array &$resultSet, $relation, $subRelation = '', $closure = null) + { + $localKey = $this->localKey; + $foreignKey = $this->foreignKey; + + $range = []; + foreach ($resultSet as $result) { + // 获取关联外键列表 + if (isset($result->$localKey)) { + $range[] = $result->$localKey; + } + } + + if (!empty($range)) { + $this->query->removeWhereField($foreignKey); + + $data = $this->eagerlyWhere([ + [$this->foreignKey, 'in', $range], + ], $foreignKey, $relation, $subRelation, $closure); + + // 关联属性名 + $attr = App::parseName($relation); + + // 关联数据封装 + foreach ($resultSet as $result) { + $pk = $result->$localKey; + if (!isset($data[$pk])) { + $data[$pk] = []; + } + + foreach ($data[$pk] as &$relationModel) { + $relationModel->setParent(clone $result); + } + + // 设置关联属性 + $result->setRelation($attr, $this->resultSetBuild($data[$pk])); + } + } + } /** - * 预载入关联查询 返回模型对象 - * @access public - * @param Model $result 数据对象 - * @param string $relation 当前关联名 - * @param string $subRelation 子关联名 - * @param \Closure $closure 闭包 + * 预载入关联查询(数据) + * @access protected + * @param Model $result 数据对象 + * @param string $relation 当前关联名 + * @param mixed $subRelation 子关联名 + * @param Closure $closure 闭包 * @return void */ - public function eagerlyResult(&$result, $relation, $subRelation, $closure) - {} + public function eagerlyResult($result, $relation, $subRelation = '', $closure = null) + { + $localKey = $this->localKey; + $foreignKey = $this->foreignKey; + $pk = $result->$localKey; + + $this->query->removeWhereField($foreignKey); + + $data = $this->eagerlyWhere([ + [$foreignKey, '=', $pk], + ], $foreignKey, $relation, $subRelation, $closure); + + // 关联数据封装 + if (!isset($data[$pk])) { + $data[$pk] = []; + } + + foreach ($data[$pk] as &$relationModel) { + $relationModel->setParent(clone $result); + } + + $result->setRelation(App::parseName($relation), $this->resultSetBuild($data[$pk])); + } + + /** + * 关联模型预查询 + * @access public + * @param array $where 关联预查询条件 + * @param string $key 关联键名 + * @param string $relation 关联名 + * @param mixed $subRelation 子关联 + * @param Closure $closure + * @return array + */ + protected function eagerlyWhere(array $where, $key, $relation, $subRelation = '', $closure = null) + { + // 预载入关联查询 支持嵌套预载入 + $throughList = $this->through->where($where)->select(); + $keys = $throughList->column($this->throughPk, $this->throughPk); + + if ($closure instanceof Closure) { + $closure($this->query); + } + + $list = $this->query->where($this->throughKey, 'in', $keys)->select(); + + // 组装模型数据 + $data = []; + $keys = $throughList->column($this->foreignKey, $this->throughPk); + + foreach ($list as $set) { + $data[$keys[$set->{$this->throughKey}]][] = $set; + } + + return $data; + } /** * 关联统计 * @access public - * @param Model $result 数据对象 - * @param \Closure $closure 闭包 - * @param string $aggregate 聚合查询方法 - * @param string $field 字段 - * @param string $name 统计字段别名 + * @param Model $result 数据对象 + * @param Closure $closure 闭包 + * @param string $aggregate 聚合查询方法 + * @param string $field 字段 + * @param string $name 统计字段别名 * @return integer */ - public function relationCount($result, $closure, $aggregate = 'count', $field = '*', &$name = '') - {} + public function relationCount($result, $closure, $aggregate = 'count', $field = '*', &$name = null) + { + $localKey = $this->localKey; + + if (!isset($result->$localKey)) { + return 0; + } + + if ($closure instanceof Closure) { + $return = $closure($this->query); + if ($return && is_string($return)) { + $name = $return; + } + } + + $alias = App::parseName(App::classBaseName($this->model)); + $throughTable = $this->through->getTable(); + $pk = $this->throughPk; + $throughKey = $this->throughKey; + $modelTable = $this->parent->getTable(); + + if (false === strpos($field, '.')) { + $field = $alias . '.' . $field; + } + + return $this->query + ->alias($alias) + ->join($throughTable, $throughTable . '.' . $pk . '=' . $alias . '.' . $throughKey) + ->join($modelTable, $modelTable . '.' . $this->localKey . '=' . $throughTable . '.' . $this->foreignKey) + ->where($throughTable . '.' . $this->foreignKey, $result->$localKey) + ->$aggregate($field); + } + + /** + * 创建关联统计子查询 + * @access public + * @param Closure $closure 闭包 + * @param string $aggregate 聚合查询方法 + * @param string $field 字段 + * @param string $name 统计字段别名 + * @return string + */ + public function getRelationCountQuery($closure = null, $aggregate = 'count', $field = '*', &$name = null) + { + if ($closure instanceof Closure) { + $return = $closure($this->query); + if ($return && is_string($return)) { + $name = $return; + } + } + + $alias = App::parseName(App::classBaseName($this->model)); + $throughTable = $this->through->getTable(); + $pk = $this->throughPk; + $throughKey = $this->throughKey; + $modelTable = $this->parent->getTable(); + + if (false === strpos($field, '.')) { + $field = $alias . '.' . $field; + } + + return $this->query + ->alias($alias) + ->join($throughTable, $throughTable . '.' . $pk . '=' . $alias . '.' . $throughKey) + ->join($modelTable, $modelTable . '.' . $this->localKey . '=' . $throughTable . '.' . $this->foreignKey) + ->whereExp($throughTable . '.' . $this->foreignKey, '=' . $this->parent->getTable() . '.' . $this->localKey) + ->fetchSql() + ->$aggregate($field); + } /** * 执行基础查询(仅执行一次) @@ -134,10 +342,9 @@ class HasManyThrough extends Relation protected function baseQuery() { if (empty($this->baseQuery) && $this->parent->getData()) { - $through = $this->through; $alias = Loader::parseName(basename(str_replace('\\', '/', $this->model))); - $throughTable = $through::getTable(); - $pk = (new $through)->getPk(); + $throughTable = $this->through->getTable(); + $pk = $this->throughPk; $throughKey = $this->throughKey; $modelTable = $this->parent->getTable(); $fields = $this->getQueryFields($alias); diff --git a/thinkphp/library/think/model/relation/HasOne.php b/thinkphp/library/think/model/relation/HasOne.php index d8e3ec79..e036bba4 100644 --- a/thinkphp/library/think/model/relation/HasOne.php +++ b/thinkphp/library/think/model/relation/HasOne.php @@ -11,6 +11,7 @@ namespace think\model\relation; +use Closure; use think\db\Query; use think\Loader; use think\Model; @@ -50,7 +51,7 @@ class HasOne extends OneToOne { $localKey = $this->localKey; - if ($closure) { + if ($closure instanceof Closure) { $closure($this->query); } @@ -79,7 +80,7 @@ class HasOne extends OneToOne */ public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*', &$aggregateAlias = '') { - if ($closure) { + if ($closure instanceof Closure) { $return = $closure($this->query); if ($return && is_string($return)) { @@ -111,7 +112,7 @@ class HasOne extends OneToOne return 0; } - if ($closure) { + if ($closure instanceof Closure) { $return = $closure($this->query); if ($return && is_string($return)) { $name = $return; @@ -139,13 +140,17 @@ class HasOne extends OneToOne $relation = basename(str_replace('\\', '/', $this->model)); $localKey = $this->localKey; $foreignKey = $this->foreignKey; + $softDelete = $this->query->getOptions('soft_delete'); return $this->parent->db() ->alias($model) ->whereExists(function ($query) use ($table, $model, $relation, $localKey, $foreignKey) { $query->table([$table => $relation]) ->field($relation . '.' . $foreignKey) - ->whereExp($model . '.' . $localKey, '=' . $relation . '.' . $foreignKey); + ->whereExp($model . '.' . $localKey, '=' . $relation . '.' . $foreignKey) + ->when($softDelete, function ($query) use ($softDelete, $relation) { + $query->where($relation . strstr($softDelete[0], '.'), '=' == $softDelete[1][0] ? $softDelete[1][1] : null); + }); }); } @@ -166,12 +171,16 @@ class HasOne extends OneToOne $this->getQueryWhere($where, $relation); } - $fields = $this->getRelationQueryFields($fields, $model); + $fields = $this->getRelationQueryFields($fields, $model); + $softDelete = $this->query->getOptions('soft_delete'); return $this->parent->db() ->alias($model) ->field($fields) ->join([$table => $relation], $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey, $this->joinType) + ->when($softDelete, function ($query) use ($softDelete, $relation) { + $query->where($relation . strstr($softDelete[0], '.'), '=' == $softDelete[1][0] ? $softDelete[1][1] : null); + }) ->where($where); } diff --git a/thinkphp/library/think/model/relation/MorphMany.php b/thinkphp/library/think/model/relation/MorphMany.php index a1f54889..d2af66e9 100644 --- a/thinkphp/library/think/model/relation/MorphMany.php +++ b/thinkphp/library/think/model/relation/MorphMany.php @@ -11,6 +11,7 @@ namespace think\model\relation; +use Closure; use think\db\Query; use think\Exception; use think\Loader; @@ -53,7 +54,7 @@ class MorphMany extends Relation */ public function getRelation($subRelation = '', $closure = null) { - if ($closure) { + if ($closure instanceof Closure) { $closure($this->query); } @@ -197,7 +198,7 @@ class MorphMany extends Relation return 0; } - if ($closure) { + if ($closure instanceof Closure) { $return = $closure($this->query); if ($return && is_string($return)) { @@ -224,7 +225,7 @@ class MorphMany extends Relation */ public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*', &$aggregateAlias = '') { - if ($closure) { + if ($closure instanceof Closure) { $return = $closure($this->query); if ($return && is_string($return)) { @@ -253,7 +254,7 @@ class MorphMany extends Relation // 预载入关联查询 支持嵌套预载入 $this->query->removeOption('where'); - if ($closure) { + if ($closure instanceof Closure) { $closure($this->query); } diff --git a/thinkphp/library/think/model/relation/MorphOne.php b/thinkphp/library/think/model/relation/MorphOne.php index 775b2dfd..6bc205c5 100644 --- a/thinkphp/library/think/model/relation/MorphOne.php +++ b/thinkphp/library/think/model/relation/MorphOne.php @@ -11,6 +11,7 @@ namespace think\model\relation; +use Closure; use think\db\Query; use think\Exception; use think\Loader; @@ -53,7 +54,7 @@ class MorphOne extends Relation */ public function getRelation($subRelation = '', $closure = null) { - if ($closure) { + if ($closure instanceof Closure) { $closure($this->query); } @@ -186,7 +187,7 @@ class MorphOne extends Relation protected function eagerlyMorphToOne($where, $relation, $subRelation = '', $closure = null) { // 预载入关联查询 支持嵌套预载入 - if ($closure) { + if ($closure instanceof Closure) { $closure($this->query); } diff --git a/thinkphp/library/think/model/relation/MorphTo.php b/thinkphp/library/think/model/relation/MorphTo.php index 6da6a025..17771ce8 100644 --- a/thinkphp/library/think/model/relation/MorphTo.php +++ b/thinkphp/library/think/model/relation/MorphTo.php @@ -11,6 +11,7 @@ namespace think\model\relation; +use Closure; use think\Exception; use think\Loader; use think\Model; diff --git a/thinkphp/library/think/model/relation/OneToOne.php b/thinkphp/library/think/model/relation/OneToOne.php index 59fce77f..a0333c86 100644 --- a/thinkphp/library/think/model/relation/OneToOne.php +++ b/thinkphp/library/think/model/relation/OneToOne.php @@ -11,6 +11,7 @@ namespace think\model\relation; +use Closure; use think\db\Query; use think\Exception; use think\Loader; @@ -87,7 +88,7 @@ abstract class OneToOne extends Relation $joinOn = $name . '.' . $this->localKey . '=' . $joinAlias . '.' . $this->foreignKey; } - if ($closure) { + if ($closure instanceof Closure) { // 执行闭包查询 $closure($query); // 使用withField指定获取关联的字段,如 @@ -311,7 +312,7 @@ abstract class OneToOne extends Relation protected function eagerlyWhere($where, $key, $relation, $subRelation = '', $closure = null) { // 预载入关联查询 支持嵌套预载入 - if ($closure) { + if ($closure instanceof Closure) { $closure($this->query); if ($field = $this->query->getOptions('with_field')) { diff --git a/thinkphp/library/think/response/View.php b/thinkphp/library/think/response/View.php index c836ccb5..3d54c735 100644 --- a/thinkphp/library/think/response/View.php +++ b/thinkphp/library/think/response/View.php @@ -18,9 +18,16 @@ class View extends Response // 输出参数 protected $options = []; protected $vars = []; + protected $config = []; protected $filter; protected $contentType = 'text/html'; + /** + * 是否内容渲染 + * @var bool + */ + protected $isContent = false; + /** * 处理数据 * @access protected @@ -32,7 +39,19 @@ class View extends Response // 渲染模板输出 return $this->app['view'] ->filter($this->filter) - ->fetch($data, $this->vars); + ->fetch($data, $this->vars, $this->config, $this->isContent); + } + + /** + * 设置是否为内容渲染 + * @access public + * @param bool $content + * @return $this + */ + public function isContent($content = true) + { + $this->isContent = $content; + return $this; } /** @@ -68,6 +87,12 @@ class View extends Response return $this; } + public function config($config) + { + $this->config = $config; + return $this; + } + /** * 视图内容过滤 * @access public diff --git a/thinkphp/library/think/route/Dispatch.php b/thinkphp/library/think/route/Dispatch.php index 93afe73b..7323c98d 100644 --- a/thinkphp/library/think/route/Dispatch.php +++ b/thinkphp/library/think/route/Dispatch.php @@ -11,9 +11,9 @@ namespace think\route; +use think\App; use think\Container; use think\exception\ValidateException; -use think\App; use think\Request; use think\Response; @@ -181,9 +181,10 @@ abstract class Dispatch $response = Response::create($data, $type); } else { - $data = ob_get_clean(); - $content = false === $data ? '' : $data; - $status = '' === $content && $this->request->isAjax() ? 204 : 200; + $data = ob_get_clean(); + $content = false === $data ? '' : $data; + $status = '' === $content && $this->request->isJson() ? 204 : 200; + $response = Response::create($content, '', $status); } diff --git a/thinkphp/library/think/route/dispatch/Module.php b/thinkphp/library/think/route/dispatch/Module.php index e8842cd3..40bd7759 100644 --- a/thinkphp/library/think/route/dispatch/Module.php +++ b/thinkphp/library/think/route/dispatch/Module.php @@ -12,7 +12,6 @@ namespace think\route\dispatch; use ReflectionMethod; -use think\Controller; use think\exception\ClassNotFoundException; use think\exception\HttpException; use think\Loader; diff --git a/thinkphp/library/think/route/dispatch/Url.php b/thinkphp/library/think/route/dispatch/Url.php index 00dc8cca..acc524e3 100644 --- a/thinkphp/library/think/route/dispatch/Url.php +++ b/thinkphp/library/think/route/dispatch/Url.php @@ -60,7 +60,7 @@ class Url extends Dispatch $controller = !empty($path) ? array_shift($path) : null; } - if ($controller && !preg_match('/^[A-Za-z][\w|\.]*$/', $controller)) { + if ($controller && !preg_match('/^[A-Za-z0-9][\w|\.]*$/', $controller)) { throw new HttpException(404, 'controller not exists:' . $controller); } diff --git a/thinkphp/library/think/session/driver/Redis.php b/thinkphp/library/think/session/driver/Redis.php index 646700bd..5a0e7bc7 100644 --- a/thinkphp/library/think/session/driver/Redis.php +++ b/thinkphp/library/think/session/driver/Redis.php @@ -124,7 +124,7 @@ class Redis implements SessionHandlerInterface */ public function destroy($sessID) { - return $this->handler->delete($this->config['session_name'] . $sessID) > 0; + return $this->handler->del($this->config['session_name'] . $sessID) > 0; } /** diff --git a/vendor/composer/ClassLoader.php b/vendor/composer/ClassLoader.php index 95f7e097..fce8549f 100644 --- a/vendor/composer/ClassLoader.php +++ b/vendor/composer/ClassLoader.php @@ -279,7 +279,7 @@ class ClassLoader */ public function setApcuPrefix($apcuPrefix) { - $this->apcuPrefix = function_exists('apcu_fetch') && ini_get('apc.enabled') ? $apcuPrefix : null; + $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; } /** diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json index 428e82f5..51b01b52 100644 --- a/vendor/composer/installed.json +++ b/vendor/composer/installed.json @@ -15,7 +15,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -69,7 +69,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -122,7 +122,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -190,7 +190,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -248,7 +248,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -351,7 +351,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -403,7 +403,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -448,7 +448,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -492,7 +492,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -555,7 +555,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -650,7 +650,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -767,7 +767,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -823,7 +823,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -889,7 +889,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -951,7 +951,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -1017,7 +1017,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -1078,21 +1078,21 @@ }, { "name": "topthink/framework", - "version": "v5.1.37.1", - "version_normalized": "5.1.37.1", + "version": "v5.1.39", + "version_normalized": "5.1.39.0", "source": { "type": "git", "url": "https://github.com/top-think/framework.git", - "reference": "05eecd121d18d6705aaa10aa44fcdf7c14da4d0b" + "reference": "5762858f3d58faafb3a39427f8788884b2927007" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/top-think/framework/zipball/05eecd121d18d6705aaa10aa44fcdf7c14da4d0b", - "reference": "05eecd121d18d6705aaa10aa44fcdf7c14da4d0b", + "url": "https://api.github.com/repos/top-think/framework/zipball/5762858f3d58faafb3a39427f8788884b2927007", + "reference": "5762858f3d58faafb3a39427f8788884b2927007", "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -1110,7 +1110,7 @@ "sebastian/phpcpd": "2.*", "squizlabs/php_codesniffer": "2.*" }, - "time": "2019-05-28T06:57:29+00:00", + "time": "2019-11-17T23:22:02+00:00", "type": "think-framework", "installation-source": "dist", "notification-url": "https://packagist.org/downloads/", @@ -1151,7 +1151,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -1198,7 +1198,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -1242,7 +1242,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -1291,7 +1291,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -1338,7 +1338,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ] @@ -1390,7 +1390,7 @@ "shasum": "", "mirrors": [ { - "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", "preferred": true } ]