diff --git a/core/base.php b/core/base.php index cbe288bf..63fc16c3 100644 --- a/core/base.php +++ b/core/base.php @@ -9,7 +9,7 @@ // | Author: liu21st // +---------------------------------------------------------------------- -define('THINK_VERSION', '5.0.5'); +define('THINK_VERSION', '5.0.6'); define('THINK_START_TIME', microtime(true)); define('THINK_START_MEM', memory_get_usage()); define('EXT', '.php'); diff --git a/core/convention.php b/core/convention.php index b6f23e28..887b076d 100644 --- a/core/convention.php +++ b/core/convention.php @@ -206,7 +206,7 @@ return [ // 是否自动开启 SESSION 'auto_start' => true, 'httponly' => true, - 'secure' => true, + 'secure' => false, ], // +---------------------------------------------------------------------- diff --git a/core/lang/zh-cn.php b/core/lang/zh-cn.php index b837bc41..c6c5a50f 100644 --- a/core/lang/zh-cn.php +++ b/core/lang/zh-cn.php @@ -64,4 +64,5 @@ return [ 'invalid request' => '非法请求', 'bind attr has exists' => '模型的属性已经存在', 'relation data not exists' => '关联数据不存在', + 'relation not support' => '关联不支持', ]; diff --git a/core/library/think/Config.php b/core/library/think/Config.php index fc6c50ac..2b808434 100644 --- a/core/library/think/Config.php +++ b/core/library/think/Config.php @@ -87,7 +87,7 @@ class Config return isset(self::$config[$range][strtolower($name)]); } else { // 二维数组设置和获取支持 - $name = explode('.', $name); + $name = explode('.', $name, 2); return isset(self::$config[$range][strtolower($name[0])][$name[1]]); } } @@ -111,7 +111,7 @@ class Config return isset(self::$config[$range][$name]) ? self::$config[$range][$name] : null; } else { // 二维数组设置和获取支持 - $name = explode('.', $name); + $name = explode('.', $name, 2); $name[0] = strtolower($name[0]); return isset(self::$config[$range][$name[0]][$name[1]]) ? self::$config[$range][$name[0]][$name[1]] : null; } @@ -135,7 +135,7 @@ class Config self::$config[$range][strtolower($name)] = $value; } else { // 二维数组设置和获取支持 - $name = explode('.', $name); + $name = explode('.', $name, 2); self::$config[$range][strtolower($name[0])][$name[1]] = $value; } return; diff --git a/core/library/think/Controller.php b/core/library/think/Controller.php index 69db0a1d..7aba39cd 100644 --- a/core/library/think/Controller.php +++ b/core/library/think/Controller.php @@ -40,7 +40,7 @@ class Controller protected $beforeActionList = []; /** - * 架构函数 + * 构造方法 * @param Request $request Request对象 * @access public */ diff --git a/core/library/think/Model.php b/core/library/think/Model.php index 61baa3a7..ebeffbb4 100644 --- a/core/library/think/Model.php +++ b/core/library/think/Model.php @@ -14,7 +14,7 @@ namespace think; use InvalidArgumentException; use think\db\Query; use think\Exception\ValidateException; -use think\model\Collection; +use think\model\Collection as ModelCollection; use think\model\Relation; use think\model\relation\BelongsTo; use think\model\relation\BelongsToMany; @@ -109,7 +109,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected static $initialized = []; /** - * 架构函数 + * 构造方法 * @access public * @param array|object $data 数据 */ @@ -624,7 +624,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 转换子模型对象 * @access protected - * @param Model|Collection $model + * @param Model|ModelCollection $model * @param $visible * @param $hidden * @param $key @@ -663,7 +663,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } foreach ($data as $key => $val) { - if ($val instanceof Model || $val instanceof Collection) { + if ($val instanceof Model || $val instanceof ModelCollection) { // 关联模型对象 $item[$key] = $this->subToArray($val, $visible, $hidden, $key); } elseif (is_array($val) && reset($val) instanceof Model) { @@ -712,14 +712,14 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 转换当前模型数据集为数据集对象 * @access public - * @param array|Collection $collection 数据集 - * @return Collection + * @param array|\think\Collection $collection 数据集 + * @return \think\Collection */ public function toCollection($collection) { if ($this->resultSetType) { if ('collection' == $this->resultSetType) { - $collection = new Collection($collection); + $collection = new ModelCollection($collection); } elseif (false !== strpos($this->resultSetType, '\\')) { $class = $this->resultSetType; $collection = new $class($collection); @@ -1081,7 +1081,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 删除条件 $pk = $this->getPk(); - if (isset($this->data[$pk])) { + if (is_string($pk) && isset($this->data[$pk])) { $where = [$pk => $this->data[$pk]]; } elseif (!empty($this->updateWhere)) { $where = $this->updateWhere; @@ -1422,15 +1422,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @param mixed $operator 比较操作符 * @param integer $count 个数 * @param string $id 关联表的统计字段 - * @return Model + * @return Relation|Query */ public static function has($relation, $operator = '>=', $count = 1, $id = '*') { - $model = new static(); + $relation = (new static())->$relation(); if (is_array($operator) || $operator instanceof \Closure) { - return $model->$relation()->hasWhere($operator); + return $relation->hasWhere($operator); } - return $model->$relation()->has($operator, $count, $id); + return $relation->has($operator, $count, $id); } /** @@ -1438,12 +1438,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @access public * @param string $relation 关联方法名 * @param mixed $where 查询条件(数组或者闭包) - * @return Model + * @return Relation|Query */ public static function hasWhere($relation, $where = []) { - $model = new static(); - return $model->$relation()->hasWhere($where); + return (new static())->$relation()->hasWhere($where); } /** @@ -1483,7 +1482,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $closure = $relation; $relation = $key; } - if (strpos($relation, '.')) { + if (is_array($relation)) { + $subRelation = $relation; + $relation = $key; + } elseif (strpos($relation, '.')) { list($relation, $subRelation) = explode('.', $relation, 2); } $method = Loader::parseName($relation, 1, false); @@ -1509,7 +1511,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $closure = $relation; $relation = $key; } - if (strpos($relation, '.')) { + if (is_array($relation)) { + $subRelation = $relation; + $relation = $key; + } elseif (strpos($relation, '.')) { list($relation, $subRelation) = explode('.', $relation, 2); } $relation = Loader::parseName($relation, 1, false); @@ -1535,7 +1540,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $closure = $relation; $relation = $key; } - if (strpos($relation, '.')) { + if (is_array($relation)) { + $subRelation = $relation; + $relation = $key; + } elseif (strpos($relation, '.')) { list($relation, $subRelation) = explode('.', $relation, 2); } $relation = Loader::parseName($relation, 1, false); @@ -1559,10 +1567,16 @@ abstract class Model implements \JsonSerializable, \ArrayAccess if ($relation instanceof \Closure) { $closure = $relation; $relation = $key; + } elseif (is_string($key)) { + $name = $relation; + $relation = $key; } $relation = Loader::parseName($relation, 1, false); $count = $this->$relation()->relationCount($result, $closure); - $result->setAttr(Loader::parseName($relation) . '_count', $count); + if (!isset($name)) { + $name = Loader::parseName($relation) . '_count'; + } + $result->setAttr($name, $count); } } diff --git a/core/library/think/Request.php b/core/library/think/Request.php index d39153fd..f32637d5 100644 --- a/core/library/think/Request.php +++ b/core/library/think/Request.php @@ -120,7 +120,7 @@ class Request protected $isCheckCache; /** - * 架构函数 + * 构造函数 * @access protected * @param array $options 参数 */ @@ -1523,13 +1523,13 @@ class Request } } // 自动缓存功能 - $key = '__URL__'; + $key = md5($this->host()) . '__URL__'; } elseif (strpos($key, '|')) { list($key, $fun) = explode('|', $key); } // 特殊规则替换 if (false !== strpos($key, '__')) { - $key = str_replace(['__MODULE__', '__CONTROLLER__', '__ACTION__', '__URL__'], [$this->module, $this->controller, $this->action, md5($this->url())], $key); + $key = str_replace(['__MODULE__', '__CONTROLLER__', '__ACTION__', '__URL__', ''], [$this->module, $this->controller, $this->action, md5($this->url())], $key); } if (false !== strpos($key, ':')) { diff --git a/core/library/think/Response.php b/core/library/think/Response.php index 656198f0..d0345782 100644 --- a/core/library/think/Response.php +++ b/core/library/think/Response.php @@ -40,7 +40,7 @@ class Response protected $content = null; /** - * 架构函数 + * 构造函数 * @access public * @param mixed $data 输出数据 * @param int $code diff --git a/core/library/think/Route.php b/core/library/think/Route.php index 1629d636..cf640a4a 100644 --- a/core/library/think/Route.php +++ b/core/library/think/Route.php @@ -232,6 +232,7 @@ class Route public static function rule($rule, $route = '', $type = '*', $option = [], $pattern = []) { $group = self::getGroup('name'); + if (!is_null($group)) { // 路由分组 $option = array_merge(self::getGroup('option'), $option); @@ -304,8 +305,12 @@ class Route } $vars = self::parseVar($rule); if (isset($name)) { - $key = $group ? $group . ($rule ? '/' . $rule : '') : $rule; - self::name($name, [$key, $vars, self::$domain]); + $key = $group ? $group . ($rule ? '/' . $rule : '') : $rule; + $suffix = isset($option['ext']) ? $option['ext'] : null; + self::name($name, [$key, $vars, self::$domain, $suffix]); + } + if (isset($option['modular'])) { + $route = $option['modular'] . '/' . $route; } if ($group) { if ('*' != $type) { @@ -447,7 +452,8 @@ class Route $vars = self::parseVar($key); $item[] = ['rule' => $key, 'route' => $route, 'var' => $vars, 'option' => $options, 'pattern' => $patterns]; // 设置路由标识 - self::name($route, [$name . ($key ? '/' . $key : ''), $vars, self::$domain]); + $suffix = isset($options['ext']) ? $options['ext'] : null; + self::name($route, [$name . ($key ? '/' . $key : ''), $vars, self::$domain, $suffix]); } self::$rules['*'][$name] = ['rule' => $item, 'route' => '', 'var' => [], 'option' => $option, 'pattern' => $pattern]; } @@ -1126,8 +1132,8 @@ class Route || (isset($option['ajax']) && !$option['ajax'] && $request->isAjax()) // 非Ajax检测 || (isset($option['pjax']) && $option['pjax'] && !$request->isPjax()) // Pjax检测 || (isset($option['pjax']) && !$option['pjax'] && $request->isPjax()) // 非Pjax检测 - || (isset($option['ext']) && false === stripos('|' . $option['ext'] . '|', $request->ext() ? '|' . $request->ext() . '|' : '')) // 伪静态后缀检测 - || (isset($option['deny_ext']) && false !== stripos('|' . $option['deny_ext'] . '|', $request->ext() ? '|' . $request->ext() . '|' : '')) + || (isset($option['ext']) && false === stripos('|' . $option['ext'] . '|', '|' . $request->ext() . '|')) // 伪静态后缀检测 + || (isset($option['deny_ext']) && false !== stripos('|' . $option['deny_ext'] . '|', '|' . $request->ext() . '|')) || (isset($option['domain']) && !in_array($option['domain'], [$_SERVER['HTTP_HOST'], self::$subDomain])) // 域名检测 || (isset($option['https']) && $option['https'] && !$request->isSsl()) // https检测 || (isset($option['https']) && !$option['https'] && $request->isSsl()) // https检测 @@ -1164,7 +1170,7 @@ class Route $len1 = substr_count($url, '|'); $len2 = substr_count($rule, '/'); // 多余参数是否合并 - $merge = !empty($option['merge_extra_vars']) ? true : false; + $merge = !empty($option['merge_extra_vars']); if ($merge && $len1 > $len2) { $url = str_replace('|', $depr, $url); $url = implode('|', explode($depr, $url, $len2 + 1)); diff --git a/core/library/think/Template.php b/core/library/think/Template.php index 66ba9e18..7e20407c 100644 --- a/core/library/think/Template.php +++ b/core/library/think/Template.php @@ -57,7 +57,7 @@ class Template protected $storage; /** - * 架构函数 + * 构造函数 * @access public */ public function __construct(array $config = []) diff --git a/core/library/think/Url.php b/core/library/think/Url.php index d7dfd973..de3cd912 100644 --- a/core/library/think/Url.php +++ b/core/library/think/Url.php @@ -80,6 +80,9 @@ class Url if (!empty($match[1])) { $domain = $match[1]; } + if (!is_null($match[2])) { + $suffix = $match[2]; + } } elseif (!empty($rule) && isset($name)) { throw new \InvalidArgumentException('route name not exists:' . $name); } else { @@ -288,18 +291,18 @@ class Url public static function getRuleUrl($rule, &$vars = []) { foreach ($rule as $item) { - list($url, $pattern, $domain) = $item; + list($url, $pattern, $domain, $suffix) = $item; if (empty($pattern)) { - return [$url, $domain]; + return [$url, $domain, $suffix]; } foreach ($pattern as $key => $val) { if (isset($vars[$key])) { $url = str_replace(['[:' . $key . ']', '<' . $key . '?>', ':' . $key . '', '<' . $key . '>'], $vars[$key], $url); unset($vars[$key]); - $result = [$url, $domain]; + $result = [$url, $domain, $suffix]; } elseif (2 == $val) { $url = str_replace(['/[:' . $key . ']', '[:' . $key . ']', '<' . $key . '?>'], '', $url); - $result = [$url, $domain]; + $result = [$url, $domain, $suffix]; } else { break; } diff --git a/core/library/think/Validate.php b/core/library/think/Validate.php index 2f728a60..718ed7a1 100644 --- a/core/library/think/Validate.php +++ b/core/library/think/Validate.php @@ -102,7 +102,7 @@ class Validate protected $batch = false; /** - * 架构函数 + * 构造函数 * @access public * @param array $rules 验证规则 * @param array $message 验证提示信息 diff --git a/core/library/think/View.php b/core/library/think/View.php index 020e5789..96ae56c9 100644 --- a/core/library/think/View.php +++ b/core/library/think/View.php @@ -25,7 +25,7 @@ class View protected $replace = []; /** - * 架构函数 + * 构造函数 * @access public * @param array $engine 模板引擎参数 * @param array $replace 字符串替换参数 diff --git a/core/library/think/cache/driver/File.php b/core/library/think/cache/driver/File.php index dba02c3e..5aadb2b1 100644 --- a/core/library/think/cache/driver/File.php +++ b/core/library/think/cache/driver/File.php @@ -28,7 +28,7 @@ class File extends Driver ]; /** - * 架构函数 + * 构造函数 * @param array $options */ public function __construct($options = []) @@ -225,6 +225,7 @@ class File extends Driver foreach ($files as $path) { if (is_dir($path)) { array_map('unlink', glob($path . '/*.php')); + rmdir($path); } else { unlink($path); } diff --git a/core/library/think/cache/driver/Lite.php b/core/library/think/cache/driver/Lite.php index 57727651..eeb96a19 100644 --- a/core/library/think/cache/driver/Lite.php +++ b/core/library/think/cache/driver/Lite.php @@ -26,7 +26,7 @@ class Lite extends Driver ]; /** - * 架构函数 + * 构造函数 * @access public * * @param array $options diff --git a/core/library/think/cache/driver/Memcache.php b/core/library/think/cache/driver/Memcache.php index d41939d8..1fbd3b08 100644 --- a/core/library/think/cache/driver/Memcache.php +++ b/core/library/think/cache/driver/Memcache.php @@ -25,7 +25,7 @@ class Memcache extends Driver ]; /** - * 架构函数 + * 构造函数 * @param array $options 缓存参数 * @access public * @throws \BadFunctionCallException diff --git a/core/library/think/cache/driver/Memcached.php b/core/library/think/cache/driver/Memcached.php index 35fafd07..fa312e8e 100644 --- a/core/library/think/cache/driver/Memcached.php +++ b/core/library/think/cache/driver/Memcached.php @@ -27,7 +27,7 @@ class Memcached extends Driver ]; /** - * 架构函数 + * 构造函数 * @param array $options 缓存参数 * @access public */ diff --git a/core/library/think/cache/driver/Redis.php b/core/library/think/cache/driver/Redis.php index 360f515a..4f618785 100644 --- a/core/library/think/cache/driver/Redis.php +++ b/core/library/think/cache/driver/Redis.php @@ -34,7 +34,7 @@ class Redis extends Driver ]; /** - * 架构函数 + * 构造函数 * @param array $options 缓存参数 * @access public */ diff --git a/core/library/think/cache/driver/Sqlite.php b/core/library/think/cache/driver/Sqlite.php index 76c592d8..305fd9e8 100644 --- a/core/library/think/cache/driver/Sqlite.php +++ b/core/library/think/cache/driver/Sqlite.php @@ -28,7 +28,7 @@ class Sqlite extends Driver ]; /** - * 架构函数 + * 构造函数 * @param array $options 缓存参数 * @throws \BadFunctionCallException * @access public diff --git a/core/library/think/cache/driver/Wincache.php b/core/library/think/cache/driver/Wincache.php index 5be8d0df..3076fc14 100644 --- a/core/library/think/cache/driver/Wincache.php +++ b/core/library/think/cache/driver/Wincache.php @@ -25,7 +25,7 @@ class Wincache extends Driver ]; /** - * 架构函数 + * 构造函数 * @param array $options 缓存参数 * @throws \BadFunctionCallException * @access public diff --git a/core/library/think/cache/driver/Xcache.php b/core/library/think/cache/driver/Xcache.php index 317a4ee3..2a0e07ad 100644 --- a/core/library/think/cache/driver/Xcache.php +++ b/core/library/think/cache/driver/Xcache.php @@ -25,7 +25,7 @@ class Xcache extends Driver ]; /** - * 架构函数 + * 构造函数 * @param array $options 缓存参数 * @access public * @throws \BadFunctionCallException diff --git a/core/library/think/controller/Rest.php b/core/library/think/controller/Rest.php index c297f4eb..8c5911df 100644 --- a/core/library/think/controller/Rest.php +++ b/core/library/think/controller/Rest.php @@ -32,7 +32,7 @@ abstract class Rest ]; /** - * 架构函数 取得模板对象实例 + * 构造函数 取得模板对象实例 * @access public */ public function __construct() diff --git a/core/library/think/controller/Yar.php b/core/library/think/controller/Yar.php index fcf5ced1..af4e9a1a 100644 --- a/core/library/think/controller/Yar.php +++ b/core/library/think/controller/Yar.php @@ -18,7 +18,7 @@ abstract class Yar { /** - * 架构函数 + * 构造函数 * @access public */ public function __construct() diff --git a/core/library/think/db/Builder.php b/core/library/think/db/Builder.php index 9034076e..56665527 100644 --- a/core/library/think/db/Builder.php +++ b/core/library/think/db/Builder.php @@ -32,7 +32,7 @@ abstract class Builder protected $deleteSql = 'DELETE FROM %TABLE% %USING% %JOIN% %WHERE% %ORDER%%LIMIT% %LOCK%%COMMENT%'; /** - * 架构函数 + * 构造函数 * @access public * @param Connection $connection 数据库连接对象实例 * @param Query $query 数据库查询对象实例 diff --git a/core/library/think/db/Connection.php b/core/library/think/db/Connection.php index 0887093d..bc67b1d8 100644 --- a/core/library/think/db/Connection.php +++ b/core/library/think/db/Connection.php @@ -125,7 +125,7 @@ abstract class Connection protected $bind = []; /** - * 架构函数 读取数据库配置信息 + * 构造函数 读取数据库配置信息 * @access public * @param array $config 数据库配置数组 */ @@ -365,8 +365,8 @@ abstract class Connection $this->bind = $bind; } - //释放前次的查询结果 - if (!empty($this->PDOStatement) && $this->PDOStatement->queryString != $sql) { + // 释放前次的查询结果 + if (!empty($this->PDOStatement)) { $this->free(); } diff --git a/core/library/think/db/Query.php b/core/library/think/db/Query.php index 3d080dcc..cd5e9a2c 100644 --- a/core/library/think/db/Query.php +++ b/core/library/think/db/Query.php @@ -54,7 +54,7 @@ class Query private static $event = []; /** - * 架构函数 + * 构造函数 * @access public * @param Connection $connection 数据库对象实例 * @param string $model 模型名 @@ -869,7 +869,7 @@ class Query public function view($join, $field = true, $on = null, $type = 'INNER') { $this->options['view'] = true; - if (is_array($join) && is_null($field)) { + if (is_array($join) && key($join) !== 0) { foreach ($join as $key => $val) { $this->view($key, $val[0], isset($val[1]) ? $val[1] : null, isset($val[2]) ? $val[2] : 'INNER'); } @@ -2065,8 +2065,10 @@ class Query $sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null); $lastInsId = $this->getLastInsID($sequence); if ($lastInsId) { - $pk = $this->getPk($options); - $data[$pk] = $lastInsId; + $pk = $this->getPk($options); + if (is_string($pk)) { + $data[$pk] = $lastInsId; + } } $options['data'] = $data; $this->trigger('after_insert', $options); @@ -2202,6 +2204,8 @@ class Query if (isset($key) && Cache::get($key)) { // 删除缓存 Cache::rm($key); + } elseif (!empty($options['cache']['tag'])) { + Cache::clear($options['cache']['tag']); } // 执行操作 $result = '' == $sql ? 0 : $this->execute($sql, $bind); @@ -2375,6 +2379,8 @@ class Query } if (isset($data)) { return 'think:' . $options['table'] . '|' . $data; + } else { + return md5(serialize($options)); } } @@ -2412,8 +2418,10 @@ class Query $cache = $options['cache']; if (true === $cache['key'] && !is_null($data) && !is_array($data)) { $key = 'think:' . (is_array($options['table']) ? key($options['table']) : $options['table']) . '|' . $data; + } elseif (is_string($cache['key'])) { + $key = $cache['key']; } elseif (!isset($key)) { - $key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options)); + $key = md5(serialize($options)); } $result = Cache::get($key); } @@ -2644,6 +2652,8 @@ class Query if (isset($key) && Cache::get($key)) { // 删除缓存 Cache::rm($key); + } elseif (!empty($options['cache']['tag'])) { + Cache::clear($options['cache']['tag']); } // 执行操作 $result = $this->execute($sql, $bind); diff --git a/core/library/think/db/builder/Mysql.php b/core/library/think/db/builder/Mysql.php index de38fac5..e432e396 100644 --- a/core/library/think/db/builder/Mysql.php +++ b/core/library/think/db/builder/Mysql.php @@ -38,6 +38,8 @@ class Mysql extends Builder list($table, $key) = explode('.', $key, 2); if (isset($options['alias'][$table])) { $table = $options['alias'][$table]; + } elseif ('__TABLE__' == $table) { + $table = $this->query->getTable(); } } if (!preg_match('/[,\'\"\*\(\)`.\s]/', $key)) { diff --git a/core/library/think/db/builder/Pgsql.php b/core/library/think/db/builder/Pgsql.php index 8b853a2f..67b98b44 100644 --- a/core/library/think/db/builder/Pgsql.php +++ b/core/library/think/db/builder/Pgsql.php @@ -57,6 +57,8 @@ class Pgsql extends Builder list($table, $key) = explode('.', $key, 2); if (isset($options['alias'][$table])) { $table = $options['alias'][$table]; + } elseif ('__TABLE__' == $table) { + $table = $this->query->getTable(); } } if (isset($table)) { diff --git a/core/library/think/db/builder/Sqlite.php b/core/library/think/db/builder/Sqlite.php index 02d1bf2e..680b4965 100644 --- a/core/library/think/db/builder/Sqlite.php +++ b/core/library/think/db/builder/Sqlite.php @@ -62,6 +62,8 @@ class Sqlite extends Builder list($table, $key) = explode('.', $key, 2); if (isset($options['alias'][$table])) { $table = $options['alias'][$table]; + } elseif ('__TABLE__' == $table) { + $table = $this->query->getTable(); } } if (isset($table)) { diff --git a/core/library/think/db/builder/Sqlsrv.php b/core/library/think/db/builder/Sqlsrv.php index d2f418f3..99f68409 100644 --- a/core/library/think/db/builder/Sqlsrv.php +++ b/core/library/think/db/builder/Sqlsrv.php @@ -75,6 +75,8 @@ class Sqlsrv extends Builder list($table, $key) = explode('.', $key, 2); if (isset($options['alias'][$table])) { $table = $options['alias'][$table]; + } elseif ('__TABLE__' == $table) { + $table = $this->query->getTable(); } } if (!is_numeric($key) && !preg_match('/[,\'\"\*\(\)\[.\s]/', $key)) { diff --git a/core/library/think/log/driver/Socket.php b/core/library/think/log/driver/Socket.php index 08fd8ace..d30bba30 100644 --- a/core/library/think/log/driver/Socket.php +++ b/core/library/think/log/driver/Socket.php @@ -43,7 +43,7 @@ class Socket protected $allowForceClientIds = []; //配置强制推送且被授权的client_id /** - * 架构函数 + * 构造函数 * @param array $config 缓存参数 * @access public */ diff --git a/core/library/think/model/Merge.php b/core/library/think/model/Merge.php index 47d54923..01f403f8 100644 --- a/core/library/think/model/Merge.php +++ b/core/library/think/model/Merge.php @@ -22,7 +22,7 @@ class Merge extends Model protected $mapFields = []; // 需要处理的模型映射字段,避免混淆 array( id => 'user.id' ) /** - * 架构函数 + * 构造函数 * @access public * @param array|object $data 数据 */ diff --git a/core/library/think/model/Pivot.php b/core/library/think/model/Pivot.php index 7823370c..9fd45719 100644 --- a/core/library/think/model/Pivot.php +++ b/core/library/think/model/Pivot.php @@ -17,7 +17,7 @@ class Pivot extends Model { /** - * 架构函数 + * 构造函数 * @access public * @param array|object $data 数据 * @param string $table 中间数据表名 diff --git a/core/library/think/model/relation/BelongsTo.php b/core/library/think/model/relation/BelongsTo.php index d1c4b9bd..6996526d 100644 --- a/core/library/think/model/relation/BelongsTo.php +++ b/core/library/think/model/relation/BelongsTo.php @@ -17,7 +17,7 @@ use think\Model; class BelongsTo extends OneToOne { /** - * 架构函数 + * 构造函数 * @access public * @param Model $parent 上级模型对象 * @param string $model 模型名 @@ -51,6 +51,45 @@ class BelongsTo extends OneToOne return $this->query->where($this->localKey, $this->parent->$foreignKey)->relation($subRelation)->find(); } + /** + * 根据关联条件查询当前模型 + * @access public + * @param string $operator 比较操作符 + * @param integer $count 个数 + * @param string $id 关联表的统计字段 + * @param string $joinType JOIN类型 + * @return Query + */ + public function has($operator = '>=', $count = 1, $id = '*') + { + return $this->parent; + } + + /** + * 根据关联条件查询当前模型 + * @access public + * @param mixed $where 查询条件(数组或者闭包) + * @return Query + */ + public function hasWhere($where = []) + { + $table = $this->query->getTable(); + $model = basename(str_replace('\\', '/', get_class($this->parent))); + $relation = basename(str_replace('\\', '/', $this->model)); + if (is_array($where)) { + foreach ($where as $key => $val) { + if (false === strpos($key, '.')) { + $where[$relation . '.' . $key] = $val; + unset($where[$key]); + } + } + } + return $this->parent->db()->alias($model) + ->field($model . '.*') + ->join($table . ' ' . $relation, $model . '.' . $this->foreignKey . '=' . $relation . '.' . $this->localKey, $this->joinType) + ->where($where); + } + /** * 预载入关联查询(数据集) * @access public @@ -85,10 +124,10 @@ class BelongsTo extends OneToOne // 关联数据封装 foreach ($resultSet as $result) { // 关联模型 - if (!isset($data[$result->$localKey])) { + if (!isset($data[$result->$foreignKey])) { $relationModel = null; } else { - $relationModel = $data[$result->$localKey]; + $relationModel = $data[$result->$foreignKey]; } if ($relationModel && !empty($this->bindAttr)) { @@ -116,10 +155,10 @@ class BelongsTo extends OneToOne $foreignKey = $this->foreignKey; $data = $this->eagerlyWhere($this, [$localKey => $result->$foreignKey], $localKey, $relation, $subRelation, $closure); // 关联模型 - if (!isset($data[$result->$localKey])) { + if (!isset($data[$result->$foreignKey])) { $relationModel = null; } else { - $relationModel = $data[$result->$localKey]; + $relationModel = $data[$result->$foreignKey]; } if ($relationModel && !empty($this->bindAttr)) { // 绑定关联属性 diff --git a/core/library/think/model/relation/BelongsToMany.php b/core/library/think/model/relation/BelongsToMany.php index 128287e0..b7758e9f 100644 --- a/core/library/think/model/relation/BelongsToMany.php +++ b/core/library/think/model/relation/BelongsToMany.php @@ -24,7 +24,7 @@ class BelongsToMany extends Relation protected $middle; /** - * 架构函数 + * 构造函数 * @access public * @param Model $parent 上级模型对象 * @param string $model 模型名 @@ -76,6 +76,31 @@ class BelongsToMany extends Relation return $result; } + /** + * 根据关联条件查询当前模型 + * @access public + * @param string $operator 比较操作符 + * @param integer $count 个数 + * @param string $id 关联表的统计字段 + * @param string $joinType JOIN类型 + * @return Query + */ + public function has($operator = '>=', $count = 1, $id = '*', $joinType = 'INNER') + { + return $this->parent; + } + + /** + * 根据关联条件查询当前模型 + * @access public + * @param mixed $where 查询条件(数组或者闭包) + * @return Query + */ + public function hasWhere($where = []) + { + throw new Exception('relation not support: hasWhere'); + } + /** * 预载入关联查询(数据集) * @access public diff --git a/core/library/think/model/relation/HasMany.php b/core/library/think/model/relation/HasMany.php index 9bcdcead..364d793d 100644 --- a/core/library/think/model/relation/HasMany.php +++ b/core/library/think/model/relation/HasMany.php @@ -20,7 +20,7 @@ use think\model\Relation; class HasMany extends Relation { /** - * 架构函数 + * 构造函数 * @access public * @param Model $parent 上级模型对象 * @param string $model 模型名 diff --git a/core/library/think/model/relation/HasManyThrough.php b/core/library/think/model/relation/HasManyThrough.php index e1518972..dd6f6a55 100644 --- a/core/library/think/model/relation/HasManyThrough.php +++ b/core/library/think/model/relation/HasManyThrough.php @@ -13,6 +13,7 @@ namespace think\model\relation; use think\Db; use think\db\Query; +use think\Exception; use think\Loader; use think\Model; use think\model\Relation; @@ -25,7 +26,7 @@ class HasManyThrough extends Relation protected $through; /** - * 架构函数 + * 构造函数 * @access public * @param Model $parent 上级模型对象 * @param string $model 模型名 @@ -54,11 +55,36 @@ class HasManyThrough extends Relation public function getRelation($subRelation = '', $closure = null) { if ($closure) { - call_user_func_array($closure, [& $this->query]); + call_user_func_array($closure, [ & $this->query]); } return $this->relation($subRelation)->select(); } + /** + * 根据关联条件查询当前模型 + * @access public + * @param string $operator 比较操作符 + * @param integer $count 个数 + * @param string $id 关联表的统计字段 + * @param string $joinType JOIN类型 + * @return Query + */ + public function has($operator = '>=', $count = 1, $id = '*', $joinType = 'INNER') + { + return $this->parent; + } + + /** + * 根据关联条件查询当前模型 + * @access public + * @param mixed $where 查询条件(数组或者闭包) + * @return Query + */ + public function hasWhere($where = []) + { + throw new Exception('relation not support: hasWhere'); + } + /** * 预载入关联查询 * @access public diff --git a/core/library/think/model/relation/HasOne.php b/core/library/think/model/relation/HasOne.php index 2c0cf350..95a401bf 100644 --- a/core/library/think/model/relation/HasOne.php +++ b/core/library/think/model/relation/HasOne.php @@ -18,7 +18,7 @@ use think\Model; class HasOne extends OneToOne { /** - * 架构函数 + * 构造函数 * @access public * @param Model $parent 上级模型对象 * @param string $model 模型名 @@ -47,12 +47,28 @@ class HasOne extends OneToOne // 执行关联定义方法 $localKey = $this->localKey; if ($closure) { - call_user_func_array($closure, [& $this->query]); + call_user_func_array($closure, [ & $this->query]); } // 判断关联类型执行查询 return $this->query->where($this->foreignKey, $this->parent->$localKey)->relation($subRelation)->find(); } + /** + * 根据关联条件查询当前模型 + * @access public + * @return Query + */ + public function has() + { + $table = $this->query->getTable(); + $localKey = $this->localKey; + $foreignKey = $this->foreignKey; + return $this->parent->db()->alias('a') + ->whereExists(function ($query) use ($table, $localKey, $foreignKey) { + $query->table([$table => 'b'])->field('b.' . $foreignKey)->whereExp('a.' . $localKey, '=b.' . $foreignKey); + }); + } + /** * 根据关联条件查询当前模型 * @access public diff --git a/core/library/think/model/relation/MorphMany.php b/core/library/think/model/relation/MorphMany.php index 3fc8179c..58e44135 100644 --- a/core/library/think/model/relation/MorphMany.php +++ b/core/library/think/model/relation/MorphMany.php @@ -13,6 +13,7 @@ namespace think\model\relation; use think\Db; use think\db\Query; +use think\Exception; use think\Loader; use think\Model; use think\model\Relation; @@ -26,7 +27,7 @@ class MorphMany extends Relation protected $type; /** - * 架构函数 + * 构造函数 * @access public * @param Model $parent 上级模型对象 * @param string $model 模型名 @@ -53,11 +54,36 @@ class MorphMany extends Relation public function getRelation($subRelation = '', $closure = null) { if ($closure) { - call_user_func_array($closure, [& $this->query]); + call_user_func_array($closure, [ & $this->query]); } return $this->relation($subRelation)->select(); } + /** + * 根据关联条件查询当前模型 + * @access public + * @param string $operator 比较操作符 + * @param integer $count 个数 + * @param string $id 关联表的统计字段 + * @param string $joinType JOIN类型 + * @return Query + */ + public function has($operator = '>=', $count = 1, $id = '*', $joinType = 'INNER') + { + throw new Exception('relation not support: has'); + } + + /** + * 根据关联条件查询当前模型 + * @access public + * @param mixed $where 查询条件(数组或者闭包) + * @return Query + */ + public function hasWhere($where = []) + { + throw new Exception('relation not support: hasWhere'); + } + /** * 预载入关联查询 * @access public @@ -113,7 +139,7 @@ class MorphMany extends Relation if (isset($result->$pk)) { $data = $this->eagerlyMorphToMany([ $this->morphKey => $result->$pk, - $this->morphType => $this->type + $this->morphType => $this->type, ], $relation, $subRelation, $closure); $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$pk])); } @@ -132,7 +158,7 @@ class MorphMany extends Relation $count = 0; if (isset($result->$pk)) { if ($closure) { - call_user_func_array($closure, [& $this->query]); + call_user_func_array($closure, [ & $this->query]); } $count = $this->query->where([$this->morphKey => $result->$pk, $this->morphType => $this->type])->count(); } @@ -148,15 +174,15 @@ class MorphMany extends Relation public function getRelationCountQuery($closure) { if ($closure) { - call_user_func_array($closure, [& $this->query]); + call_user_func_array($closure, [ & $this->query]); } return $this->query->where([ $this->morphKey => [ 'exp', - '=' . $this->parent->getTable() . '.' . $this->parent->getPk() + '=' . $this->parent->getTable() . '.' . $this->parent->getPk(), ], - $this->morphType => $this->type + $this->morphType => $this->type, ])->fetchSql()->count(); } @@ -173,7 +199,7 @@ class MorphMany extends Relation { // 预载入关联查询 支持嵌套预载入 if ($closure) { - call_user_func_array($closure, [& $this]); + call_user_func_array($closure, [ & $this]); } $list = $this->query->where($where)->with($subRelation)->select(); $morphKey = $this->morphKey; diff --git a/core/library/think/model/relation/MorphTo.php b/core/library/think/model/relation/MorphTo.php index 5e1867f2..6c31a752 100644 --- a/core/library/think/model/relation/MorphTo.php +++ b/core/library/think/model/relation/MorphTo.php @@ -25,7 +25,7 @@ class MorphTo extends Relation protected $alias; /** - * 架构函数 + * 构造函数 * @access public * @param Model $parent 上级模型对象 * @param string $morphType 多态字段名 @@ -57,6 +57,31 @@ class MorphTo extends Relation return (new $model)->relation($subRelation)->find($pk); } + /** + * 根据关联条件查询当前模型 + * @access public + * @param string $operator 比较操作符 + * @param integer $count 个数 + * @param string $id 关联表的统计字段 + * @param string $joinType JOIN类型 + * @return Query + */ + public function has($operator = '>=', $count = 1, $id = '*', $joinType = 'INNER') + { + return $this->parent; + } + + /** + * 根据关联条件查询当前模型 + * @access public + * @param mixed $where 查询条件(数组或者闭包) + * @return Query + */ + public function hasWhere($where = []) + { + throw new Exception('relation not support: hasWhere'); + } + /** * 解析模型的完整命名空间 * @access public diff --git a/core/library/think/template/TagLib.php b/core/library/think/template/TagLib.php index b3325191..2e0d232f 100644 --- a/core/library/think/template/TagLib.php +++ b/core/library/think/template/TagLib.php @@ -68,7 +68,7 @@ class TagLib protected $comparison = [' nheq ' => ' !== ', ' heq ' => ' === ', ' neq ' => ' != ', ' eq ' => ' == ', ' egt ' => ' >= ', ' gt ' => ' > ', ' elt ' => ' <= ', ' lt ' => ' < ']; /** - * 架构函数 + * 构造函数 * @access public * @param \stdClass $template 模板引擎对象 */ diff --git a/core/library/traits/model/SoftDelete.php b/core/library/traits/model/SoftDelete.php index 720b6e88..2b97ff72 100644 --- a/core/library/traits/model/SoftDelete.php +++ b/core/library/traits/model/SoftDelete.php @@ -142,7 +142,7 @@ trait SoftDelete { $field = isset($this->deleteTime) ? $this->deleteTime : 'delete_time'; if (!strpos($field, '.')) { - $field = $this->db(false)->getTable() . '.' . $field; + $field = '__TABLE__.' . $field; } if (!$read && strpos($field, '.')) { $array = explode('.', $field); diff --git a/core/tpl/think_exception.tpl b/core/tpl/think_exception.tpl index e80c57b2..2d898c30 100644 --- a/core/tpl/think_exception.tpl +++ b/core/tpl/think_exception.tpl @@ -79,15 +79,17 @@ - 系统发生错误 + <?php echo lang('System Error'); ?> +