From 81d9f5e5e54c56cd4f275d9fd369c20084c9ea86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B7=B4=E6=8E=8C?= Date: Thu, 1 Nov 2018 09:01:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7=E5=88=B05.1.28=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.lock | 10 +- thinkphp/README.md | 4 +- thinkphp/library/think/App.php | 2 +- thinkphp/library/think/Controller.php | 59 ++++++------ thinkphp/library/think/Lang.php | 2 +- thinkphp/library/think/Log.php | 24 ++--- thinkphp/library/think/Url.php | 6 +- thinkphp/library/think/Validate.php | 38 +++++++- thinkphp/library/think/db/Connection.php | 6 +- thinkphp/library/think/db/Query.php | 14 +-- thinkphp/library/think/model/Relation.php | 2 +- .../library/think/model/concern/Attribute.php | 3 +- .../think/model/concern/RelationShip.php | 7 +- .../think/model/relation/BelongsTo.php | 49 ++++++++-- .../think/model/relation/BelongsToMany.php | 94 +++++++++++++++---- .../library/think/model/relation/HasMany.php | 32 ++++--- .../think/model/relation/HasManyThrough.php | 3 +- .../library/think/model/relation/HasOne.php | 39 +++++++- .../think/model/relation/MorphMany.php | 43 +++++---- .../library/think/model/relation/MorphTo.php | 3 +- .../library/think/model/relation/OneToOne.php | 14 --- .../library/think/route/dispatch/Module.php | 5 + vendor/composer/installed.json | 12 +-- 23 files changed, 321 insertions(+), 150 deletions(-) diff --git a/composer.lock b/composer.lock index 16e01ad2..1e8057a9 100644 --- a/composer.lock +++ b/composer.lock @@ -298,16 +298,16 @@ }, { "name": "topthink/framework", - "version": "v5.1.27", + "version": "v5.1.28", "source": { "type": "git", "url": "https://github.com/top-think/framework.git", - "reference": "f607b8fd51cc25e4da7e2f4ef0d5e64f390d36bd" + "reference": "b5077520855a1f406205687c73cdeaee1ed618c2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/top-think/framework/zipball/f607b8fd51cc25e4da7e2f4ef0d5e64f390d36bd", - "reference": "f607b8fd51cc25e4da7e2f4ef0d5e64f390d36bd", + "url": "https://api.github.com/repos/top-think/framework/zipball/b5077520855a1f406205687c73cdeaee1ed618c2", + "reference": "b5077520855a1f406205687c73cdeaee1ed618c2", "shasum": "", "mirrors": [ { @@ -351,7 +351,7 @@ "orm", "thinkphp" ], - "time": "2018-10-22T02:10:08+00:00" + "time": "2018-10-29T02:52:45+00:00" }, { "name": "topthink/think-captcha", diff --git a/thinkphp/README.md b/thinkphp/README.md index 92731cb2..a30e8f0e 100644 --- a/thinkphp/README.md +++ b/thinkphp/README.md @@ -1,6 +1,6 @@ ![](http://www.thinkphp.cn/Uploads/editor/2016-06-23/576b4732a6e04.png) -ThinkPHP 5.1 —— 12载初心,你值得信赖的PHP框架 +ThinkPHP 5.1(LTS) —— 12载初心,你值得信赖的PHP框架 =============== [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/top-think/framework/badges/quality-score.png?b=5.1)](https://scrutinizer-ci.com/g/top-think/framework/?branch=5.1) @@ -27,6 +27,7 @@ ThinkPHP5.1对底层架构做了进一步的改进,减少依赖,其主要特 + 中间件支持(`V5.1.6+`) + 支持`Swoole`/`Workerman`运行(`V5.1.18+`) +官方已经正式宣布`5.1.27`版本为LTS版本。 ### 废除的功能: @@ -36,6 +37,7 @@ ThinkPHP5.1对底层架构做了进一步的改进,减少依赖,其主要特 > ThinkPHP5.1的运行环境要求PHP5.6+。 + ## 安装 使用composer安装 diff --git a/thinkphp/library/think/App.php b/thinkphp/library/think/App.php index d888e3c9..254f53a9 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.27 LTS'; + const VERSION = '5.1.28 LTS'; /** * 当前模块路径 diff --git a/thinkphp/library/think/Controller.php b/thinkphp/library/think/Controller.php index e0966eb2..a57da9e3 100644 --- a/thinkphp/library/think/Controller.php +++ b/thinkphp/library/think/Controller.php @@ -67,35 +67,6 @@ class Controller // 控制器初始化 $this->initialize(); - // 控制器中间件 - if ($this->middleware) { - foreach ($this->middleware as $key => $val) { - if (!is_int($key)) { - $only = $except = null; - - if (isset($val['only'])) { - $only = array_map(function ($item) { - return strtolower($item); - }, $val['only']); - } elseif (isset($val['except'])) { - $except = array_map(function ($item) { - return strtolower($item); - }, $val['except']); - } - - if (isset($only) && !in_array($this->request->action(), $only)) { - continue; - } elseif (isset($except) && in_array($this->request->action(), $except)) { - continue; - } else { - $val = $key; - } - } - - $this->app['middleware']->controller($val); - } - } - // 前置操作方法 即将废弃 foreach ((array) $this->beforeActionList as $method => $options) { is_numeric($method) ? @@ -108,6 +79,36 @@ class Controller protected function initialize() {} + // 注册控制器中间件 + public function registerMiddleware() + { + foreach ($this->middleware as $key => $val) { + if (!is_int($key)) { + $only = $except = null; + + if (isset($val['only'])) { + $only = array_map(function ($item) { + return strtolower($item); + }, $val['only']); + } elseif (isset($val['except'])) { + $except = array_map(function ($item) { + return strtolower($item); + }, $val['except']); + } + + if (isset($only) && !in_array($this->request->action(), $only)) { + continue; + } elseif (isset($except) && in_array($this->request->action(), $except)) { + continue; + } else { + $val = $key; + } + } + + $this->app['middleware']->controller($val); + } + } + /** * 前置操作 * @access protected diff --git a/thinkphp/library/think/Lang.php b/thinkphp/library/think/Lang.php index 5d8d9d99..be7979f8 100644 --- a/thinkphp/library/think/Lang.php +++ b/thinkphp/library/think/Lang.php @@ -161,7 +161,7 @@ class Lang $range = $range ?: $this->range; // 空参数返回所有定义 - if (empty($name)) { + if (is_null($name)) { return $this->lang[$range]; } diff --git a/thinkphp/library/think/Log.php b/thinkphp/library/think/Log.php index 70c7bb9a..1a374962 100644 --- a/thinkphp/library/think/Log.php +++ b/thinkphp/library/think/Log.php @@ -205,26 +205,18 @@ class Log implements LoggerInterface return false; } - if (empty($this->config['level'])) { - // 获取全部日志 - $log = $this->log; - if (!$this->app->isDebug() && isset($log['debug'])) { - unset($log['debug']); + $log = []; + + foreach ($this->log as $level => $info) { + if (!$this->app->isDebug() && 'debug' == $level) { + continue; } - foreach ($log as $level => $info) { + if (empty($this->config['level']) || in_array($level, $this->config['level'])) { + $log[$level] = $info; + $this->app['hook']->listen('log_level', [$level, $info]); } - } else { - // 记录允许级别 - $log = []; - foreach ($this->config['level'] as $level) { - if (isset($this->log[$level])) { - $log[$level] = $this->log[$level]; - // 监听log_level - $this->app['hook']->listen('log_level', [$level, $log[$level]]); - } - } } $result = $this->driver->save($log, true); diff --git a/thinkphp/library/think/Url.php b/thinkphp/library/think/Url.php index 6707bf28..9e41b881 100644 --- a/thinkphp/library/think/Url.php +++ b/thinkphp/library/think/Url.php @@ -348,12 +348,16 @@ class Url public function getRuleUrl($rule, &$vars = [], $allowDomain = '') { foreach ($rule as $item) { - list($url, $pattern, $domain, $suffix) = $item; + list($url, $pattern, $domain, $suffix, $method) = $item; if (is_string($allowDomain) && $domain != $allowDomain) { continue; } + if ($this->app['request']->port() != 80) { + $domain .= ':' . $this->app['request']->port(); + } + if (empty($pattern)) { return [rtrim($url, '?/-'), $domain, $suffix]; } diff --git a/thinkphp/library/think/Validate.php b/thinkphp/library/think/Validate.php index de46956c..ebc07293 100644 --- a/thinkphp/library/think/Validate.php +++ b/thinkphp/library/think/Validate.php @@ -87,6 +87,8 @@ class Validate 'min' => 'min size of :attribute must be :rule', 'after' => ':attribute cannot be less than :rule', 'before' => ':attribute cannot exceed :rule', + 'afterWith' => ':attribute cannot be less than :rule', + 'beforeWith' => ':attribute cannot exceed :rule', 'expire' => ':attribute not within :rule', 'allowIp' => 'access IP is not allowed', 'denyIp' => 'access IP denied', @@ -422,7 +424,7 @@ class Validate // 字段验证 if ($rule instanceof \Closure) { - $result = call_user_func_array($rule, [$value, $data]); + $result = call_user_func_array($rule, [$value, $data, $title, $this]); } elseif ($rule instanceof ValidateRule) { // 验证因子 $result = $this->checkItem($key, $value, $rule->getRule(), $data, $rule->getTitle() ?: $title, $rule->getMsg()); @@ -1241,9 +1243,10 @@ class Validate * @access public * @param mixed $value 字段值 * @param mixed $rule 验证规则 + * @param array $data 数据 * @return bool */ - public function after($value, $rule) + public function after($value, $rule, $data) { return strtotime($value) >= strtotime($rule); } @@ -1253,13 +1256,42 @@ class Validate * @access public * @param mixed $value 字段值 * @param mixed $rule 验证规则 + * @param array $data 数据 * @return bool */ - public function before($value, $rule) + public function before($value, $rule, $data) { return strtotime($value) <= strtotime($rule); } + /** + * 验证日期字段 + * @access protected + * @param mixed $value 字段值 + * @param mixed $rule 验证规则 + * @param array $data 数据 + * @return bool + */ + protected function afterWith($value, $rule, $data) + { + $rule = $this->getDataValue($data, $rule); + return !is_null($rule) && strtotime($value) >= strtotime($rule); + } + + /** + * 验证日期字段 + * @access protected + * @param mixed $value 字段值 + * @param mixed $rule 验证规则 + * @param array $data 数据 + * @return bool + */ + protected function beforeWith($value, $rule, $data) + { + $rule = $this->getDataValue($data, $rule); + return !is_null($rule) && strtotime($value) <= strtotime($rule); + } + /** * 验证有效期 * @access public diff --git a/thinkphp/library/think/db/Connection.php b/thinkphp/library/think/db/Connection.php index d2bad3c0..6c337bc2 100644 --- a/thinkphp/library/think/db/Connection.php +++ b/thinkphp/library/think/db/Connection.php @@ -1314,7 +1314,11 @@ abstract class Connection */ public function aggregate(Query $query, $aggregate, $field) { - $field = $aggregate . '(' . $this->builder->parseKey($query, $field, true) . ') AS tp_' . strtolower($aggregate); + if (0 === stripos($field, 'DISTINCT ')) { + list($distinct, $field) = explode(' ', $field); + } + + $field = $aggregate . '(' . (!empty($distinct) ? 'DISTINCT ' : '') . $this->builder->parseKey($query, $field, true) . ') AS tp_' . strtolower($aggregate); return $this->value($query, $field, 0); } diff --git a/thinkphp/library/think/db/Query.php b/thinkphp/library/think/db/Query.php index cea5a4c8..d48b95c6 100644 --- a/thinkphp/library/think/db/Query.php +++ b/thinkphp/library/think/db/Query.php @@ -2650,7 +2650,8 @@ class Query } foreach ($relations as $key => $relation) { - $closure = null; + $closure = $aggregateField = null; + if ($relation instanceof \Closure) { $closure = $relation; $relation = $key; @@ -2659,14 +2660,15 @@ class Query $relation = $key; } - if (!isset($aggregateField)) { + $relation = Loader::parseName($relation, 1, false); + + $count = $this->model->$relation()->getRelationCountQuery($closure, $aggregate, $field, $aggregateField); + + if (empty($aggregateField)) { $aggregateField = Loader::parseName($relation) . '_' . $aggregate; } - $relation = Loader::parseName($relation, 1, false); - $count = '(' . $this->model->$relation()->getRelationCountQuery($closure, $aggregate, $field) . ')'; - - $this->field([$count => $aggregateField]); + $this->field(['(' . $count . ')' => $aggregateField]); } } diff --git a/thinkphp/library/think/model/Relation.php b/thinkphp/library/think/model/Relation.php index b1434856..c2b9adc7 100644 --- a/thinkphp/library/think/model/Relation.php +++ b/thinkphp/library/think/model/Relation.php @@ -158,7 +158,7 @@ abstract class Relation $result = call_user_func_array([$this->query->getModel(), $method], $args); - return $result === $this->query ? $this : $result; + return $result === $this->query && !in_array(strtolower($method), ['fetchsql', 'fetchpdo']) ? $this : $result; } else { throw new Exception('method not exists:' . __CLASS__ . '->' . $method); } diff --git a/thinkphp/library/think/model/concern/Attribute.php b/thinkphp/library/think/model/concern/Attribute.php index 76342b9f..02f62b14 100644 --- a/thinkphp/library/think/model/concern/Attribute.php +++ b/thinkphp/library/think/model/concern/Attribute.php @@ -327,6 +327,8 @@ trait Attribute if (method_exists($this, $method)) { $value = $this->$method($value, array_merge($this->data, $data)); + + $this->set[$name] = true; } elseif (isset($this->type[$name])) { // 类型转换 $value = $this->writeTransform($value, $this->type[$name]); @@ -335,7 +337,6 @@ trait Attribute // 设置数据对象属性 $this->data[$name] = $value; - $this->set[$name] = true; } /** diff --git a/thinkphp/library/think/model/concern/RelationShip.php b/thinkphp/library/think/model/concern/RelationShip.php index f4c091b6..38ad5d20 100644 --- a/thinkphp/library/think/model/concern/RelationShip.php +++ b/thinkphp/library/think/model/concern/RelationShip.php @@ -309,7 +309,7 @@ trait RelationShip public function relationCount(&$result, $relations, $aggregate = 'sum', $field = '*') { foreach ($relations as $key => $relation) { - $closure = null; + $closure = $name = null; if ($relation instanceof \Closure) { $closure = $relation; @@ -320,9 +320,10 @@ trait RelationShip } $relation = Loader::parseName($relation, 1, false); - $count = $this->$relation()->relationCount($result, $closure, $aggregate, $field); - if (!isset($name)) { + $count = $this->$relation()->relationCount($result, $closure, $aggregate, $field, $name); + + if (empty($name)) { $name = Loader::parseName($relation) . '_' . $aggregate; } diff --git a/thinkphp/library/think/model/relation/BelongsTo.php b/thinkphp/library/think/model/relation/BelongsTo.php index a1d2b23c..a90c5418 100644 --- a/thinkphp/library/think/model/relation/BelongsTo.php +++ b/thinkphp/library/think/model/relation/BelongsTo.php @@ -74,12 +74,17 @@ class BelongsTo extends OneToOne * @param \Closure $closure 闭包 * @param string $aggregate 聚合查询方法 * @param string $field 字段 + * @param string $aggregateAlias 聚合字段别名 * @return string */ - public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*') + public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*', &$aggregateAlias = '') { if ($closure) { - $closure($this->query); + $return = $closure($this->query); + + if ($return && is_string($return)) { + $aggregateAlias = $return; + } } return $this->query @@ -88,6 +93,37 @@ class BelongsTo extends OneToOne ->$aggregate($field); } + /** + * 关联统计 + * @access public + * @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 = '') + { + $foreignKey = $this->foreignKey; + + if (!isset($result->$foreignKey)) { + return 0; + } + + if ($closure) { + $return = $closure($this->query); + + if ($return && is_string($return)) { + $name = $return; + } + } + + return $this->query + ->where($this->localKey, '=', $result->$foreignKey) + ->$aggregate($field); + } + /** * 根据关联条件查询当前模型 * @access public @@ -228,10 +264,7 @@ class BelongsTo extends OneToOne */ public function associate($model) { - $foreignKey = $this->foreignKey; - $pk = $model->getPk(); - - $this->parent->setAttr($foreignKey, $model->$pk); + $this->parent->setAttr($this->foreignKey, $model->getKey()); $this->parent->save(); return $this->parent->setRelation($this->relation, $model); @@ -244,9 +277,7 @@ class BelongsTo extends OneToOne */ public function dissociate() { - $foreignKey = $this->foreignKey; - - $this->parent->setAttr($foreignKey, null); + $this->parent->setAttr($this->foreignKey, null); $this->parent->save(); return $this->parent->setRelation($this->relation, null); diff --git a/thinkphp/library/think/model/relation/BelongsToMany.php b/thinkphp/library/think/model/relation/BelongsToMany.php index d90430a8..a66b0d01 100644 --- a/thinkphp/library/think/model/relation/BelongsToMany.php +++ b/thinkphp/library/think/model/relation/BelongsToMany.php @@ -25,6 +25,8 @@ class BelongsToMany extends Relation protected $middle; // 中间表模型名称 protected $pivotName; + // 中间表数据名称 + protected $pivotDataName = 'pivot'; // 中间表模型对象 protected $pivot; @@ -67,6 +69,18 @@ class BelongsToMany extends Relation return $this; } + /** + * 设置中间表数据名称 + * @access public + * @param string $name + * @return $this + */ + public function pivotDataName($name) + { + $this->pivotDataName = $name; + return $this; + } + /** * 获取中间表更新条件 * @param $data @@ -121,7 +135,7 @@ class BelongsToMany extends Relation } } - $model->setRelation('pivot', $this->newPivot($pivot, true)); + $model->setRelation($this->pivotDataName, $this->newPivot($pivot, true)); } } @@ -350,21 +364,30 @@ class BelongsToMany extends Relation * @param \Closure $closure 闭包 * @param string $aggregate 聚合查询方法 * @param string $field 字段 + * @param string $name 统计字段别名 * @return integer */ - public function relationCount($result, $closure, $aggregate = 'count', $field = '*') + public function relationCount($result, $closure, $aggregate = 'count', $field = '*', &$name = '') { - $pk = $result->getPk(); - $count = 0; + $pk = $result->getPk(); - if (isset($result->$pk)) { - $pk = $result->$pk; - $count = $this->belongsToManyQuery($this->foreignKey, $this->localKey, [ - ['pivot.' . $this->localKey, '=', $pk], - ])->$aggregate($field); + if (!isset($result->$pk)) { + return 0; } - return $count; + $pk = $result->$pk; + + if ($closure) { + $return = $closure($this->query); + + if ($return && is_string($return)) { + $name = $return; + } + } + + return $this->belongsToManyQuery($this->foreignKey, $this->localKey, [ + ['pivot.' . $this->localKey, '=', $pk], + ])->$aggregate($field); } /** @@ -373,10 +396,19 @@ class BelongsToMany extends Relation * @param \Closure $closure 闭包 * @param string $aggregate 聚合查询方法 * @param string $field 字段 - * @return string + * @param string $aggregateAlias 聚合字段别名 + * @return array */ - public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*') + public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*', &$aggregateAlias = '') { + if ($closure) { + $return = $closure($this->query); + + if ($return && is_string($return)) { + $aggregateAlias = $return; + } + } + return $this->belongsToManyQuery($this->foreignKey, $this->localKey, [ [ 'pivot.' . $this->localKey, 'exp', $this->query->raw('=' . $this->parent->getTable() . '.' . $this->parent->getPk()), @@ -396,7 +428,11 @@ class BelongsToMany extends Relation protected function eagerlyManyToMany($where, $relation, $subRelation = '', $closure = null) { // 预载入关联查询 支持嵌套预载入 - $list = $this->belongsToManyQuery($this->foreignKey, $this->localKey, $where, $closure) + if ($closure) { + $closure($this->query); + } + + $list = $this->belongsToManyQuery($this->foreignKey, $this->localKey, $where) ->with($subRelation) ->select(); @@ -414,7 +450,7 @@ class BelongsToMany extends Relation } } - $set->setRelation('pivot', $this->newPivot($pivot, true)); + $set->setRelation($this->pivotDataName, $this->newPivot($pivot, true)); $data[$pivot[$this->localKey]][] = $set; } @@ -428,15 +464,10 @@ class BelongsToMany extends Relation * @param string $foreignKey 关联模型关联键 * @param string $localKey 当前模型关联键 * @param array $condition 关联查询条件 - * @param \Closure $closure 闭包 * @return Query */ - protected function belongsToManyQuery($foreignKey, $localKey, $condition = [], $closure = null) + protected function belongsToManyQuery($foreignKey, $localKey, $condition = []) { - if ($closure) { - $closure($this->query); - } - // 关联查询封装 $tableName = $this->query->getTable(); $table = $this->pivot->getTable(); @@ -543,6 +574,29 @@ class BelongsToMany extends Relation } } + /** + * 判断是否存在关联数据 + * @access public + * @param mixed $data 数据 可以使用关联模型对象 或者 关联对象的主键 + * @return Pivot + * @throws Exception + */ + public function attached($data) + { + if ($data instanceof Model) { + $id = $data->getKey(); + } else { + $id = $data; + } + + $pivot = $this->pivot + ->where($this->localKey, $this->parent->getKey()) + ->where($this->foreignKey, $id) + ->find(); + + return $pivot ?: false; + } + /** * 解除关联的一个中间表数据 * @access public diff --git a/thinkphp/library/think/model/relation/HasMany.php b/thinkphp/library/think/model/relation/HasMany.php index 2038a960..874f97b7 100644 --- a/thinkphp/library/think/model/relation/HasMany.php +++ b/thinkphp/library/think/model/relation/HasMany.php @@ -152,22 +152,27 @@ class HasMany extends Relation * @param \Closure $closure 闭包 * @param string $aggregate 聚合查询方法 * @param string $field 字段 + * @param string $name 统计字段别名 * @return integer */ - public function relationCount($result, $closure, $aggregate = 'count', $field = '*') + public function relationCount($result, $closure, $aggregate = 'count', $field = '*', &$name = '') { $localKey = $this->localKey; - $count = 0; - if (isset($result->$localKey)) { - if ($closure) { - $closure($this->query); - } - - $count = $this->query->where($this->foreignKey, '=', $result->$localKey)->$aggregate($field); + if (!isset($result->$localKey)) { + return 0; } - return $count; + if ($closure) { + $return = $closure($this->query); + if ($return && is_string($return)) { + $name = $return; + } + } + + return $this->query + ->where($this->foreignKey, '=', $result->$localKey) + ->$aggregate($field); } /** @@ -176,12 +181,17 @@ class HasMany extends Relation * @param \Closure $closure 闭包 * @param string $aggregate 聚合查询方法 * @param string $field 字段 + * @param string $aggregateAlias 聚合字段别名 * @return string */ - public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*') + public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*', &$aggregateAlias = '') { if ($closure) { - $closure($this->query); + $return = $closure($this->query); + + if ($return && is_string($return)) { + $aggregateAlias = $return; + } } return $this->query diff --git a/thinkphp/library/think/model/relation/HasManyThrough.php b/thinkphp/library/think/model/relation/HasManyThrough.php index 411f3fcc..7c7acaa0 100644 --- a/thinkphp/library/think/model/relation/HasManyThrough.php +++ b/thinkphp/library/think/model/relation/HasManyThrough.php @@ -120,9 +120,10 @@ class HasManyThrough extends Relation * @param \Closure $closure 闭包 * @param string $aggregate 聚合查询方法 * @param string $field 字段 + * @param string $name 统计字段别名 * @return integer */ - public function relationCount($result, $closure, $aggregate = 'count', $field = '*') + public function relationCount($result, $closure, $aggregate = 'count', $field = '*', &$name = '') {} /** diff --git a/thinkphp/library/think/model/relation/HasOne.php b/thinkphp/library/think/model/relation/HasOne.php index 1ccb6adc..e6620fff 100644 --- a/thinkphp/library/think/model/relation/HasOne.php +++ b/thinkphp/library/think/model/relation/HasOne.php @@ -74,12 +74,17 @@ class HasOne extends OneToOne * @param \Closure $closure 闭包 * @param string $aggregate 聚合查询方法 * @param string $field 字段 + * @param string $aggregateAlias 聚合字段别名 * @return string */ - public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*') + public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*', &$aggregateAlias = '') { if ($closure) { - $closure($this->query); + $return = $closure($this->query); + + if ($return && is_string($return)) { + $aggregateAlias = $return; + } } return $this->query @@ -88,6 +93,36 @@ class HasOne extends OneToOne ->$aggregate($field); } + /** + * 关联统计 + * @access public + * @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 = '') + { + $localKey = $this->localKey; + + if (!isset($result->$localKey)) { + return 0; + } + + if ($closure) { + $return = $closure($this->query); + if ($return && is_string($return)) { + $name = $return; + } + } + + return $this->query + ->where($this->foreignKey, '=', $result->$localKey) + ->$aggregate($field); + } + /** * 根据关联条件查询当前模型 * @access public diff --git a/thinkphp/library/think/model/relation/MorphMany.php b/thinkphp/library/think/model/relation/MorphMany.php index 7c083dd7..e1ca0912 100644 --- a/thinkphp/library/think/model/relation/MorphMany.php +++ b/thinkphp/library/think/model/relation/MorphMany.php @@ -186,27 +186,31 @@ class MorphMany extends Relation * @param \Closure $closure 闭包 * @param string $aggregate 聚合查询方法 * @param string $field 字段 + * @param string $name 统计字段别名 * @return integer */ - public function relationCount($result, $closure, $aggregate = 'count', $field = '*') + public function relationCount($result, $closure, $aggregate = 'count', $field = '*', &$name = '') { - $pk = $result->getPk(); - $count = 0; + $pk = $result->getPk(); - if (isset($result->$pk)) { - if ($closure) { - $closure($this->query); - } - - $count = $this->query - ->where([ - [$this->morphKey, '=', $result->$pk], - [$this->morphType, '=', $this->type], - ]) - ->$aggregate($field); + if (!isset($result->$pk)) { + return 0; } - return $count; + if ($closure) { + $return = $closure($this->query); + + if ($return && is_string($return)) { + $name = $return; + } + } + + return $this->query + ->where([ + [$this->morphKey, '=', $result->$pk], + [$this->morphType, '=', $this->type], + ]) + ->$aggregate($field); } /** @@ -215,12 +219,17 @@ class MorphMany extends Relation * @param \Closure $closure 闭包 * @param string $aggregate 聚合查询方法 * @param string $field 字段 + * @param string $aggregateAlias 聚合字段别名 * @return string */ - public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*') + public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*', &$aggregateAlias = '') { if ($closure) { - $closure($this->query); + $return = $closure($this->query); + + if ($return && is_string($return)) { + $aggregateAlias = $return; + } } return $this->query diff --git a/thinkphp/library/think/model/relation/MorphTo.php b/thinkphp/library/think/model/relation/MorphTo.php index bb7c4d0b..6da6a025 100644 --- a/thinkphp/library/think/model/relation/MorphTo.php +++ b/thinkphp/library/think/model/relation/MorphTo.php @@ -238,9 +238,10 @@ class MorphTo extends Relation * @param \Closure $closure 闭包 * @param string $aggregate 聚合查询方法 * @param string $field 字段 + * @param string $name 统计字段别名 * @return integer */ - public function relationCount($result, $closure, $aggregate = 'count', $field = '*') + public function relationCount($result, $closure, $aggregate = 'count', $field = '*', &$name = '') {} /** diff --git a/thinkphp/library/think/model/relation/OneToOne.php b/thinkphp/library/think/model/relation/OneToOne.php index dbffcb77..59fce77f 100644 --- a/thinkphp/library/think/model/relation/OneToOne.php +++ b/thinkphp/library/think/model/relation/OneToOne.php @@ -236,20 +236,6 @@ abstract class OneToOne extends Relation return $this->bindAttr; } - /** - * 关联统计 - * @access public - * @param Model $result 数据对象 - * @param \Closure $closure 闭包 - * @param string $aggregate 聚合查询方法 - * @param string $field 字段 - * @return integer - */ - public function relationCount($result, $closure, $aggregate = 'count', $field = '*') - { - throw new Exception('relation not support: ' . $aggregate); - } - /** * 一对一 关联模型预查询拼装 * @access public diff --git a/thinkphp/library/think/route/dispatch/Module.php b/thinkphp/library/think/route/dispatch/Module.php index bfffec0f..396ce11d 100644 --- a/thinkphp/library/think/route/dispatch/Module.php +++ b/thinkphp/library/think/route/dispatch/Module.php @@ -12,6 +12,7 @@ namespace think\route\dispatch; use ReflectionMethod; +use think\Controller; use think\exception\ClassNotFoundException; use think\exception\HttpException; use think\Loader; @@ -91,6 +92,10 @@ class Module extends Dispatch $this->rule->getConfig('url_controller_layer'), $this->rule->getConfig('controller_suffix'), $this->rule->getConfig('empty_controller')); + + if ($instance instanceof Controller) { + $instance->registerMiddleware(); + } } catch (ClassNotFoundException $e) { throw new HttpException(404, 'controller not exists:' . $e->getClass()); } diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json index 1c0d7ea8..2703603e 100644 --- a/vendor/composer/installed.json +++ b/vendor/composer/installed.json @@ -299,17 +299,17 @@ }, { "name": "topthink/framework", - "version": "v5.1.27", - "version_normalized": "5.1.27.0", + "version": "v5.1.28", + "version_normalized": "5.1.28.0", "source": { "type": "git", "url": "https://github.com/top-think/framework.git", - "reference": "f607b8fd51cc25e4da7e2f4ef0d5e64f390d36bd" + "reference": "b5077520855a1f406205687c73cdeaee1ed618c2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/top-think/framework/zipball/f607b8fd51cc25e4da7e2f4ef0d5e64f390d36bd", - "reference": "f607b8fd51cc25e4da7e2f4ef0d5e64f390d36bd", + "url": "https://api.github.com/repos/top-think/framework/zipball/b5077520855a1f406205687c73cdeaee1ed618c2", + "reference": "b5077520855a1f406205687c73cdeaee1ed618c2", "shasum": "", "mirrors": [ { @@ -331,7 +331,7 @@ "sebastian/phpcpd": "2.*", "squizlabs/php_codesniffer": "2.*" }, - "time": "2018-10-22T02:10:08+00:00", + "time": "2018-10-29T02:52:45+00:00", "type": "think-framework", "installation-source": "dist", "notification-url": "https://packagist.org/downloads/",