From 4cb2428df483949c1ca5e921f1b4eea6e67b9368 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 11 Jan 2017 16:01:48 +0800 Subject: [PATCH 001/125] =?UTF-8?q?fetchPdo=E6=96=B9=E6=B3=95=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 263565f9..83fcb1a0 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1400,7 +1400,7 @@ class Query */ public function fetchPdo($pdo = true) { - $this->options['fetch_class'] = $pdo; + $this->options['fetch_pdo'] = $pdo; return $this; } @@ -2071,7 +2071,7 @@ class Query if ($resultSet = $this->trigger('before_select', $options)) { } else { // 执行查询操作 - $resultSet = $this->query($sql, $bind, $options['master'], $options['fetch_class']); + $resultSet = $this->query($sql, $bind, $options['master'], $options['fetch_pdo']); if ($resultSet instanceof \PDOStatement) { // 返回PDOStatement对象 @@ -2174,7 +2174,7 @@ class Query if ($result = $this->trigger('before_find', $options)) { } else { // 执行查询 - $result = $this->query($sql, $bind, $options['master'], $options['fetch_class']); + $result = $this->query($sql, $bind, $options['master'], $options['fetch_pdo']); if ($result instanceof \PDOStatement) { // 返回PDOStatement对象 @@ -2454,7 +2454,7 @@ class Query $options['strict'] = $this->getConfig('fields_strict'); } - foreach (['master', 'lock', 'fetch_class', 'fetch_sql', 'distinct'] as $name) { + foreach (['master', 'lock', 'fetch_pdo', 'fetch_sql', 'distinct'] as $name) { if (!isset($options[$name])) { $options[$name] = false; } From 7ab6b6e04bf3c4e8d9210c871bf1ea216b618cca Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 11 Jan 2017 18:17:32 +0800 Subject: [PATCH 002/125] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BF=AB=E6=8D=B7?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 151 +++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 83fcb1a0..439d3299 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -972,6 +972,156 @@ class Query return $this; } + /** + * 指定Null查询条件 + * @access public + * @param mixed $field 查询字段 + * @param string $logic 查询逻辑 and or xor + * @return $this + */ + public function whereNull($field, $logic = 'AND') + { + $this->parseWhereExp($logic, $field, 'null', null); + return $this; + } + + /** + * 指定NotNull查询条件 + * @access public + * @param mixed $field 查询字段 + * @param string $logic 查询逻辑 and or xor + * @return $this + */ + public function whereNotNull($field, $logic = 'AND') + { + $this->parseWhereExp($logic, $field, 'notnull', null); + return $this; + } + + /** + * 指定Exists查询条件 + * @access public + * @param mixed $condition 查询条件 + * @param string $logic 查询逻辑 and or xor + * @return $this + */ + public function whereExists($condition, $logic = 'AND') + { + $this->options['where'][strtoupper($logic)][] = ['exists', $condition]; + return $this; + } + + /** + * 指定NotExists查询条件 + * @access public + * @param mixed $condition 查询条件 + * @param string $logic 查询逻辑 and or xor + * @return $this + */ + public function whereNotExists($condition, $logic = 'AND') + { + $this->options['where'][strtoupper($logic)][] = ['not exists', $condition]; + return $this; + } + + /** + * 指定In查询条件 + * @access public + * @param mixed $field 查询字段 + * @param mixed $condition 查询条件 + * @param string $logic 查询逻辑 and or xor + * @return $this + */ + public function whereIn($field, $condition, $logic = 'AND') + { + $this->parseWhereExp($logic, $field, 'in', $condition); + return $this; + } + + /** + * 指定NotIn查询条件 + * @access public + * @param mixed $field 查询字段 + * @param mixed $condition 查询条件 + * @param string $logic 查询逻辑 and or xor + * @return $this + */ + public function whereNotIn($field, $condition, $logic = 'AND') + { + $this->parseWhereExp($logic, $field, 'not in', $condition); + return $this; + } + + /** + * 指定Like查询条件 + * @access public + * @param mixed $field 查询字段 + * @param mixed $condition 查询条件 + * @param string $logic 查询逻辑 and or xor + * @return $this + */ + public function whereLike($field, $condition, $logic = 'AND') + { + $this->parseWhereExp($logic, $field, 'like', $condition); + return $this; + } + + /** + * 指定NotLike查询条件 + * @access public + * @param mixed $field 查询字段 + * @param mixed $condition 查询条件 + * @param string $logic 查询逻辑 and or xor + * @return $this + */ + public function whereNotLike($field, $condition, $logic = 'AND') + { + $this->parseWhereExp($logic, $field, 'not like', $condition); + return $this; + } + + /** + * 指定Between查询条件 + * @access public + * @param mixed $field 查询字段 + * @param mixed $condition 查询条件 + * @param string $logic 查询逻辑 and or xor + * @return $this + */ + public function whereBetween($field, $condition, $logic = 'AND') + { + $this->parseWhereExp($logic, $field, 'between', $condition); + return $this; + } + + /** + * 指定NotBetween查询条件 + * @access public + * @param mixed $field 查询字段 + * @param mixed $condition 查询条件 + * @param string $logic 查询逻辑 and or xor + * @return $this + */ + public function whereNotBetween($field, $condition, $logic = 'AND') + { + $this->parseWhereExp($logic, $field, 'not between', $condition); + return $this; + } + + /** + * 指定Exp查询条件 + * @access public + * @param mixed $field 查询字段 + * @param mixed $condition 查询条件 + * @param string $logic 查询逻辑 and or xor + * @return $this + */ + public function whereExp($field, $condition, $logic = 'AND') + { + $this->parseWhereExp($logic, $field, 'exp', $condition); + return $this; + } + /** * 分析查询表达式 * @access public @@ -984,6 +1134,7 @@ class Query */ protected function parseWhereExp($logic, $field, $op, $condition, $param = []) { + $logic = strtoupper($logic); if ($field instanceof \Closure) { $this->options['where'][$logic][] = is_string($op) ? [$op, $field] : $field; return; From 48064414c4ad51ddc2efe25f14a64813e4641992 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 11 Jan 2017 22:35:02 +0800 Subject: [PATCH 003/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BQuery=E7=B1=BBpaginat?= =?UTF-8?q?e=E6=96=B9=E6=B3=95=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E5=8F=AF=E4=BB=A5=E4=BD=BF=E7=94=A8=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 263565f9..e5c9df7f 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1115,9 +1115,9 @@ class Query /** * 分页查询 - * @param int|null $listRows 每页数量 - * @param int|bool $simple 简洁模式或者总记录数 - * @param array $config 配置参数 + * @param int|array $listRows 每页数量 数组表示配置参数 + * @param int|bool $simple 是否简洁模式或者总记录数 + * @param array $config 配置参数 * page:当前页, * path:url路径, * query:url额外参数, @@ -1134,8 +1134,13 @@ class Query $total = $simple; $simple = false; } - $config = array_merge(Config::get('paginate'), $config); - $listRows = $listRows ?: $config['list_rows']; + if (is_array($listRows)) { + $config = array_merge(Config::get('paginate'), $listRows); + $listRows = $config['list_rows']; + } else { + $config = array_merge(Config::get('paginate'), $config); + $listRows = $listRows ?: $config['list_rows']; + } /** @var Paginator $class */ $class = false !== strpos($config['type'], '\\') ? $config['type'] : '\\think\\paginator\\driver\\' . ucwords($config['type']); From 5b5e7373e7347b680f5aa446eb8583737f5664db Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 12 Jan 2017 10:45:21 +0800 Subject: [PATCH 004/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BQuery=E7=B1=BB?= =?UTF-8?q?=E6=9D=A1=E4=BB=B6=E6=9F=A5=E8=AF=A2=E4=B8=80=E5=A4=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 88cbf6e2..e15a48fd 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1175,18 +1175,18 @@ class Query $this->bind($param[2]); } // 记录一个字段多次查询条件 - $this->options['multi'][$field][] = $where[$field]; + $this->options['multi'][$logic][$field][] = $where[$field]; } if (!empty($where)) { if (!isset($this->options['where'][$logic])) { $this->options['where'][$logic] = []; } - if (is_string($field) && $this->checkMultiField($field)) { - $where[$field] = $this->options['multi'][$field]; + if (is_string($field) && $this->checkMultiField($field, $logic)) { + $where[$field] = $this->options['multi'][$logic][$field]; } elseif (is_array($field)) { foreach ($field as $key => $val) { - if ($this->checkMultiField($key)) { - $where[$key] = $this->options['multi'][$key]; + if ($this->checkMultiField($key, $logic)) { + $where[$key] = $this->options['multi'][$logic][$key]; } } } @@ -1194,10 +1194,16 @@ class Query } } - // 检查是否存在一个字段多次查询条件 - private function checkMultiField($field) + /** + * 检查是否存在一个字段多次查询条件 + * @access public + * @param string $field 查询字段 + * @param string $logic 查询逻辑 and or xor + * @return bool + */ + private function checkMultiField($field, $logic) { - return isset($this->options['multi'][$field]) && count($this->options['multi'][$field]) > 1; + return isset($this->options['multi'][$logic][$field]) && count($this->options['multi'][$logic][$field]) > 1; } /** From 1ab44ff8e7b1df128ef4ee09fb590ea327f5d0ee Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 12 Jan 2017 11:45:55 +0800 Subject: [PATCH 005/125] =?UTF-8?q?=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index e15a48fd..5ed6fb17 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1154,7 +1154,7 @@ class Query // 数组批量查询 $where = $field; foreach ($where as $k => $val) { - $this->options['multi'][$k][] = $val; + $this->options['multi'][$logic][$k][] = $val; } } elseif ($field && is_string($field)) { // 字符串查询 From b47fe5553b71c54fdbfce067e293275ea7cacbf1 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 12 Jan 2017 12:17:53 +0800 Subject: [PATCH 006/125] =?UTF-8?q?=E4=BF=AE=E6=AD=A3null=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E7=9A=84=E6=9D=A1=E4=BB=B6=E5=90=88=E5=B9=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 5ed6fb17..9c796081 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1158,13 +1158,15 @@ class Query } } elseif ($field && is_string($field)) { // 字符串查询 - $where[$field] = ['null', '']; + $where[$field] = ['null', '']; + $this->options['multi'][$logic][$field][] = $where[$field]; } } elseif (is_array($op)) { $where[$field] = $param; } elseif (in_array(strtolower($op), ['null', 'notnull', 'not null'])) { // null查询 - $where[$field] = [$op, '']; + $where[$field] = [$op, '']; + $this->options['multi'][$logic][$field][] = $where[$field]; } elseif (is_null($condition)) { // 字段相等查询 $where[$field] = ['eq', $op]; From b54442a42fbfa78590173f7d638f61357316ec87 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 12 Jan 2017 12:43:58 +0800 Subject: [PATCH 007/125] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E5=8D=95=E4=B8=AA?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E5=A4=9A=E6=AC=A1Or=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E6=83=85=E5=86=B5=E7=9A=84=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 9c796081..1ef97571 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1169,7 +1169,8 @@ class Query $this->options['multi'][$logic][$field][] = $where[$field]; } elseif (is_null($condition)) { // 字段相等查询 - $where[$field] = ['eq', $op]; + $where[$field] = ['eq', $op]; + $this->options['multi'][$logic][$field][] = $where[$field]; } else { $where[$field] = [$op, $condition, isset($param[2]) ? $param[2] : null]; if ('exp' == strtolower($op) && isset($param[2]) && is_array($param[2])) { From 28747aa5dcc59d7957fb61476fbb023be0e96352 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 12 Jan 2017 13:29:46 +0800 Subject: [PATCH 008/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BValidate=E7=B1=BB?= =?UTF-8?q?=E7=9A=84=E9=94=99=E8=AF=AF=E4=BF=A1=E6=81=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Validate.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/think/Validate.php b/library/think/Validate.php index e3bc9db6..e91ee338 100644 --- a/library/think/Validate.php +++ b/library/think/Validate.php @@ -1222,9 +1222,9 @@ class Validate $msg = Lang::get(substr($msg, 2, -1)); } - if (is_string($msg) && is_string($rule) && false !== strpos($msg, ':')) { + if (is_string($msg) && is_scalar($rule) && false !== strpos($msg, ':')) { // 变量替换 - if (strpos($rule, ',')) { + if (is_string($rule) && strpos($rule, ',')) { $array = array_pad(explode(',', $rule), 3, ''); } else { $array = array_pad([], 3, ''); From f2a82bcf82dfaaec76eff25db1525c41a48650dd Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 12 Jan 2017 13:53:09 +0800 Subject: [PATCH 009/125] =?UTF-8?q?=E6=94=B9=E8=BF=9Btoday=E7=9A=84?= =?UTF-8?q?=E6=97=A5=E6=9C=9F=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 1ef97571..3ba42b2f 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1644,7 +1644,7 @@ class Query switch (strtolower($op)) { case 'today': case 'd': - $range = 'today'; + $range = ['today', 'tomorrow']; break; case 'week': case 'w': From bd007e81e9ca9a97694a0135b6617a13982c7477 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 12 Jan 2017 14:47:20 +0800 Subject: [PATCH 010/125] =?UTF-8?q?Query=E7=B1=BB=E5=A2=9E=E5=8A=A0getPdo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 3ba42b2f..e2c38d2f 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -409,7 +409,7 @@ class Query if (isset($this->options['field'])) { unset($this->options['field']); } - $pdo = $this->field($field)->fetchPdo(true)->find(); + $pdo = $this->field($field)->limit(1)->getPdo(); if (is_string($pdo)) { // 返回SQL语句 return $pdo; @@ -459,7 +459,7 @@ class Query if ($key && '*' != $field) { $field = $key . ',' . $field; } - $pdo = $this->field($field)->fetchPdo(true)->select(); + $pdo = $this->field($field)->getPdo(); if (is_string($pdo)) { // 返回SQL语句 return $pdo; @@ -2188,6 +2188,27 @@ class Query } } + /** + * 执行查询但只返回PDOStatement对象 + * @access public + * @return \PDOStatement|string + */ + public function getPdo() + { + // 分析查询表达式 + $options = $this->parseExpress(); + // 生成查询SQL + $sql = $this->builder->select($options); + // 获取参数绑定 + $bind = $this->getBind(); + if ($options['fetch_sql']) { + // 获取实际执行的SQL语句 + return $this->connection->getRealSql($sql, $bind); + } + // 执行查询操作 + return $this->query($sql, $bind, $options['master'], true); + } + /** * 查找记录 * @access public From 508ff846c33338e14e540db4093f96b9499ceba8 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 12 Jan 2017 16:03:19 +0800 Subject: [PATCH 011/125] =?UTF-8?q?=E5=8F=96=E6=B6=88min=20max=20sum=20avg?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E7=9A=84=E5=8F=82=E6=95=B0=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index e2c38d2f..47d53670 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -522,7 +522,7 @@ class Query * @param string $field 字段名 * @return float|int */ - public function sum($field = '*') + public function sum($field) { return $this->value('SUM(' . $field . ') AS tp_sum', 0, true); } @@ -533,7 +533,7 @@ class Query * @param string $field 字段名 * @return mixed */ - public function min($field = '*') + public function min($field) { return $this->value('MIN(' . $field . ') AS tp_min', 0, true); } @@ -544,7 +544,7 @@ class Query * @param string $field 字段名 * @return mixed */ - public function max($field = '*') + public function max($field) { return $this->value('MAX(' . $field . ') AS tp_max', 0, true); } @@ -555,7 +555,7 @@ class Query * @param string $field 字段名 * @return float|int */ - public function avg($field = '*') + public function avg($field) { return $this->value('AVG(' . $field . ') AS tp_avg', 0, true); } From f37a5e8f55687ac9782f37f3b398f4f7e0cf0f86 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 12 Jan 2017 17:07:40 +0800 Subject: [PATCH 012/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BQuery=E7=B1=BB?= =?UTF-8?q?=E7=9A=84whereTime=E6=96=B9=E6=B3=95=E6=94=AF=E6=8C=81=E6=9B=B4?= =?UTF-8?q?=E5=A4=9A=E7=9A=84=E6=97=B6=E9=97=B4=E6=97=A5=E6=9C=9F=E8=A1=A8?= =?UTF-8?q?=E8=BE=BE=E5=BC=8F=EF=BC=88=E9=BB=98=E8=AE=A4=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E6=9D=A1=E4=BB=B6=E4=B8=BA=E5=A4=A7=E4=BA=8E=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E8=A1=A8=E8=BE=BE=E5=BC=8F=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 47d53670..24f5ce2a 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1670,6 +1670,8 @@ class Query case 'last year': $range = [mktime(0, 0, 0, 1, 1, $date['year'] - 1), mktime(0, 0, 0, 1, 1, $date['year'])]; break; + default: + $range = $op; } $op = is_array($range) ? 'between' : '>'; } From 1e8e169b0a4229cb5a49aa6f162d3430dd1d1e9e Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 13 Jan 2017 17:53:11 +0800 Subject: [PATCH 013/125] =?UTF-8?q?=E6=94=B9=E8=BF=9Bwhere=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E7=9A=84=E7=9B=B8=E5=90=8C=E5=AD=97=E6=AE=B5=E5=A4=9A?= =?UTF-8?q?=E6=AC=A1=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 24f5ce2a..45c66ccf 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1169,8 +1169,10 @@ class Query $this->options['multi'][$logic][$field][] = $where[$field]; } elseif (is_null($condition)) { // 字段相等查询 - $where[$field] = ['eq', $op]; - $this->options['multi'][$logic][$field][] = $where[$field]; + $where[$field] = ['eq', $op]; + if ('AND' != $logic) { + $this->options['multi'][$logic][$field][] = $where[$field]; + } } else { $where[$field] = [$op, $condition, isset($param[2]) ? $param[2] : null]; if ('exp' == strtolower($op) && isset($param[2]) && is_array($param[2])) { From 84a832b3ec6d6edfe56e17e62a52fc548adcd5cd Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 14 Jan 2017 10:46:21 +0800 Subject: [PATCH 014/125] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=85=B3=E8=81=94?= =?UTF-8?q?=E5=BB=B6=E8=BF=9F=E9=A2=84=E8=BD=BD=E5=85=A5=EF=BC=88=E4=BB=85?= =?UTF-8?q?=E9=92=88=E5=AF=B9=E6=95=B0=E6=8D=AE=E9=9B=86=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Collection.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/library/think/Collection.php b/library/think/Collection.php index 41b42759..0e718616 100644 --- a/library/think/Collection.php +++ b/library/think/Collection.php @@ -16,6 +16,7 @@ use ArrayIterator; use Countable; use IteratorAggregate; use JsonSerializable; +use think\Model; class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable { @@ -352,6 +353,21 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria return json_encode($this->toArray(), $options); } + /** + * 延迟预载入关联查询 + * @access public + * @param mixed $relation 关联 + * @return $this + */ + public function load($relation) + { + $item = current($this->items); + if ($item instanceof Model) { + $item->eagerlyResultSet($this->items, $relation); + } + return $this; + } + public function __toString() { return $this->toJson(); From ba7232b678b2beea40950983a07c9600aa02b483 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 15 Jan 2017 18:50:51 +0800 Subject: [PATCH 015/125] =?UTF-8?q?=E5=A2=9E=E5=8A=A0laodRelation=E5=8A=A9?= =?UTF-8?q?=E6=89=8B=E5=87=BD=E6=95=B0=20=E7=94=A8=E4=BA=8E=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E9=9B=86=E7=9A=84=E5=BB=B6=E8=BF=9F=E9=A2=84=E8=BD=BD?= =?UTF-8?q?=E5=85=A5=E5=85=B3=E8=81=94=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/helper.php b/helper.php index 83dbddda..f26400c3 100644 --- a/helper.php +++ b/helper.php @@ -548,3 +548,20 @@ if (!function_exists('token')) { return ''; } } + +if (!function_exists('loadRelation')) { + /** + * 延迟预载入关联查询 + * @param mixed $resultSet 数据集 + * @param mixed $relation 关联 + * @return array + */ + function loadRelation($resultSet, $relation) + { + $item = current($resultSet); + if ($item instanceof Model) { + $item->eagerlyResultSet($resultSet, $relation); + } + return $resultSet; + } +} From d5d5314f2e2f1fc07f0e4ca2532fe55a55d769f7 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 16 Jan 2017 10:31:03 +0800 Subject: [PATCH 016/125] =?UTF-8?q?Query=E7=B1=BB=E7=9A=84relation?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=92=8Cwith=E6=96=B9=E6=B3=95=E4=B8=80?= =?UTF-8?q?=E6=A0=B7=E6=94=AF=E6=8C=81=E4=BD=BF=E7=94=A8=E9=97=AD=E5=8C=85?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E8=87=AA=E5=AE=9A=E4=B9=89=E5=85=B3=E8=81=94?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 11 +++++++++-- library/think/model/relation/BelongsTo.php | 6 +++++- library/think/model/relation/BelongsToMany.php | 6 +++++- library/think/model/relation/HasMany.php | 6 +++++- library/think/model/relation/HasManyThrough.php | 6 +++++- library/think/model/relation/HasOne.php | 7 +++++-- library/think/model/relation/MorphMany.php | 6 +++++- library/think/model/relation/MorphTo.php | 3 ++- 8 files changed, 41 insertions(+), 10 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 0491e551..14deb300 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1281,8 +1281,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $relations = explode(',', $relations); } - foreach ($relations as $relation) { - $this->data[$relation] = $this->$relation()->getRelation(); + foreach ($relations as $key => $relation) { + $closure = null; + if ($relation instanceof \Closure) { + // 支持闭包查询过滤关联条件 + $closure = $relation; + $relation = $key; + } + $method = Loader::parseName($relation, 1, false); + $this->data[$relation] = $this->$method()->getRelation($closure); } return $this; } diff --git a/library/think/model/relation/BelongsTo.php b/library/think/model/relation/BelongsTo.php index 8f26d033..a96d9028 100644 --- a/library/think/model/relation/BelongsTo.php +++ b/library/think/model/relation/BelongsTo.php @@ -38,12 +38,16 @@ class BelongsTo extends OneToOne /** * 延迟获取关联数据 + * @param \Closure $closure 闭包查询条件 * @access public */ - public function getRelation() + public function getRelation($closure = null) { $foreignKey = $this->foreignKey; $localKey = $this->localKey; + if ($closure) { + call_user_func_array($closure, [ & $this->query]); + } return $this->query->where($localKey, $this->parent->$foreignKey)->find(); } diff --git a/library/think/model/relation/BelongsToMany.php b/library/think/model/relation/BelongsToMany.php index 428620a2..25e1a078 100644 --- a/library/think/model/relation/BelongsToMany.php +++ b/library/think/model/relation/BelongsToMany.php @@ -46,13 +46,17 @@ class BelongsToMany extends Relation /** * 延迟获取关联数据 + * @param \Closure $closure 闭包查询条件 * @access public */ - public function getRelation() + public function getRelation($closure = null) { $foreignKey = $this->foreignKey; $localKey = $this->localKey; $middle = $this->middle; + if ($closure) { + call_user_func_array($closure, [ & $this->query]); + } // 关联查询 $pk = $this->parent->getPk(); $condition['pivot.' . $localKey] = $this->parent->$pk; diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index c9235bac..044cc88d 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -39,10 +39,14 @@ class HasMany extends Relation /** * 延迟获取关联数据 + * @param \Closure $closure 闭包查询条件 * @access public */ - public function getRelation() + public function getRelation($closure = null) { + if ($closure) { + call_user_func_array($closure, [ & $this->query]); + } return $this->select(); } diff --git a/library/think/model/relation/HasManyThrough.php b/library/think/model/relation/HasManyThrough.php index 2585f354..983daf22 100644 --- a/library/think/model/relation/HasManyThrough.php +++ b/library/think/model/relation/HasManyThrough.php @@ -49,10 +49,14 @@ class HasManyThrough extends Relation /** * 延迟获取关联数据 + * @param \Closure $closure 闭包查询条件 * @access public */ - public function getRelation() + public function getRelation($closure = null) { + if ($closure) { + call_user_func_array($closure, [ & $this->query]); + } return $this->select(); } diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index 41667e2a..05230922 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -38,13 +38,16 @@ class HasOne extends OneToOne /** * 延迟获取关联数据 + * @param \Closure $closure 闭包查询条件 * @access public */ - public function getRelation() + public function getRelation($colsure = null) { // 执行关联定义方法 $localKey = $this->localKey; - + if ($closure) { + call_user_func_array($closure, [ & $this->query]); + } // 判断关联类型执行查询 return $this->query->where($this->foreignKey, $this->parent->$localKey)->find(); } diff --git a/library/think/model/relation/MorphMany.php b/library/think/model/relation/MorphMany.php index abb32ebd..40c1f791 100644 --- a/library/think/model/relation/MorphMany.php +++ b/library/think/model/relation/MorphMany.php @@ -45,10 +45,14 @@ class MorphMany extends Relation /** * 延迟获取关联数据 + * @param \Closure $closure 闭包查询条件 * @access public */ - public function getRelation() + public function getRelation($closure = null) { + if ($closure) { + call_user_func_array($closure, [ & $this->query]); + } return $this->select(); } diff --git a/library/think/model/relation/MorphTo.php b/library/think/model/relation/MorphTo.php index e6676574..05017e8a 100644 --- a/library/think/model/relation/MorphTo.php +++ b/library/think/model/relation/MorphTo.php @@ -39,9 +39,10 @@ class MorphTo extends Relation /** * 延迟获取关联数据 + * @param \Closure $closure 闭包查询条件 * @access public */ - public function getRelation() + public function getRelation($closure = null) { $morphKey = $this->morphKey; $morphType = $this->morphType; From 00554847527af3fcf13915d24975279d5793b270 Mon Sep 17 00:00:00 2001 From: ThinkPHP Date: Mon, 16 Jan 2017 02:31:28 +0000 Subject: [PATCH 017/125] Apply fixes from StyleCI --- library/think/Collection.php | 1 - 1 file changed, 1 deletion(-) diff --git a/library/think/Collection.php b/library/think/Collection.php index 0e718616..7b57ee03 100644 --- a/library/think/Collection.php +++ b/library/think/Collection.php @@ -16,7 +16,6 @@ use ArrayIterator; use Countable; use IteratorAggregate; use JsonSerializable; -use think\Model; class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable { From f49ebcdc1dd87cf68a16120de7e2044d884734c4 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 16 Jan 2017 11:02:49 +0800 Subject: [PATCH 018/125] =?UTF-8?q?=E5=91=BD=E5=90=8D=E8=A7=84=E8=8C=83?= =?UTF-8?q?=E8=B0=83=E6=95=B4loadRelation=20=3D>=20load=5Frelation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/helper.php b/helper.php index f26400c3..e8af0a52 100644 --- a/helper.php +++ b/helper.php @@ -549,14 +549,14 @@ if (!function_exists('token')) { } } -if (!function_exists('loadRelation')) { +if (!function_exists('load_relation')) { /** * 延迟预载入关联查询 * @param mixed $resultSet 数据集 * @param mixed $relation 关联 * @return array */ - function loadRelation($resultSet, $relation) + function load_relation($resultSet, $relation) { $item = current($resultSet); if ($item instanceof Model) { From f923c9dba55c5c358eb9ee8139ed9bb765a378b6 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 16 Jan 2017 11:16:15 +0800 Subject: [PATCH 019/125] =?UTF-8?q?=E7=8B=AC=E7=AB=8B=E5=87=BAthink\model\?= =?UTF-8?q?Collection=E7=B1=BB=20=E7=94=A8=E4=BA=8E=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Collection.php | 15 -------------- library/think/Model.php | 1 + library/think/model/Collection.php | 32 ++++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 library/think/model/Collection.php diff --git a/library/think/Collection.php b/library/think/Collection.php index 7b57ee03..41b42759 100644 --- a/library/think/Collection.php +++ b/library/think/Collection.php @@ -352,21 +352,6 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria return json_encode($this->toArray(), $options); } - /** - * 延迟预载入关联查询 - * @access public - * @param mixed $relation 关联 - * @return $this - */ - public function load($relation) - { - $item = current($this->items); - if ($item instanceof Model) { - $item->eagerlyResultSet($this->items, $relation); - } - return $this; - } - public function __toString() { return $this->toJson(); diff --git a/library/think/Model.php b/library/think/Model.php index 14deb300..1e1d4460 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -14,6 +14,7 @@ namespace think; use InvalidArgumentException; use think\db\Query; use think\Exception\ValidateException; +use think\model\Collection; use think\model\Relation; use think\model\relation\BelongsTo; use think\model\relation\BelongsToMany; diff --git a/library/think/model/Collection.php b/library/think/model/Collection.php new file mode 100644 index 00000000..81ca17a9 --- /dev/null +++ b/library/think/model/Collection.php @@ -0,0 +1,32 @@ + +// +---------------------------------------------------------------------- + +namespace think\model; + +use think\Collection as BaseCollection; + +class Collection extends BaseCollection +{ + /** + * 延迟预载入关联查询 + * @access public + * @param mixed $relation 关联 + * @return $this + */ + public function load($relation) + { + $item = current($this->items); + if ($item instanceof Model) { + $item->eagerlyResultSet($this->items, $relation); + } + return $this; + } +} From 771c0a90be4f303e4295e36ff685c6c15827be5d Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 16 Jan 2017 11:34:53 +0800 Subject: [PATCH 020/125] =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/Collection.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/library/think/model/Collection.php b/library/think/model/Collection.php index 81ca17a9..e44ffca6 100644 --- a/library/think/model/Collection.php +++ b/library/think/model/Collection.php @@ -24,9 +24,7 @@ class Collection extends BaseCollection public function load($relation) { $item = current($this->items); - if ($item instanceof Model) { - $item->eagerlyResultSet($this->items, $relation); - } + $item->eagerlyResultSet($this->items, $relation); return $this; } } From b4290f0d01fffcef1fd67dee6e77485877732955 Mon Sep 17 00:00:00 2001 From: yunwuxin <448901948@qq.com> Date: Mon, 16 Jan 2017 14:41:39 +0800 Subject: [PATCH 021/125] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=88=86=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Collection.php | 2 +- library/think/Db.php | 13 ++- library/think/Paginator.php | 137 +++++++++++++++++++++--- library/think/db/Query.php | 142 ++++++++++++------------- library/think/paginator/Collection.php | 74 ------------- 5 files changed, 203 insertions(+), 165 deletions(-) delete mode 100644 library/think/paginator/Collection.php diff --git a/library/think/Collection.php b/library/think/Collection.php index 41b42759..c23395a9 100644 --- a/library/think/Collection.php +++ b/library/think/Collection.php @@ -225,7 +225,7 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria $result = []; foreach ($this->items as $row) { - $key = $value = null; + $key = $value = null; $keySet = $valueSet = false; if (null !== $index_key && array_key_exists($index_key, $row)) { $keySet = true; diff --git a/library/think/Db.php b/library/think/Db.php index a29dac3e..00f719e0 100644 --- a/library/think/Db.php +++ b/library/think/Db.php @@ -13,7 +13,6 @@ namespace think; use think\db\Connection; use think\db\Query; -use think\paginator\Collection as PaginatorCollection; /** * Class Db @@ -25,21 +24,21 @@ use think\paginator\Collection as PaginatorCollection; * @method Query union(mixed $union, boolean $all = false) static UNION查询 * @method Query limit(mixed $offset, integer $length = null) static 查询LIMIT * @method Query order(mixed $field, string $order = null) static 查询ORDER - * @method Query cache(mixed $key = true , integer $expire = null) static 设置查询缓存 + * @method Query cache(mixed $key = null , integer $expire = null) static 设置查询缓存 * @method mixed value(string $field) static 获取某个字段的值 * @method array column(string $field, string $key = '') static 获取某个列的值 * @method Query view(mixed $join, mixed $field = null, mixed $on = null, string $type = 'INNER') static 视图查询 - * @method mixed find(mixed $data = []) static 查询单个记录 - * @method mixed select(mixed $data = []) static 查询多个记录 + * @method mixed find(mixed $data = null) static 查询单个记录 + * @method mixed select(mixed $data = null) static 查询多个记录 * @method integer insert(array $data, boolean $replace = false, boolean $getLastInsID = false, string $sequence = null) static 插入一条记录 * @method integer insertGetId(array $data, boolean $replace = false, string $sequence = null) static 插入一条记录并返回自增ID * @method integer insertAll(array $dataSet) static 插入多条记录 * @method integer update(array $data) static 更新记录 - * @method integer delete(mixed $data = []) static 删除记录 + * @method integer delete(mixed $data = null) static 删除记录 * @method boolean chunk(integer $count, callable $callback, string $column = null) static 分块获取数据 - * @method mixed query(string $sql, array $bind = [], boolean $fetch = false, boolean $master = false, mixed $class = false) static SQL查询 + * @method mixed query(string $sql, array $bind = [], boolean $fetch = false, boolean $master = false, mixed $class = null) static SQL查询 * @method integer execute(string $sql, array $bind = [], boolean $fetch = false, boolean $getLastInsID = false, string $sequence = null) static SQL执行 - * @method PaginatorCollection paginate(integer $listRows = 15, mixed $simple = false, array $config = []) static 分页查询 + * @method Paginator paginate(integer $listRows = 15, mixed $simple = null, array $config = []) static 分页查询 * @method mixed transaction(callable $callback) static 执行数据库事务 * @method void startTrans() static 启动事务 * @method void commit() static 用于非自动提交状态下面的查询提交 diff --git a/library/think/Paginator.php b/library/think/Paginator.php index d2fe79d2..0c1bea8a 100644 --- a/library/think/Paginator.php +++ b/library/think/Paginator.php @@ -11,14 +11,19 @@ namespace think; -use think\paginator\Collection as PaginatorCollection; +use ArrayAccess; +use ArrayIterator; +use Countable; +use IteratorAggregate; +use JsonSerializable; +use Traversable; -abstract class Paginator +abstract class Paginator implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable { /** @var bool 是否为简洁模式 */ protected $simple = false; - /** @var PaginatorCollection 数据集 */ + /** @var Collection 数据集 */ protected $items; /** @var integer 当前页 */ @@ -44,7 +49,7 @@ abstract class Paginator 'fragment' => '', ]; - protected function __construct($items, $listRows, $currentPage = null, $total = null, $simple = false, $options = []) + public function __construct($items, $listRows, $currentPage = null, $total = null, $simple = false, $options = []) { $this->options = array_merge($this->options, $options); @@ -53,10 +58,11 @@ abstract class Paginator $this->simple = $simple; $this->listRows = $listRows; + if (!$items instanceof Collection) { + $items = Collection::make($items); + } + if ($simple) { - if (!$items instanceof Collection) { - $items = Collection::make($items); - } $this->currentPage = $this->setCurrentPage($currentPage); $this->hasMore = count($items) > ($this->listRows); $items = $items->slice(0, $this->listRows); @@ -66,8 +72,7 @@ abstract class Paginator $this->currentPage = $this->setCurrentPage($currentPage); $this->hasMore = $this->currentPage < $this->lastPage; } - - $this->items = PaginatorCollection::make($items, $this); + $this->items = $items; } /** @@ -77,12 +82,11 @@ abstract class Paginator * @param bool $simple * @param null $total * @param array $options - * @return PaginatorCollection + * @return Paginator */ public static function make($items, $listRows, $currentPage = null, $total = null, $simple = false, $options = []) { - $paginator = new static($items, $listRows, $currentPage, $total, $simple, $options); - return $paginator->items; + return new static($items, $listRows, $currentPage, $total, $simple, $options); } protected function setCurrentPage($currentPage) @@ -253,4 +257,113 @@ abstract class Paginator * @return mixed */ abstract public function render(); + + public function items() + { + return $this->items->all(); + } + + public function getCollection() + { + return $this->items; + } + + public function isEmpty() + { + return $this->items->isEmpty(); + } + + /** + * Retrieve an external iterator + * @return Traversable An instance of an object implementing Iterator or + * Traversable + */ + public function getIterator() + { + return new ArrayIterator($this->items->all()); + } + + /** + * Whether a offset exists + * @param mixed $offset + * @return bool + */ + public function offsetExists($offset) + { + return $this->items->offsetExists($offset); + } + + /** + * Offset to retrieve + * @param mixed $offset + * @return mixed + */ + public function offsetGet($offset) + { + return $this->items->offsetGet($offset); + } + + /** + * Offset to set + * @param mixed $offset + * @param mixed $value + */ + public function offsetSet($offset, $value) + { + $this->items->offsetSet($offset, $value); + } + + /** + * Offset to unset + * @param mixed $offset + * @return void + * @since 5.0.0 + */ + public function offsetUnset($offset) + { + $this->items->offsetUnset($offset); + } + + /** + * Count elements of an object + */ + public function count() + { + return $this->items->count(); + } + + public function __toString() + { + return (string) $this->render(); + } + + public function toArray() + { + try { + $total = $this->total(); + } catch (Exception $e) { + $total = null; + } + + return [ + 'total' => $total, + 'per_page' => $this->listRows(), + 'current_page' => $this->currentPage(), + 'data' => $this->items->toArray() + ]; + } + + /** + * Specify data which should be serialized to JSON + */ + public function jsonSerialize() + { + return $this->toArray(); + } + + public function __call($name, $arguments) + { + return call_user_func_array([$this->getCollection(), $name], $arguments); + } + } diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 45c66ccf..da5990a8 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -229,8 +229,8 @@ class Query /** * 执行语句 * @access public - * @param string $sql sql指令 - * @param array $bind 参数绑定 + * @param string $sql sql指令 + * @param array $bind 参数绑定 * @return int * @throws BindParamException * @throws PDOException @@ -243,7 +243,7 @@ class Query /** * 获取最近插入的ID * @access public - * @param string $sequence 自增序列名 + * @param string $sequence 自增序列名 * @return string */ public function getLastInsID($sequence = null) @@ -390,7 +390,7 @@ class Query * @access public * @param string $field 字段名 * @param mixed $default 默认值 - * @param bool $force 强制转为数字类型 + * @param bool $force 强制转为数字类型 * @return mixed */ public function value($field, $default = null, $force = false) @@ -694,7 +694,7 @@ class Query * 获取Join表名及别名 支持 * ['prefix_table或者子查询'=>'alias'] 'prefix_table alias' 'table alias' * @access public - * @param array|string $join + * @param array|string $join * @return array|string */ protected function getJoinTable($join, &$alias = null) @@ -817,8 +817,8 @@ class Query /** * 字段值增长 * @access public - * @param string|array $field 字段名 - * @param integer $step 增长值 + * @param string|array $field 字段名 + * @param integer $step 增长值 * @return $this */ public function inc($field, $step = 1) @@ -833,8 +833,8 @@ class Query /** * 字段值减少 * @access public - * @param string|array $field 字段名 - * @param integer $step 增长值 + * @param string|array $field 字段名 + * @param integer $step 增长值 * @return $this */ public function dec($field, $step = 1) @@ -849,8 +849,8 @@ class Query /** * 使用表达式设置数据 * @access public - * @param string $field 字段名 - * @param string $value 字段值 + * @param string $field 字段名 + * @param string $value 字段值 * @return $this */ public function exp($field, $value) @@ -975,8 +975,8 @@ class Query /** * 指定Null查询条件 * @access public - * @param mixed $field 查询字段 - * @param string $logic 查询逻辑 and or xor + * @param mixed $field 查询字段 + * @param string $logic 查询逻辑 and or xor * @return $this */ public function whereNull($field, $logic = 'AND') @@ -988,8 +988,8 @@ class Query /** * 指定NotNull查询条件 * @access public - * @param mixed $field 查询字段 - * @param string $logic 查询逻辑 and or xor + * @param mixed $field 查询字段 + * @param string $logic 查询逻辑 and or xor * @return $this */ public function whereNotNull($field, $logic = 'AND') @@ -1001,8 +1001,8 @@ class Query /** * 指定Exists查询条件 * @access public - * @param mixed $condition 查询条件 - * @param string $logic 查询逻辑 and or xor + * @param mixed $condition 查询条件 + * @param string $logic 查询逻辑 and or xor * @return $this */ public function whereExists($condition, $logic = 'AND') @@ -1014,8 +1014,8 @@ class Query /** * 指定NotExists查询条件 * @access public - * @param mixed $condition 查询条件 - * @param string $logic 查询逻辑 and or xor + * @param mixed $condition 查询条件 + * @param string $logic 查询逻辑 and or xor * @return $this */ public function whereNotExists($condition, $logic = 'AND') @@ -1027,9 +1027,9 @@ class Query /** * 指定In查询条件 * @access public - * @param mixed $field 查询字段 - * @param mixed $condition 查询条件 - * @param string $logic 查询逻辑 and or xor + * @param mixed $field 查询字段 + * @param mixed $condition 查询条件 + * @param string $logic 查询逻辑 and or xor * @return $this */ public function whereIn($field, $condition, $logic = 'AND') @@ -1041,9 +1041,9 @@ class Query /** * 指定NotIn查询条件 * @access public - * @param mixed $field 查询字段 - * @param mixed $condition 查询条件 - * @param string $logic 查询逻辑 and or xor + * @param mixed $field 查询字段 + * @param mixed $condition 查询条件 + * @param string $logic 查询逻辑 and or xor * @return $this */ public function whereNotIn($field, $condition, $logic = 'AND') @@ -1055,9 +1055,9 @@ class Query /** * 指定Like查询条件 * @access public - * @param mixed $field 查询字段 - * @param mixed $condition 查询条件 - * @param string $logic 查询逻辑 and or xor + * @param mixed $field 查询字段 + * @param mixed $condition 查询条件 + * @param string $logic 查询逻辑 and or xor * @return $this */ public function whereLike($field, $condition, $logic = 'AND') @@ -1069,9 +1069,9 @@ class Query /** * 指定NotLike查询条件 * @access public - * @param mixed $field 查询字段 - * @param mixed $condition 查询条件 - * @param string $logic 查询逻辑 and or xor + * @param mixed $field 查询字段 + * @param mixed $condition 查询条件 + * @param string $logic 查询逻辑 and or xor * @return $this */ public function whereNotLike($field, $condition, $logic = 'AND') @@ -1083,9 +1083,9 @@ class Query /** * 指定Between查询条件 * @access public - * @param mixed $field 查询字段 - * @param mixed $condition 查询条件 - * @param string $logic 查询逻辑 and or xor + * @param mixed $field 查询字段 + * @param mixed $condition 查询条件 + * @param string $logic 查询逻辑 and or xor * @return $this */ public function whereBetween($field, $condition, $logic = 'AND') @@ -1097,9 +1097,9 @@ class Query /** * 指定NotBetween查询条件 * @access public - * @param mixed $field 查询字段 - * @param mixed $condition 查询条件 - * @param string $logic 查询逻辑 and or xor + * @param mixed $field 查询字段 + * @param mixed $condition 查询条件 + * @param string $logic 查询逻辑 and or xor * @return $this */ public function whereNotBetween($field, $condition, $logic = 'AND') @@ -1111,9 +1111,9 @@ class Query /** * 指定Exp查询条件 * @access public - * @param mixed $field 查询字段 - * @param mixed $condition 查询条件 - * @param string $logic 查询逻辑 and or xor + * @param mixed $field 查询字段 + * @param mixed $condition 查询条件 + * @param string $logic 查询逻辑 and or xor * @return $this */ public function whereExp($field, $condition, $logic = 'AND') @@ -1202,8 +1202,8 @@ class Query /** * 检查是否存在一个字段多次查询条件 * @access public - * @param string $field 查询字段 - * @param string $logic 查询逻辑 and or xor + * @param string $field 查询字段 + * @param string $logic 查询逻辑 and or xor * @return bool */ private function checkMultiField($field, $logic) @@ -1214,8 +1214,8 @@ class Query /** * 去除某个查询条件 * @access public - * @param string $field 查询字段 - * @param string $logic 查询逻辑 and or xor + * @param string $field 查询字段 + * @param string $logic 查询逻辑 and or xor * @return $this */ public function removeWhereField($field, $logic = 'AND') @@ -1230,7 +1230,7 @@ class Query /** * 去除查询参数 * @access public - * @param string|bool $option 参数名 true 表示去除所有参数 + * @param string|bool $option 参数名 true 表示去除所有参数 * @return $this */ public function removeOption($option = true) @@ -1280,14 +1280,14 @@ class Query * @param int|array $listRows 每页数量 数组表示配置参数 * @param int|bool $simple 是否简洁模式或者总记录数 * @param array $config 配置参数 - * page:当前页, - * path:url路径, - * query:url额外参数, - * fragment:url锚点, - * var_page:分页变量, - * list_rows:每页数量 - * type:分页类名 - * @return \think\paginator\Collection + * page:当前页, + * path:url路径, + * query:url额外参数, + * fragment:url锚点, + * var_page:分页变量, + * list_rows:每页数量 + * type:分页类名 + * @return \think\Paginator * @throws DbException */ public function paginate($listRows = null, $simple = false, $config = []) @@ -1317,9 +1317,9 @@ class Query if (!isset($total) && !$simple) { $options = $this->getOptions(); - if (isset($options['order'])) { - unset($this->options['order']); - } + + unset($this->options['order'], $this->options['limit'], $this->options['page'], $this->options['field']); + $bind = $this->bind; $total = $this->count(); $results = $this->options($options)->bind($bind)->page($page, $listRows)->select(); @@ -1710,7 +1710,7 @@ class Query } list($guid) = explode(' ', $tableName); - $db = $this->getConfig('database'); + $db = $this->getConfig('database'); if (!isset(self::$info[$db . '.' . $guid])) { if (!strpos($guid, '.')) { $schema = $db . '.' . $guid; @@ -1724,7 +1724,7 @@ class Query $info = $this->connection->getFields($guid); } $fields = array_keys($info); - $bind = $type = []; + $bind = $type = []; foreach ($info as $key => $val) { // 记录字段类型 $type[$key] = $val['type']; @@ -1889,7 +1889,7 @@ class Query $relation = $key; $with[$key] = $key; } elseif (is_string($relation) && strpos($relation, '.')) { - $with[$key] = $relation; + $with[$key] = $relation; list($relation, $subRelation) = explode('.', $relation, 2); } @@ -1911,8 +1911,8 @@ class Query /** * 关联统计 * @access public - * @param string|array $relation 关联方法名 - * @param bool $subQuery 是否使用子查询 + * @param string|array $relation 关联方法名 + * @param bool $subQuery 是否使用子查询 * @return $this */ public function withCount($relation, $subQuery = true) @@ -2227,7 +2227,7 @@ class Query if ($data instanceof Query) { return $data->select(); } elseif ($data instanceof \Closure) { - call_user_func_array($data, [ & $this]); + call_user_func_array($data, [& $this]); $data = null; } // 分析查询表达式 @@ -2327,7 +2327,7 @@ class Query if ($data instanceof Query) { return $data->find(); } elseif ($data instanceof \Closure) { - call_user_func_array($data, [ & $this]); + call_user_func_array($data, [& $this]); $data = null; } // 分析查询表达式 @@ -2659,10 +2659,10 @@ class Query if (isset($options['page'])) { // 根据页数计算limit list($page, $listRows) = $options['page']; - $page = $page > 0 ? $page : 1; - $listRows = $listRows > 0 ? $listRows : (is_numeric($options['limit']) ? $options['limit'] : 20); - $offset = $listRows * ($page - 1); - $options['limit'] = $offset . ',' . $listRows; + $page = $page > 0 ? $page : 1; + $listRows = $listRows > 0 ? $listRows : (is_numeric($options['limit']) ? $options['limit'] : 20); + $offset = $listRows * ($page - 1); + $options['limit'] = $offset . ',' . $listRows; } $this->options = []; @@ -2672,8 +2672,8 @@ class Query /** * 注册回调方法 * @access public - * @param string $event 事件名 - * @param callable $callback 回调方法 + * @param string $event 事件名 + * @param callable $callback 回调方法 * @return void */ public static function event($event, $callback) @@ -2684,8 +2684,8 @@ class Query /** * 触发事件 * @access protected - * @param string $event 事件名 - * @param mixed $options 当前查询参数 + * @param string $event 事件名 + * @param mixed $options 当前查询参数 * @return bool */ protected function trigger($event, $options = []) diff --git a/library/think/paginator/Collection.php b/library/think/paginator/Collection.php deleted file mode 100644 index 0c877388..00000000 --- a/library/think/paginator/Collection.php +++ /dev/null @@ -1,74 +0,0 @@ - -// +---------------------------------------------------------------------- - -namespace think\paginator; - -use Exception; -use think\Paginator; - -/** - * Class Collection - * @package think\paginator - * @method integer total() - * @method integer listRows() - * @method integer currentPage() - * @method string render() - * @method Paginator fragment($fragment) - * @method Paginator appends($key, $value) - * @method integer lastPage() - * @method boolean hasPages() - */ -class Collection extends \think\Collection -{ - - /** @var Paginator */ - protected $paginator; - - public function __construct($items = [], Paginator $paginator = null) - { - $this->paginator = $paginator; - parent::__construct($items); - } - - public static function make($items = [], Paginator $paginator = null) - { - return new static($items, $paginator); - } - - public function toArray() - { - if ($this->paginator) { - try { - $total = $this->total(); - } catch (Exception $e) { - $total = null; - } - - return [ - 'total' => $total, - 'per_page' => $this->listRows(), - 'current_page' => $this->currentPage(), - 'data' => parent::toArray() - ]; - } else { - return parent::toArray(); - } - } - - public function __call($method, $args) - { - if ($this->paginator && method_exists($this->paginator, $method)) { - return call_user_func_array([$this->paginator, $method], $args); - } else { - throw new Exception('method not exists:' . __CLASS__ . '->' . $method); - } - } -} From 00136a3f4be5da863ce5f3e82ab6b86bad9cd26d Mon Sep 17 00:00:00 2001 From: yunwuxin <448901948@qq.com> Date: Mon, 16 Jan 2017 15:17:50 +0800 Subject: [PATCH 022/125] =?UTF-8?q?=E6=B3=A8=E9=87=8A=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 1e1d4460..00191954 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -23,12 +23,11 @@ use think\model\relation\HasManyThrough; use think\model\relation\HasOne; use think\model\relation\MorphMany; use think\model\relation\MorphTo; -use think\paginator\Collection as PaginatorCollection; /** * Class Model * @package think - * @method static PaginatorCollection paginate(integer $listRows = 15, boolean $simple = false, array $config = []) 分页查询 + * @method static Paginator paginate(integer $listRows = 15, boolean $simple = false, array $config = []) 分页查询 * @method static mixed value($field, $default = null) 得到某个字段的值 * @method static array column($field, $key = '') 得到某个列的数组 * @method static integer count($field = '*') COUNT查询 From 47a869f3af6ce441c99592d0e86fdee2cc11229a Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 16 Jan 2017 15:52:21 +0800 Subject: [PATCH 023/125] =?UTF-8?q?Query=E7=B1=BBrelation=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=B5=8C=E5=A5=97=E5=85=B3=E8=81=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 8 ++++++-- library/think/model/relation/BelongsTo.php | 6 +++--- library/think/model/relation/BelongsToMany.php | 5 +++-- library/think/model/relation/HasMany.php | 5 +++-- library/think/model/relation/HasManyThrough.php | 5 +++-- library/think/model/relation/HasOne.php | 5 +++-- library/think/model/relation/MorphMany.php | 5 +++-- library/think/model/relation/MorphTo.php | 5 +++-- 8 files changed, 27 insertions(+), 17 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 00191954..d4003a2d 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1282,14 +1282,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } foreach ($relations as $key => $relation) { - $closure = null; + $subRelation = ''; + $closure = null; if ($relation instanceof \Closure) { // 支持闭包查询过滤关联条件 $closure = $relation; $relation = $key; } + if (strpos($relation, '.')) { + list($relation, $subRelation) = explode('.', $relation); + } $method = Loader::parseName($relation, 1, false); - $this->data[$relation] = $this->$method()->getRelation($closure); + $this->data[$relation] = $this->$method()->getRelation($subRelation, $closure); } return $this; } diff --git a/library/think/model/relation/BelongsTo.php b/library/think/model/relation/BelongsTo.php index a96d9028..1f7454b0 100644 --- a/library/think/model/relation/BelongsTo.php +++ b/library/think/model/relation/BelongsTo.php @@ -38,17 +38,17 @@ class BelongsTo extends OneToOne /** * 延迟获取关联数据 + * @param string $subRelation 子关联名 * @param \Closure $closure 闭包查询条件 * @access public */ - public function getRelation($closure = null) + public function getRelation($subRelation = '', $closure = null) { $foreignKey = $this->foreignKey; - $localKey = $this->localKey; if ($closure) { call_user_func_array($closure, [ & $this->query]); } - return $this->query->where($localKey, $this->parent->$foreignKey)->find(); + return $this->query->where($this->localKey, $this->parent->$foreignKey)->relation($subRelation)->find(); } /** diff --git a/library/think/model/relation/BelongsToMany.php b/library/think/model/relation/BelongsToMany.php index 25e1a078..9b63b011 100644 --- a/library/think/model/relation/BelongsToMany.php +++ b/library/think/model/relation/BelongsToMany.php @@ -46,10 +46,11 @@ class BelongsToMany extends Relation /** * 延迟获取关联数据 + * @param string $subRelation 子关联名 * @param \Closure $closure 闭包查询条件 * @access public */ - public function getRelation($closure = null) + public function getRelation($subRelation = '', $closure = null) { $foreignKey = $this->foreignKey; $localKey = $this->localKey; @@ -60,7 +61,7 @@ class BelongsToMany extends Relation // 关联查询 $pk = $this->parent->getPk(); $condition['pivot.' . $localKey] = $this->parent->$pk; - $result = $this->belongsToManyQuery($middle, $foreignKey, $localKey, $condition)->select(); + $result = $this->belongsToManyQuery($middle, $foreignKey, $localKey, $condition)->relation($subRelation)->select(); foreach ($result as $set) { $pivot = []; foreach ($set->getData() as $key => $val) { diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index 044cc88d..60bd1677 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -39,15 +39,16 @@ class HasMany extends Relation /** * 延迟获取关联数据 + * @param string $subRelation 子关联名 * @param \Closure $closure 闭包查询条件 * @access public */ - public function getRelation($closure = null) + public function getRelation($subRelation = '', $closure = null) { if ($closure) { call_user_func_array($closure, [ & $this->query]); } - return $this->select(); + return $this->relation($subRelation)->select(); } /** diff --git a/library/think/model/relation/HasManyThrough.php b/library/think/model/relation/HasManyThrough.php index 983daf22..fee83b00 100644 --- a/library/think/model/relation/HasManyThrough.php +++ b/library/think/model/relation/HasManyThrough.php @@ -49,15 +49,16 @@ class HasManyThrough extends Relation /** * 延迟获取关联数据 + * @param string $subRelation 子关联名 * @param \Closure $closure 闭包查询条件 * @access public */ - public function getRelation($closure = null) + public function getRelation($subRelation = '', $closure = null) { if ($closure) { call_user_func_array($closure, [ & $this->query]); } - return $this->select(); + return $this->relation($subRelation)->select(); } /** diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index 05230922..d2e1f9c4 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -38,10 +38,11 @@ class HasOne extends OneToOne /** * 延迟获取关联数据 + * @param string $subRelation 子关联名 * @param \Closure $closure 闭包查询条件 * @access public */ - public function getRelation($colsure = null) + public function getRelation($subRelation = '', $colsure = null) { // 执行关联定义方法 $localKey = $this->localKey; @@ -49,7 +50,7 @@ class HasOne extends OneToOne call_user_func_array($closure, [ & $this->query]); } // 判断关联类型执行查询 - return $this->query->where($this->foreignKey, $this->parent->$localKey)->find(); + return $this->query->where($this->foreignKey, $this->parent->$localKey)->relation($subRelation)->find(); } /** diff --git a/library/think/model/relation/MorphMany.php b/library/think/model/relation/MorphMany.php index 40c1f791..c4c23df7 100644 --- a/library/think/model/relation/MorphMany.php +++ b/library/think/model/relation/MorphMany.php @@ -45,15 +45,16 @@ class MorphMany extends Relation /** * 延迟获取关联数据 + * @param string $subRelation 子关联名 * @param \Closure $closure 闭包查询条件 * @access public */ - public function getRelation($closure = null) + public function getRelation($subRelation = '', $closure = null) { if ($closure) { call_user_func_array($closure, [ & $this->query]); } - return $this->select(); + return $this->relation($subRelation)->select(); } /** diff --git a/library/think/model/relation/MorphTo.php b/library/think/model/relation/MorphTo.php index 05017e8a..4014d7bd 100644 --- a/library/think/model/relation/MorphTo.php +++ b/library/think/model/relation/MorphTo.php @@ -39,10 +39,11 @@ class MorphTo extends Relation /** * 延迟获取关联数据 + * @param string $subRelation 子关联名 * @param \Closure $closure 闭包查询条件 * @access public */ - public function getRelation($closure = null) + public function getRelation($subRelation = '', $closure = null) { $morphKey = $this->morphKey; $morphType = $this->morphType; @@ -50,7 +51,7 @@ class MorphTo extends Relation $model = $this->parseModel($this->parent->$morphType); // 主键数据 $pk = $this->parent->$morphKey; - return (new $model)->find($pk); + return (new $model)->relation($subRelation)->find($pk); } /** From 2db77f1aad3ff7d3966579ab2d1f52050a993d2c Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 16 Jan 2017 15:55:05 +0800 Subject: [PATCH 024/125] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E5=85=B3=E8=81=94?= =?UTF-8?q?=E5=B5=8C=E5=A5=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index d4003a2d..a6f11353 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1290,7 +1290,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $relation = $key; } if (strpos($relation, '.')) { - list($relation, $subRelation) = explode('.', $relation); + list($relation, $subRelation) = explode('.', $relation, 2); } $method = Loader::parseName($relation, 1, false); $this->data[$relation] = $this->$method()->getRelation($subRelation, $closure); @@ -1317,7 +1317,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $relation = $key; } if (strpos($relation, '.')) { - list($relation, $subRelation) = explode('.', $relation); + list($relation, $subRelation) = explode('.', $relation, 2); } $relation = Loader::parseName($relation, 1, false); $this->$relation()->eagerlyResultSet($resultSet, $relation, $subRelation, $closure, $class); @@ -1344,7 +1344,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $relation = $key; } if (strpos($relation, '.')) { - list($relation, $subRelation) = explode('.', $relation); + list($relation, $subRelation) = explode('.', $relation, 2); } $relation = Loader::parseName($relation, 1, false); $this->$relation()->eagerlyResult($result, $relation, $subRelation, $closure, $class); From 04c81ca6b5c31f738ad21888b5ce26e4e4cb84ec Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 16 Jan 2017 17:03:53 +0800 Subject: [PATCH 025/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BModel=E7=B1=BB?= =?UTF-8?q?=E7=9A=84has=E6=96=B9=E6=B3=95=E7=AC=AC=E4=BA=8C=E4=B8=AA?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E6=94=AF=E6=8C=81=E4=BD=BF=E7=94=A8=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E6=88=96=E8=80=85=E9=97=AD=E5=8C=85=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E6=9D=A1=E4=BB=B6=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/library/think/Model.php b/library/think/Model.php index a6f11353..0e633c54 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1228,7 +1228,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * 根据关联条件查询当前模型 * @access public * @param string $relation 关联方法名 - * @param string $operator 比较操作符 + * @param mixed $operator 比较操作符 * @param integer $count 个数 * @param string $id 关联表的统计字段 * @return Model @@ -1236,6 +1236,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public static function has($relation, $operator = '>=', $count = 1, $id = '*') { $model = new static(); + if (is_array($operator) || $operator instanceof \Closure) { + return $model->$relation()->hasWhere($model, $operator); + } return $model->$relation()->has($model, $operator, $count, $id); } From 0877798447731ca391cdce91c23d7470ce0cef98 Mon Sep 17 00:00:00 2001 From: yunwuxin <448901948@qq.com> Date: Mon, 16 Jan 2017 17:29:07 +0800 Subject: [PATCH 026/125] =?UTF-8?q?=E4=BF=AE=E6=AD=A3cx=E6=A0=87=E7=AD=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/template/taglib/Cx.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library/think/template/taglib/Cx.php b/library/think/template/taglib/Cx.php index 1a23c764..2a9c4ff4 100644 --- a/library/think/template/taglib/Cx.php +++ b/library/think/template/taglib/Cx.php @@ -98,7 +98,7 @@ class Cx extends Taglib $name = $this->autoBuildVar($name); } - $parseStr .= 'if(is_array(' . $name . ') || ' . $name . ' instanceof \think\Collection): $' . $key . ' = 0;'; + $parseStr .= 'if(is_array(' . $name . ') || ' . $name . ' instanceof \think\Collection || ' . $name . ' instanceof \think\Paginator): $' . $key . ' = 0;'; // 设置了输出数组长度 if (0 != $offset || 'null' != $length) { $parseStr .= '$__LIST__ = is_array(' . $name . ') ? array_slice(' . $name . ',' . $offset . ',' . $length . ', true) : ' . $name . '->slice(' . $offset . ',' . $length . ', true); '; @@ -158,7 +158,7 @@ class Cx extends Taglib } else { $name = $this->autoBuildVar($name); } - $parseStr .= 'if(is_array(' . $name . ') || ' . $name . ' instanceof \think\Collection): '; + $parseStr .= 'if(is_array(' . $name . ') || ' . $name . ' instanceof \think\Collection || ' . $name . ' instanceof \think\Paginator): '; // 设置了输出数组长度 if (0 != $offset || 'null' != $length) { if (!isset($var)) { @@ -431,7 +431,7 @@ class Cx extends Taglib { $name = $tag['name']; $name = $this->autoBuildVar($name); - $parseStr = 'isEmpty())): ?>' . $content . ''; + $parseStr = 'isEmpty())): ?>' . $content . ''; return $parseStr; } @@ -448,7 +448,7 @@ class Cx extends Taglib { $name = $tag['name']; $name = $this->autoBuildVar($name); - $parseStr = 'isEmpty()))): ?>' . $content . ''; + $parseStr = 'isEmpty()))): ?>' . $content . ''; return $parseStr; } From ff4be602c0971090f1d732b9d015041579bc8f39 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 16 Jan 2017 17:36:15 +0800 Subject: [PATCH 027/125] =?UTF-8?q?=E8=B0=83=E6=95=B4HasMany=E7=B1=BB?= =?UTF-8?q?=E7=9A=84has=E5=92=8ChasWhere=E6=96=B9=E6=B3=95=EF=BC=8C?= =?UTF-8?q?=E5=8F=96=E6=B6=88=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 6 +++--- library/think/model/relation/HasMany.php | 22 +++++++++++----------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 0e633c54..c69d48c3 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1237,9 +1237,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess { $model = new static(); if (is_array($operator) || $operator instanceof \Closure) { - return $model->$relation()->hasWhere($model, $operator); + return $model->$relation()->hasWhere($operator); } - return $model->$relation()->has($model, $operator, $count, $id); + return $model->$relation()->has($operator, $count, $id); } /** @@ -1252,7 +1252,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public static function hasWhere($relation, $where = []) { $model = new static(); - return $model->$relation()->hasWhere($model, $where); + return $model->$relation()->hasWhere($where); } /** diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index 60bd1677..bf2e2757 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -212,16 +212,15 @@ class HasMany extends Relation /** * 根据关联条件查询当前模型 * @access public - * @param Model $model 模型对象 * @param string $operator 比较操作符 * @param integer $count 个数 * @param string $id 关联表的统计字段 * @return Query */ - public function has($model, $operator = '>=', $count = 1, $id = '*') + public function has($operator = '>=', $count = 1, $id = '*') { $table = $this->query->getTable(); - return $model->db()->alias('a') + return $this->parent->db()->alias('a') ->join($table . ' b', 'a.' . $this->localKey . '=b.' . $this->foreignKey, $this->joinType) ->group('b.' . $this->foreignKey) ->having('count(' . $id . ')' . $operator . $count); @@ -230,24 +229,25 @@ class HasMany extends Relation /** * 根据关联条件查询当前模型 * @access public - * @param Model $model 模型对象 - * @param mixed $where 查询条件(数组或者闭包) + * @param mixed $where 查询条件(数组或者闭包) * @return Query */ - public function hasWhere($model, $where = []) + public function hasWhere($where = []) { - $table = $this->query->getTable(); + $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['b.' . $key] = $val; + $where[$relation . '.' . $key] = $val; unset($where[$key]); } } } - return $model->db()->alias('a') - ->field('a.*') - ->join($table . ' b', 'a.' . $this->localKey . '=b.' . $this->foreignKey, $this->joinType) + return $this->parent->db()->alias($model) + ->field($model . '.*') + ->join($table . ' ' . $relation, $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey, $this->joinType) ->where($where); } From 1568af03d836bebe968e27c680387c8c5d761792 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 16 Jan 2017 23:03:12 +0800 Subject: [PATCH 028/125] =?UTF-8?q?=E5=8F=96=E6=B6=88=E5=85=B3=E8=81=94?= =?UTF-8?q?=E7=B1=BB=E6=97=A0=E6=95=88=E7=9A=84alias=E5=8F=82=E6=95=B0=20?= =?UTF-8?q?=E4=BB=85morphTo=E5=85=B3=E8=81=94=E4=BF=9D=E7=95=99=20?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E8=AE=BE=E7=BD=AE=E5=A4=9A=E6=80=81=E5=88=AB?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 23 ++++++++----------- library/think/model/Relation.php | 14 ----------- library/think/model/relation/BelongsTo.php | 4 +--- .../think/model/relation/BelongsToMany.php | 4 +--- library/think/model/relation/HasMany.php | 4 +--- .../think/model/relation/HasManyThrough.php | 4 +--- library/think/model/relation/HasOne.php | 4 +--- library/think/model/relation/MorphTo.php | 14 +++++++++++ library/think/model/relation/OneToOne.php | 4 ++-- 9 files changed, 31 insertions(+), 44 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index c69d48c3..fbd19454 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1383,7 +1383,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @param string $model 模型名 * @param string $foreignKey 关联外键 * @param string $localKey 关联主键 - * @param array $alias 别名定义 + * @param array $alias 别名定义(已经废弃) * @param string $joinType JOIN类型 * @return HasOne */ @@ -1393,7 +1393,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $model = $this->parseModel($model); $localKey = $localKey ?: $this->getPk(); $foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; - return new HasOne($this, $model, $foreignKey, $localKey, $alias, $joinType); + return new HasOne($this, $model, $foreignKey, $localKey, $joinType); } /** @@ -1402,7 +1402,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @param string $model 模型名 * @param string $foreignKey 关联外键 * @param string $otherKey 关联主键 - * @param array $alias 别名定义 + * @param array $alias 别名定义(已经废弃) * @param string $joinType JOIN类型 * @return BelongsTo */ @@ -1412,7 +1412,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $model = $this->parseModel($model); $foreignKey = $foreignKey ?: Loader::parseName(basename(str_replace('\\', '/', $model))) . '_id'; $otherKey = $otherKey ?: (new $model)->getPk(); - return new BelongsTo($this, $model, $foreignKey, $otherKey, $alias, $joinType); + return new BelongsTo($this, $model, $foreignKey, $otherKey, $joinType); } /** @@ -1421,16 +1421,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @param string $model 模型名 * @param string $foreignKey 关联外键 * @param string $localKey 关联主键 - * @param array $alias 别名定义 * @return HasMany */ - public function hasMany($model, $foreignKey = '', $localKey = '', $alias = []) + public function hasMany($model, $foreignKey = '', $localKey = '') { // 记录当前关联信息 $model = $this->parseModel($model); $localKey = $localKey ?: $this->getPk(); $foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; - return new HasMany($this, $model, $foreignKey, $localKey, $alias); + return new HasMany($this, $model, $foreignKey, $localKey); } /** @@ -1441,10 +1440,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @param string $foreignKey 关联外键 * @param string $throughKey 关联外键 * @param string $localKey 关联主键 - * @param array $alias 别名定义 * @return HasManyThrough */ - public function hasManyThrough($model, $through, $foreignKey = '', $throughKey = '', $localKey = '', $alias = []) + public function hasManyThrough($model, $through, $foreignKey = '', $throughKey = '', $localKey = '') { // 记录当前关联信息 $model = $this->parseModel($model); @@ -1453,7 +1451,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; $name = Loader::parseName(basename(str_replace('\\', '/', $through))); $throughKey = $throughKey ?: $name . '_id'; - return new HasManyThrough($this, $model, $through, $foreignKey, $throughKey, $localKey, $alias); + return new HasManyThrough($this, $model, $through, $foreignKey, $throughKey, $localKey); } /** @@ -1463,10 +1461,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @param string $table 中间表名 * @param string $foreignKey 关联外键 * @param string $localKey 当前模型关联键 - * @param array $alias 别名定义 * @return BelongsToMany */ - public function belongsToMany($model, $table = '', $foreignKey = '', $localKey = '', $alias = []) + public function belongsToMany($model, $table = '', $foreignKey = '', $localKey = '') { // 记录当前关联信息 $model = $this->parseModel($model); @@ -1474,7 +1471,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $table = $table ?: $this->db(false)->getTable(Loader::parseName($this->name) . '_' . $name); $foreignKey = $foreignKey ?: $name . '_id'; $localKey = $localKey ?: Loader::parseName($this->name) . '_id'; - return new BelongsToMany($this, $model, $table, $foreignKey, $localKey, $alias); + return new BelongsToMany($this, $model, $table, $foreignKey, $localKey); } /** diff --git a/library/think/model/Relation.php b/library/think/model/Relation.php index e940a78d..8797cca4 100644 --- a/library/think/model/Relation.php +++ b/library/think/model/Relation.php @@ -25,8 +25,6 @@ abstract class Relation protected $foreignKey; // 关联表主键 protected $localKey; - // 数据表别名 - protected $alias; // 当前关联的JOIN类型 protected $joinType; // 关联模型查询对象 @@ -80,18 +78,6 @@ abstract class Relation return $class ? new $class($resultSet) : $resultSet; } - /** - * 设置当前关联定义的数据表别名 - * @access public - * @param array $alias 别名定义 - * @return $this - */ - public function setAlias($alias) - { - $this->alias = $alias; - return $this; - } - /** * 移除关联查询参数 * @access public diff --git a/library/think/model/relation/BelongsTo.php b/library/think/model/relation/BelongsTo.php index 1f7454b0..319e62fb 100644 --- a/library/think/model/relation/BelongsTo.php +++ b/library/think/model/relation/BelongsTo.php @@ -22,16 +22,14 @@ class BelongsTo extends OneToOne * @param string $model 模型名 * @param string $foreignKey 关联外键 * @param string $localKey 关联主键 - * @param array $alias 别名定义 * @param string $joinType JOIN类型 */ - public function __construct(Model $parent, $model, $foreignKey, $localKey, $alias = [], $joinType = 'INNER') + public function __construct(Model $parent, $model, $foreignKey, $localKey, $joinType = 'INNER') { $this->parent = $parent; $this->model = $model; $this->foreignKey = $foreignKey; $this->localKey = $localKey; - $this->alias = $alias; $this->joinType = $joinType; $this->query = (new $model)->db(); } diff --git a/library/think/model/relation/BelongsToMany.php b/library/think/model/relation/BelongsToMany.php index 9b63b011..2d6c353f 100644 --- a/library/think/model/relation/BelongsToMany.php +++ b/library/think/model/relation/BelongsToMany.php @@ -31,16 +31,14 @@ class BelongsToMany extends Relation * @param string $table 中间表名 * @param string $foreignKey 关联模型外键 * @param string $localKey 当前模型关联键 - * @param array $alias 别名定义 */ - public function __construct(Model $parent, $model, $table, $foreignKey, $localKey, $alias = []) + public function __construct(Model $parent, $model, $table, $foreignKey, $localKey) { $this->parent = $parent; $this->model = $model; $this->foreignKey = $foreignKey; $this->localKey = $localKey; $this->middle = $table; - $this->alias = $alias; $this->query = (new $model)->db(); } diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index bf2e2757..96688cb1 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -25,15 +25,13 @@ class HasMany extends Relation * @param string $model 模型名 * @param string $foreignKey 关联外键 * @param string $localKey 关联主键 - * @param array $alias 别名定义 */ - public function __construct(Model $parent, $model, $foreignKey, $localKey, $alias = []) + public function __construct(Model $parent, $model, $foreignKey, $localKey) { $this->parent = $parent; $this->model = $model; $this->foreignKey = $foreignKey; $this->localKey = $localKey; - $this->alias = $alias; $this->query = (new $model)->db(); } diff --git a/library/think/model/relation/HasManyThrough.php b/library/think/model/relation/HasManyThrough.php index fee83b00..00caa8ad 100644 --- a/library/think/model/relation/HasManyThrough.php +++ b/library/think/model/relation/HasManyThrough.php @@ -33,9 +33,8 @@ class HasManyThrough extends Relation * @param string $firstkey 关联外键 * @param string $secondKey 关联外键 * @param string $localKey 关联主键 - * @param array $alias 别名定义 */ - public function __construct(Model $parent, $model, $through, $foreignKey, $throughKey, $localKey, $alias = []) + public function __construct(Model $parent, $model, $through, $foreignKey, $throughKey, $localKey) { $this->parent = $parent; $this->model = $model; @@ -43,7 +42,6 @@ class HasManyThrough extends Relation $this->foreignKey = $foreignKey; $this->throughKey = $throughKey; $this->localKey = $localKey; - $this->alias = $alias; $this->query = (new $model)->db(); } diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index d2e1f9c4..f0b9b9cb 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -22,16 +22,14 @@ class HasOne extends OneToOne * @param string $model 模型名 * @param string $foreignKey 关联外键 * @param string $localKey 关联主键 - * @param array $alias 别名定义 * @param string $joinType JOIN类型 */ - public function __construct(Model $parent, $model, $foreignKey, $localKey, $alias = [], $joinType = 'INNER') + public function __construct(Model $parent, $model, $foreignKey, $localKey, $joinType = 'INNER') { $this->parent = $parent; $this->model = $model; $this->foreignKey = $foreignKey; $this->localKey = $localKey; - $this->alias = $alias; $this->joinType = $joinType; $this->query = (new $model)->db(); } diff --git a/library/think/model/relation/MorphTo.php b/library/think/model/relation/MorphTo.php index 4014d7bd..a4672233 100644 --- a/library/think/model/relation/MorphTo.php +++ b/library/think/model/relation/MorphTo.php @@ -20,6 +20,8 @@ class MorphTo extends Relation // 多态字段 protected $morphKey; protected $morphType; + // 多态别名 + protected $alias; /** * 架构函数 @@ -74,6 +76,18 @@ class MorphTo extends Relation return $model; } + /** + * 设置多态别名 + * @access public + * @param array $alias 别名定义 + * @return $this + */ + public function setAlias($alias) + { + $this->alias = $alias; + return $this; + } + /** * 预载入关联查询 * @access public diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index ef21bee0..549a389a 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -37,7 +37,7 @@ abstract class OneToOne extends Relation public function eagerly(Query $query, $relation, $subRelation, $closure, $first) { $name = Loader::parseName(basename(str_replace('\\', '/', $query->getModel()))); - $alias = isset($this->alias[$name]) ? $this->alias[$name] : $name; + $alias = $name; if ($first) { $table = $query->getTable(); $query->table([$table => $alias]); @@ -53,7 +53,7 @@ abstract class OneToOne extends Relation // 预载入封装 $joinTable = $this->query->getTable(); $joinName = Loader::parseName(basename(str_replace('\\', '/', $this->model))); - $joinAlias = isset($this->alias[$joinName]) ? $this->alias[$joinName] : $relation; + $joinAlias = $relation; $query->via($joinAlias); if ($this instanceof BelongsTo) { From d68115c946c68c029bafd267aefe369a9135e513 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 16 Jan 2017 23:03:23 +0800 Subject: [PATCH 029/125] =?UTF-8?q?=E5=A2=9E=E5=8A=A0collection=E5=8A=A9?= =?UTF-8?q?=E6=89=8B=E5=87=BD=E6=95=B0=20=E5=BF=AB=E9=80=9F=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2=E6=95=B0=E7=BB=84=E4=B8=BA=E6=95=B0=E6=8D=AE=E9=9B=86?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/helper.php b/helper.php index e8af0a52..367e827f 100644 --- a/helper.php +++ b/helper.php @@ -23,6 +23,7 @@ use think\exception\HttpResponseException; use think\Lang; use think\Loader; use think\Log; +use think\model\Collection; use think\Request; use think\Response; use think\Session; @@ -565,3 +566,15 @@ if (!function_exists('load_relation')) { return $resultSet; } } + +if (!function_exists('collection')) { + /** + * 数组转换为数据集对象 + * @param array $resultSet 数据集数组 + * @return Collection + */ + function collection($resultSet) + { + return new Collection($resultSet); + } +} From 8b962ae5c5d027e4040ea7af142e2ea8df9bd10f Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 16 Jan 2017 23:09:26 +0800 Subject: [PATCH 030/125] =?UTF-8?q?=E4=B8=80=E5=AF=B9=E4=B8=80=E5=85=B3?= =?UTF-8?q?=E8=81=94=E7=B1=BB=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/Relation.php | 6 ++---- library/think/model/relation/OneToOne.php | 2 ++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/library/think/model/Relation.php b/library/think/model/Relation.php index 8797cca4..b16d08e4 100644 --- a/library/think/model/Relation.php +++ b/library/think/model/Relation.php @@ -21,14 +21,12 @@ abstract class Relation protected $parent; /** @var Model 当前关联的模型类 */ protected $model; + // 关联模型查询对象 + protected $query; // 关联表外键 protected $foreignKey; // 关联表主键 protected $localKey; - // 当前关联的JOIN类型 - protected $joinType; - // 关联模型查询对象 - protected $query; // 关联查询条件 protected $where; // 关联查询参数 diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index 549a389a..ea16b92b 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -21,6 +21,8 @@ abstract class OneToOne extends Relation { // 预载入方式 protected $eagerlyType = 0; + // 当前关联的JOIN类型 + protected $joinType; // 要绑定的属性 protected $bindAttr = []; From ebfcbac863de681bee227b588bc94f09b5be2045 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 17 Jan 2017 08:19:20 +0800 Subject: [PATCH 031/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BModel=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index fbd19454..dc745b7f 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1377,6 +1377,20 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } } + /** + * 获取模型的默认外键名 + * @access public + * @param string $name 模型名 + * @return string + */ + protected function getForeignKey($name) + { + if (strpos($name, '\\')) { + $name = basename(str_replace('\\', '/', $model)); + } + return Loader::parseName($name) . '_id'; + } + /** * HAS ONE 关联定义 * @access public @@ -1392,7 +1406,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 记录当前关联信息 $model = $this->parseModel($model); $localKey = $localKey ?: $this->getPk(); - $foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; + $foreignKey = $foreignKey ?: $this->getForeignKey($this->name); return new HasOne($this, $model, $foreignKey, $localKey, $joinType); } @@ -1410,7 +1424,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess { // 记录当前关联信息 $model = $this->parseModel($model); - $foreignKey = $foreignKey ?: Loader::parseName(basename(str_replace('\\', '/', $model))) . '_id'; + $foreignKey = $foreignKey ?: $this->getForeignKey($model); $otherKey = $otherKey ?: (new $model)->getPk(); return new BelongsTo($this, $model, $foreignKey, $otherKey, $joinType); } @@ -1428,7 +1442,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 记录当前关联信息 $model = $this->parseModel($model); $localKey = $localKey ?: $this->getPk(); - $foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; + $foreignKey = $foreignKey ?: $this->getForeignKey($this->name); return new HasMany($this, $model, $foreignKey, $localKey); } @@ -1448,9 +1462,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $model = $this->parseModel($model); $through = $this->parseModel($through); $localKey = $localKey ?: $this->getPk(); - $foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; - $name = Loader::parseName(basename(str_replace('\\', '/', $through))); - $throughKey = $throughKey ?: $name . '_id'; + $foreignKey = $foreignKey ?: $this->getForeignKey($this->name); + $throughKey = $throughKey ?: $this->getForeignKey($through); return new HasManyThrough($this, $model, $through, $foreignKey, $throughKey, $localKey); } @@ -1470,7 +1483,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $name = Loader::parseName(basename(str_replace('\\', '/', $model))); $table = $table ?: $this->db(false)->getTable(Loader::parseName($this->name) . '_' . $name); $foreignKey = $foreignKey ?: $name . '_id'; - $localKey = $localKey ?: Loader::parseName($this->name) . '_id'; + $localKey = $localKey ?: $this->getForeignKey($this->name); return new BelongsToMany($this, $model, $table, $foreignKey, $localKey); } From ed3becf64fcf4f7a50177558387bdf9d5aaa9cdc Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 17 Jan 2017 10:53:14 +0800 Subject: [PATCH 032/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BHasMany=E5=92=8COneTo?= =?UTF-8?q?One?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/HasMany.php | 2 +- library/think/model/relation/HasOne.php | 15 ++++++++------- library/think/model/relation/OneToOne.php | 12 ++++++++++++ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index 96688cb1..2ddf370f 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -245,7 +245,7 @@ class HasMany extends Relation } return $this->parent->db()->alias($model) ->field($model . '.*') - ->join($table . ' ' . $relation, $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey, $this->joinType) + ->join($table . ' ' . $relation, $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey) ->where($where); } diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index f0b9b9cb..339a271e 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -54,24 +54,25 @@ class HasOne extends OneToOne /** * 根据关联条件查询当前模型 * @access public - * @param Model $model 模型对象 * @param mixed $where 查询条件(数组或者闭包) * @return Query */ - public function hasWhere($model, $where = []) + public function hasWhere($where = []) { - $table = $this->query->getTable(); + $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['b.' . $key] = $val; + $where[$relation . '.' . $key] = $val; unset($where[$key]); } } } - return $model->db()->alias('a') - ->field('a.*') - ->join($table . ' b', 'a.' . $this->localKey . '=b.' . $this->foreignKey, $this->joinType) + return $this->parent->db()->alias($model) + ->field($model . '.*') + ->join($table . ' ' . $relation, $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey, $this->joinType) ->where($where); } diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index ea16b92b..e7d7f0b6 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -26,6 +26,18 @@ abstract class OneToOne extends Relation // 要绑定的属性 protected $bindAttr = []; + /** + * 设置join类型 + * @access public + * @param string $type JOIN类型 + * @return $this + */ + public function joinType($type) + { + $this->joinType = $type; + return $this; + } + /** * 预载入关联查询(JOIN方式) * @access public From a3fd00eb3594c0f78edd51fedfe4357e03b0ba4d Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 17 Jan 2017 10:55:26 +0800 Subject: [PATCH 033/125] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=B8=80=E5=A4=84?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/think/Model.php b/library/think/Model.php index dc745b7f..a7e5dda2 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1386,7 +1386,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected function getForeignKey($name) { if (strpos($name, '\\')) { - $name = basename(str_replace('\\', '/', $model)); + $name = basename(str_replace('\\', '/', $name)); } return Loader::parseName($name) . '_id'; } From fea1b5958235d5880400c68e1d67f3e9715ff99e Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 17 Jan 2017 11:42:12 +0800 Subject: [PATCH 034/125] =?UTF-8?q?=E6=94=B9=E8=BF=9Bcookie=E5=8A=A9?= =?UTF-8?q?=E6=89=8B=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helper.php b/helper.php index 367e827f..d74f3d58 100644 --- a/helper.php +++ b/helper.php @@ -330,7 +330,7 @@ if (!function_exists('cookie')) { Cookie::clear($value); } elseif ('' === $value) { // 获取 - return 0 === strpos($name, '?') ? Cookie::has(substr($name, 1), $option) : Cookie::get($name); + return 0 === strpos($name, '?') ? Cookie::has(substr($name, 1), $option) : Cookie::get($name, $option); } elseif (is_null($value)) { // 删除 return Cookie::delete($name); From a1cb6ea4cabab32a38cb881e2e4d99ad9c93fb0b Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 17 Jan 2017 13:45:51 +0800 Subject: [PATCH 035/125] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=B8=80=E5=A4=84?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/HasOne.php | 2 +- library/think/model/relation/OneToOne.php | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index 339a271e..b1fa25f0 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -40,7 +40,7 @@ class HasOne extends OneToOne * @param \Closure $closure 闭包查询条件 * @access public */ - public function getRelation($subRelation = '', $colsure = null) + public function getRelation($subRelation = '', $closure = null) { // 执行关联定义方法 $localKey = $this->localKey; diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index e7d7f0b6..63903a47 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -19,7 +19,7 @@ use think\model\Relation; abstract class OneToOne extends Relation { - // 预载入方式 + // 预载入方式 0 -JOIN 1 -IN protected $eagerlyType = 0; // 当前关联的JOIN类型 protected $joinType; @@ -79,8 +79,7 @@ abstract class OneToOne extends Relation if ($closure) { // 执行闭包查询 call_user_func_array($closure, [ & $query]); - //指定获取关联的字段 - //需要在 回调中 调方法 withField 方法,如 + // 使用withField指定获取关联的字段,如 // $query->where(['id'=>1])->withField('id,name'); if ($query->getOptions('with_field')) { $field = $query->getOptions('with_field'); From be6d528f68773dab4f5d552bf0480c8b71d518dc Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 18 Jan 2017 10:25:10 +0800 Subject: [PATCH 036/125] =?UTF-8?q?collection=E5=8A=A9=E6=89=8B=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E8=BF=94=E5=9B=9Ethink\Collection=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper.php | 2 +- library/think/model/relation/MorphMany.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/helper.php b/helper.php index d74f3d58..d5946fb2 100644 --- a/helper.php +++ b/helper.php @@ -14,6 +14,7 @@ //------------------------- use think\Cache; +use think\Collection; use think\Config; use think\Cookie; use think\Db; @@ -23,7 +24,6 @@ use think\exception\HttpResponseException; use think\Lang; use think\Loader; use think\Log; -use think\model\Collection; use think\Request; use think\Response; use think\Session; diff --git a/library/think/model/relation/MorphMany.php b/library/think/model/relation/MorphMany.php index c4c23df7..68eb0834 100644 --- a/library/think/model/relation/MorphMany.php +++ b/library/think/model/relation/MorphMany.php @@ -160,6 +160,7 @@ class MorphMany extends Relation * @param array $where 关联预查询条件 * @param string $relation 关联名 * @param string $subRelation 子关联 + * @param \Closure $closure 闭包 * @return array */ protected function eagerlyMorphToMany($where, $relation, $subRelation = '', $closure = false) From 4c2665b9f9d1b8bd4b6d3d354f88b971f5b8edc9 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 18 Jan 2017 13:49:50 +0800 Subject: [PATCH 037/125] =?UTF-8?q?=E4=B8=80=E5=AF=B9=E4=B8=80=E9=A2=84?= =?UTF-8?q?=E8=BD=BD=E5=85=A5=E6=9F=A5=E8=AF=A2=E9=BB=98=E8=AE=A4=E6=94=B9?= =?UTF-8?q?=E4=B8=BAIN=E6=9F=A5=E8=AF=A2=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/OneToOne.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index 63903a47..da989798 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -20,7 +20,7 @@ use think\model\Relation; abstract class OneToOne extends Relation { // 预载入方式 0 -JOIN 1 -IN - protected $eagerlyType = 0; + protected $eagerlyType = 1; // 当前关联的JOIN类型 protected $joinType; // 要绑定的属性 From 4a7e4d5d28b8f051a834436c4f194f51700377c2 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 18 Jan 2017 13:56:41 +0800 Subject: [PATCH 038/125] =?UTF-8?q?=E4=B8=80=E5=AF=B9=E4=B8=80=E9=A2=84?= =?UTF-8?q?=E8=BD=BD=E5=85=A5=20In=E6=9F=A5=E8=AF=A2=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E5=85=BC=E5=AE=B9withField=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/OneToOne.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index da989798..69f57072 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -267,6 +267,10 @@ abstract class OneToOne extends Relation // 预载入关联查询 支持嵌套预载入 if ($closure) { call_user_func_array($closure, [ & $model]); + if ($model->getOptions('with_field')) { + $model->field($model->getOptions('with_field')); + $model->removeOption('with_field'); + } } $list = $model->where($where)->with($subRelation)->select(); From cffb772743a94d803dd3223189f39acdb878f4fb Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 18 Jan 2017 13:58:27 +0800 Subject: [PATCH 039/125] =?UTF-8?q?=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/OneToOne.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index 69f57072..0ff739cc 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -267,9 +267,8 @@ abstract class OneToOne extends Relation // 预载入关联查询 支持嵌套预载入 if ($closure) { call_user_func_array($closure, [ & $model]); - if ($model->getOptions('with_field')) { - $model->field($model->getOptions('with_field')); - $model->removeOption('with_field'); + if ($field = $model->getOptions('with_field')) { + $model->field($field)->removeOption('with_field'); } } $list = $model->where($where)->with($subRelation)->select(); From da2421113bf861915383f97f53ff0889697232b6 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 18 Jan 2017 22:01:28 +0800 Subject: [PATCH 040/125] =?UTF-8?q?=E5=8F=98=E9=87=8F=E5=90=8D=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index dc745b7f..d671028d 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1415,18 +1415,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @access public * @param string $model 模型名 * @param string $foreignKey 关联外键 - * @param string $otherKey 关联主键 + * @param string $localKey 关联主键 * @param array $alias 别名定义(已经废弃) * @param string $joinType JOIN类型 * @return BelongsTo */ - public function belongsTo($model, $foreignKey = '', $otherKey = '', $alias = [], $joinType = 'INNER') + public function belongsTo($model, $foreignKey = '', $localKey = '', $alias = [], $joinType = 'INNER') { // 记录当前关联信息 $model = $this->parseModel($model); $foreignKey = $foreignKey ?: $this->getForeignKey($model); - $otherKey = $otherKey ?: (new $model)->getPk(); - return new BelongsTo($this, $model, $foreignKey, $otherKey, $joinType); + $localKey = $localKey ?: (new $model)->getPk(); + return new BelongsTo($this, $model, $foreignKey, $localKey, $joinType); } /** From 6afb6e981ca88f5dff7a8fc061e66c5443c41ab4 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 19 Jan 2017 12:35:20 +0800 Subject: [PATCH 041/125] =?UTF-8?q?Model=E7=B1=BB=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=B8=80=E5=AF=B9=E4=B8=80=E5=85=B3=E8=81=94=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=86=99=E5=85=A5=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 61 +++++++++++++++++++++++ library/think/model/relation/OneToOne.php | 2 +- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/library/think/Model.php b/library/think/Model.php index a7e5dda2..9abc1c2b 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -107,6 +107,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected $batchValidate = false; // 查询数据集对象 protected $resultSetType; + // 关联自动写入 + protected $relationWrite; // protected static $db; @@ -657,6 +659,21 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return $collection; } + /** + * 关联数据一起更新 + * @access public + * @param mixed $relation 关联 + * @return $this + */ + public function together($relation) + { + if (is_string($relation)) { + $relation = explode(',', $relation); + } + $this->relationWrite = $relation; + return $this; + } + /** * 获取模型对象的主键 * @access public @@ -715,6 +732,19 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } } + // 自动关联写入 + if (!empty($this->relationWrite)) { + $relation = []; + foreach ($this->relationWrite as $name) { + if (isset($this->data[$name])) { + $relation[$name] = $this->data[$name]; + if (!$this->isUpdate) { + unset($this->data[$name]); + } + } + } + } + // 检测字段 if (!empty($this->field)) { foreach ($this->data as $key => $val) { @@ -775,7 +805,29 @@ abstract class Model implements \JsonSerializable, \ArrayAccess unset($data[$pk]); } + // 关联更新 + if (isset($relation)) { + foreach ($relation as $name => $val) { + if (isset($data[$name])) { + unset($data[$name]); + } + } + } + + // 模型更新 $result = $this->db()->where($where)->update($data); + + // 关联更新 + if (isset($relation)) { + foreach ($relation as $name => $val) { + if ($val instanceof Model) { + $val->save(); + } else { + $this->getAttr($name)->save($val); + } + } + } + // 清空change $this->change = []; // 更新回调 @@ -802,6 +854,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $this->data[$pk] = $insertId; } } + + // 关联写入 + if (isset($relation)) { + foreach ($relation as $name => $val) { + $method = Loader::parseName($name, 1, false); + $this->$method()->save($val); + } + } + // 标记为更新 $this->isUpdate = true; // 清空change diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index 0ff739cc..c07cce05 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -276,7 +276,7 @@ abstract class OneToOne extends Relation // 组装模型数据 $data = []; foreach ($list as $set) { - $data[$set->$key][] = $set; + $data[$set->$key] = $set; } return $data; } From 542b432fa6e1318839d857007d8b654abd097233 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 19 Jan 2017 12:57:13 +0800 Subject: [PATCH 042/125] =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=81=9A=E5=90=88?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index eb693ce9..8af809c0 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -735,8 +735,16 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 自动关联写入 if (!empty($this->relationWrite)) { $relation = []; - foreach ($this->relationWrite as $name) { - if (isset($this->data[$name])) { + foreach ($this->relationWrite as $key => $name) { + if (!is_numeric($key)) { + $relation[$key] = []; + foreach ($name as $val) { + if (isset($this->data[$val])) { + $relation[$key][$val] = $this->data[$val]; + unset($this->data[$val]); + } + } + } elseif (isset($this->data[$name])) { $relation[$name] = $this->data[$name]; if (!$this->isUpdate) { unset($this->data[$name]); From 55bcfca1bd86e54360fa5163ba792603bacfb4d8 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 19 Jan 2017 13:50:57 +0800 Subject: [PATCH 043/125] =?UTF-8?q?Model=E7=B1=BB=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=B8=80=E5=AF=B9=E4=B8=80=E5=85=B3=E8=81=94=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/library/think/Model.php b/library/think/Model.php index 8af809c0..21fc10f0 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -831,7 +831,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess if ($val instanceof Model) { $val->save(); } else { - $this->getAttr($name)->save($val); + $model = $this->getAttr($name); + if ($model instanceof Model) { + $model->save($val); + } } } } @@ -987,8 +990,20 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return false; } + // 删除当前模型数据 $result = $this->db()->delete($this->data); + // 关联删除 + if (!empty($this->relationWrite)) { + foreach ($this->relationWrite as $key => $name) { + $name = is_numeric($key) ? $name : $key; + $model = $this->getAttr($name); + if ($model instanceof Model) { + $model->delete(); + } + } + } + $this->trigger('after_delete', $this); return $result; } From ecd0ff35361615395b69ac4d809bc72eeb36b5e7 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 19 Jan 2017 14:18:26 +0800 Subject: [PATCH 044/125] =?UTF-8?q?=E5=AE=8C=E5=96=84Model=E7=9A=84?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=85=B3=E8=81=94=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 1 + 1 file changed, 1 insertion(+) diff --git a/library/think/Model.php b/library/think/Model.php index 21fc10f0..3455b4a8 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -831,6 +831,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess if ($val instanceof Model) { $val->save(); } else { + unset($this->data[$name]); $model = $this->getAttr($name); if ($model instanceof Model) { $model->save($val); From e1b35abd5165913f066df5ca1c6d10035248e69d Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 19 Jan 2017 18:52:26 +0800 Subject: [PATCH 045/125] =?UTF-8?q?=E6=94=B9=E8=BF=9Bcount=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E5=AF=B9group=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index da5990a8..348535e8 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -509,10 +509,17 @@ class Query * COUNT查询 * @access public * @param string $field 字段名 - * @return integer + * @return integer|string */ public function count($field = '*') { + if (isset($this->options['group'])) { + // 支持GROUP + $options = $this->getOptions(); + $subSql = $this->options($options)->field('count(' . $field . ')')->bind($this->bind)->buildSql(); + return $this->table([$subSql => '_group_count_'])->value('COUNT(*) AS tp_count', 0, true); + } + return $this->value('COUNT(' . $field . ') AS tp_count', 0, true); } @@ -1710,7 +1717,7 @@ class Query } list($guid) = explode(' ', $tableName); - $db = $this->getConfig('database'); + $db = $this->getConfig('database'); if (!isset(self::$info[$db . '.' . $guid])) { if (!strpos($guid, '.')) { $schema = $db . '.' . $guid; @@ -1724,7 +1731,7 @@ class Query $info = $this->connection->getFields($guid); } $fields = array_keys($info); - $bind = $type = []; + $bind = $type = []; foreach ($info as $key => $val) { // 记录字段类型 $type[$key] = $val['type']; @@ -1889,7 +1896,7 @@ class Query $relation = $key; $with[$key] = $key; } elseif (is_string($relation) && strpos($relation, '.')) { - $with[$key] = $relation; + $with[$key] = $relation; list($relation, $subRelation) = explode('.', $relation, 2); } @@ -2227,7 +2234,7 @@ class Query if ($data instanceof Query) { return $data->select(); } elseif ($data instanceof \Closure) { - call_user_func_array($data, [& $this]); + call_user_func_array($data, [ & $this]); $data = null; } // 分析查询表达式 @@ -2327,7 +2334,7 @@ class Query if ($data instanceof Query) { return $data->find(); } elseif ($data instanceof \Closure) { - call_user_func_array($data, [& $this]); + call_user_func_array($data, [ & $this]); $data = null; } // 分析查询表达式 @@ -2659,10 +2666,10 @@ class Query if (isset($options['page'])) { // 根据页数计算limit list($page, $listRows) = $options['page']; - $page = $page > 0 ? $page : 1; - $listRows = $listRows > 0 ? $listRows : (is_numeric($options['limit']) ? $options['limit'] : 20); - $offset = $listRows * ($page - 1); - $options['limit'] = $offset . ',' . $listRows; + $page = $page > 0 ? $page : 1; + $listRows = $listRows > 0 ? $listRows : (is_numeric($options['limit']) ? $options['limit'] : 20); + $offset = $listRows * ($page - 1); + $options['limit'] = $offset . ',' . $listRows; } $this->options = []; From 35ae8decf73673109621cb242ebc7e23b4c23deb Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 20 Jan 2017 12:37:25 +0800 Subject: [PATCH 046/125] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E7=B1=BB=E7=9A=84=E5=B8=83=E5=B0=94=E5=80=BC=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Validate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/think/Validate.php b/library/think/Validate.php index e91ee338..ac9e6d55 100644 --- a/library/think/Validate.php +++ b/library/think/Validate.php @@ -591,7 +591,7 @@ class Validate break; case 'boolean': // 是否为布尔值 - $result = in_array($value, [0, 1, true, false]); + $result = in_array($value, [true, false, 0, 1, '0', '1'], true); break; case 'array': // 是否为数组 From 5981a54c4061a4a92587d53ebe1304df0bf053ae Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 20 Jan 2017 12:37:51 +0800 Subject: [PATCH 047/125] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E7=B1=BB=E7=9A=84allowField=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 3455b4a8..cfd404ad 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -755,6 +755,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 检测字段 if (!empty($this->field)) { + if (true === $this->field) { + $this->field = $this->db(false)->getTableInfo('', 'fields'); + } foreach ($this->data as $key => $val) { if (!in_array($key, $this->field)) { unset($this->data[$key]); @@ -938,9 +941,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public function allowField($field) { - if (true === $field) { - $field = $this->db(false)->getTableInfo('', 'fields'); - } $this->field = $field; return $this; } From bbfc33f334fb4a069b8ee6cbb4f1c06312054bec Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 20 Jan 2017 13:20:47 +0800 Subject: [PATCH 048/125] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../library/think/template/taglib/cxTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/thinkphp/library/think/template/taglib/cxTest.php b/tests/thinkphp/library/think/template/taglib/cxTest.php index c1c67413..8aee392f 100644 --- a/tests/thinkphp/library/think/template/taglib/cxTest.php +++ b/tests/thinkphp/library/think/template/taglib/cxTest.php @@ -47,7 +47,7 @@ EOF; {/volist} EOF; $data = <<\$vo): \$mod = (\$key % 2 );++\$key;?> +\$vo): \$mod = (\$key % 2 );++\$key;?> EOF; @@ -88,7 +88,7 @@ EOF; {/foreach} EOF; $data = <<\$val): ?> +\$val): ?> EOF; @@ -389,7 +389,7 @@ default {/empty} EOF; $data = <<isEmpty())): ?> +isEmpty())): ?> default EOF; @@ -402,7 +402,7 @@ default {/notempty} EOF; $data = <<isEmpty()))): ?> +isEmpty()))): ?> default EOF; @@ -538,13 +538,13 @@ EOF; public function testUrl() { $template = new template(); - $content = <<display($content); $this->expectOutputString(\think\Url::build('Index/index')); } - + public function testFunction() { $template = new template(); From 069d63a2fccf745b63ba41f3b243cd42d0704ab5 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 20 Jan 2017 13:28:57 +0800 Subject: [PATCH 049/125] =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/template/taglib/Cx.php | 2 +- tests/thinkphp/library/think/validateTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library/think/template/taglib/Cx.php b/library/think/template/taglib/Cx.php index 2a9c4ff4..af7a54c8 100644 --- a/library/think/template/taglib/Cx.php +++ b/library/think/template/taglib/Cx.php @@ -448,7 +448,7 @@ class Cx extends Taglib { $name = $tag['name']; $name = $this->autoBuildVar($name); - $parseStr = 'isEmpty()))): ?>' . $content . ''; + $parseStr = 'isEmpty()))): ?>' . $content . ''; return $parseStr; } diff --git a/tests/thinkphp/library/think/validateTest.php b/tests/thinkphp/library/think/validateTest.php index 4d7819b5..081a6f2b 100644 --- a/tests/thinkphp/library/think/validateTest.php +++ b/tests/thinkphp/library/think/validateTest.php @@ -88,7 +88,7 @@ class validateTest extends \PHPUnit_Framework_TestCase 'date' => '16-3-8', 'ok' => 'yes', 'value' => 100, - 'bool' => 'true', + 'bool' => true, 'title' => '流年ThinkPHP', 'city' => '上海', 'nickname' => '流年ThinkPHP_2016', From 249c5e62e475dc87ecfc7a943d3a921c2b4837e5 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 20 Jan 2017 20:27:55 +0800 Subject: [PATCH 050/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BparseOrder=E5=AF=B9ra?= =?UTF-8?q?nd=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Builder.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/library/think/db/Builder.php b/library/think/db/Builder.php index 3e7a60e9..b9683969 100644 --- a/library/think/db/Builder.php +++ b/library/think/db/Builder.php @@ -523,10 +523,12 @@ abstract class Builder $array = []; foreach ($order as $key => $val) { if (is_numeric($key)) { - if (false === strpos($val, '(')) { - $array[] = $this->parseKey($val, $options); - } elseif ('[rand]' == $val) { + if ('[rand]' == $val) { $array[] = $this->parseRand(); + } elseif (false === strpos($val, '(')) { + $array[] = $this->parseKey($val, $options); + } else { + $array[] = $val; } } else { $sort = in_array(strtolower(trim($val)), ['asc', 'desc']) ? ' ' . $val : ''; From 48b856a7f12a29e4f34cc2e0103b5cc60c261e58 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 20 Jan 2017 21:26:51 +0800 Subject: [PATCH 051/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BModel=E7=B1=BB?= =?UTF-8?q?=E7=9A=84delete=E6=96=B9=E6=B3=95=20=E6=94=AF=E6=8C=81=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E4=B8=BB=E9=94=AE=E6=83=85=E5=86=B5=E4=B8=8B=E7=9A=84?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/library/think/Model.php b/library/think/Model.php index cfd404ad..ebc84abd 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -991,8 +991,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return false; } + // 删除条件 + $pk = $this->getPk(); + if (isset($this->data[$pk])) { + $where = [$pk => $this->data[$pk]]; + } elseif (!empty($this->updateWhere)) { + $where = $this->updateWhere; + } else { + $where = null; + } + // 删除当前模型数据 - $result = $this->db()->delete($this->data); + $result = $this->db()->where($where)->delete(); // 关联删除 if (!empty($this->relationWrite)) { From 7e0282081d312b13120136ca4802068040bed71b Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 20 Jan 2017 21:36:52 +0800 Subject: [PATCH 052/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BModel=E7=B1=BB?= =?UTF-8?q?=E7=9A=84allowField=E6=96=B9=E6=B3=95=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E4=BC=A0=E5=85=A5=E9=80=97=E5=8F=B7=E5=88=86=E9=9A=94=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/library/think/Model.php b/library/think/Model.php index ebc84abd..eb01a3f6 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -936,11 +936,14 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 设置允许写入的字段 * @access public - * @param bool|array $field 允许写入的字段 如果为true只允许写入数据表字段 + * @param mixed $field 允许写入的字段 如果为true只允许写入数据表字段 * @return $this */ public function allowField($field) { + if (is_string($field)) { + $field = explode(',', $field); + } $this->field = $field; return $this; } From 776bf4805eb6bca3681ff221f3095224c2f90c8e Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 21 Jan 2017 12:36:50 +0800 Subject: [PATCH 053/125] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E9=9B=86=E5=AF=B9=E8=B1=A1=E7=9A=84=E8=BF=94=E5=9B=9E=20?= =?UTF-8?q?=E7=94=B1Query=E7=B1=BBselect=E6=96=B9=E6=B3=95=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E6=95=B0=E6=8D=AE=E9=9B=86=E5=AF=B9=E8=B1=A1=20Connec?= =?UTF-8?q?tion=E7=B1=BBgetResult=E6=96=B9=E6=B3=95=E4=B8=8D=E5=86=8D?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E6=95=B0=E6=8D=AE=E9=9B=86=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Connection.php | 6 ------ library/think/db/Query.php | 3 +++ 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index 4e18f812..9c66ddd1 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -13,7 +13,6 @@ namespace think\db; use PDO; use PDOStatement; -use think\Collection; use think\Db; use think\db\exception\BindParamException; use think\Debug; @@ -547,11 +546,6 @@ abstract class Connection } $result = $this->PDOStatement->fetchAll($this->fetchType); $this->numRows = count($result); - - if ('collection' == $this->resultSetType) { - // 返回数据集Collection对象 - $result = new Collection($result); - } return $result; } diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 348535e8..dc05ddaf 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -2312,6 +2312,9 @@ class Query } // 模型数据集转换 $resultSet = (new $model)->toCollection($resultSet); + } elseif ('collection' == $this->connection->getConfig('resultset_type')) { + // 返回Collection对象 + $resultSet = new Collection($resultSet); } // 返回结果处理 if (!empty($options['fail']) && count($resultSet) == 0) { From a13eb10116daa4ebfaa2133bd6757deb57a3634e Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 21 Jan 2017 14:20:26 +0800 Subject: [PATCH 054/125] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E5=86=99=E5=85=A5?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=9A=84=E8=87=AA=E5=8A=A8=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E7=BB=91=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Builder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/think/db/Builder.php b/library/think/db/Builder.php index b9683969..380fe239 100644 --- a/library/think/db/Builder.php +++ b/library/think/db/Builder.php @@ -112,8 +112,8 @@ abstract class Builder $result[$item] = $val; } else { $key = str_replace('.', '_', $key); - $this->query->bind($key, $val, isset($bind[$key]) ? $bind[$key] : PDO::PARAM_STR); - $result[$item] = ':' . $key; + $this->query->bind('__data__' . $key, $val, isset($bind[$key]) ? $bind[$key] : PDO::PARAM_STR); + $result[$item] = ':__data__' . $key; } } elseif (is_object($val) && method_exists($val, '__toString')) { // 对象数据写入 From 0944d0fe0e9f42260be4021cb3f3ef7a447601d3 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 21 Jan 2017 17:27:17 +0800 Subject: [PATCH 055/125] =?UTF-8?q?=E9=A2=84=E8=BD=BD=E5=85=A5=E5=85=B3?= =?UTF-8?q?=E8=81=94=E5=B1=9E=E6=80=A7=E7=9A=84=E9=BB=98=E8=AE=A4=E8=A7=84?= =?UTF-8?q?=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/BelongsTo.php | 5 +++-- library/think/model/relation/BelongsToMany.php | 3 ++- library/think/model/relation/HasMany.php | 5 +++-- library/think/model/relation/HasOne.php | 5 +++-- library/think/model/relation/MorphMany.php | 5 +++-- library/think/model/relation/MorphTo.php | 4 ++-- library/think/model/relation/OneToOne.php | 2 +- 7 files changed, 17 insertions(+), 12 deletions(-) diff --git a/library/think/model/relation/BelongsTo.php b/library/think/model/relation/BelongsTo.php index 319e62fb..8bf4cc4a 100644 --- a/library/think/model/relation/BelongsTo.php +++ b/library/think/model/relation/BelongsTo.php @@ -11,6 +11,7 @@ namespace think\model\relation; +use think\Loader; use think\Model; class BelongsTo extends OneToOne @@ -92,7 +93,7 @@ class BelongsTo extends OneToOne $this->bindAttr($relationModel, $result, $this->bindAttr); } // 设置关联属性 - $result->setAttr($relation, $relationModel); + $result->setAttr(Loader::parseName($relation), $relationModel); } } } @@ -122,7 +123,7 @@ class BelongsTo extends OneToOne $this->bindAttr($relationModel, $result, $this->bindAttr); } // 设置关联属性 - $result->setAttr($relation, $relationModel); + $result->setAttr(Loader::parseName($relation), $relationModel); } } diff --git a/library/think/model/relation/BelongsToMany.php b/library/think/model/relation/BelongsToMany.php index 2d6c353f..fba77a31 100644 --- a/library/think/model/relation/BelongsToMany.php +++ b/library/think/model/relation/BelongsToMany.php @@ -14,6 +14,7 @@ namespace think\model\relation; use think\Db; use think\db\Query; use think\Exception; +use think\Loader; use think\Model; use think\model\Pivot; use think\model\Relation; @@ -115,7 +116,7 @@ class BelongsToMany extends Relation $data[$result->$pk] = []; } - $result->setAttr($relation, $this->resultSetBuild($data[$result->$pk], $class)); + $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$pk], $class)); } } } diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index 2ddf370f..0b145f77 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -13,6 +13,7 @@ namespace think\model\relation; use think\Db; use think\db\Query; +use think\Loader; use think\Model; use think\model\Relation; @@ -84,7 +85,7 @@ class HasMany extends Relation if (!isset($data[$result->$localKey])) { $data[$result->$localKey] = []; } - $result->setAttr($relation, $this->resultSetBuild($data[$result->$localKey], $class)); + $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$localKey], $class)); } } } @@ -109,7 +110,7 @@ class HasMany extends Relation if (!isset($data[$result->$localKey])) { $data[$result->$localKey] = []; } - $result->setAttr($relation, $this->resultSetBuild($data[$result->$localKey], $class)); + $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$localKey], $class)); } } diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index b1fa25f0..23ffecd2 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -11,6 +11,7 @@ namespace think\model\relation; +use think\Loader; use think\Model; class HasOne extends OneToOne @@ -119,7 +120,7 @@ class HasOne extends OneToOne $this->bindAttr($relationModel, $result, $this->bindAttr); } // 设置关联属性 - $result->setAttr($relation, $relationModel); + $result->setAttr(Loader::parseName($relation), $relationModel); } } } @@ -148,7 +149,7 @@ class HasOne extends OneToOne // 绑定关联属性 $this->bindAttr($relationModel, $result, $this->bindAttr); } - $result->setAttr($relation, $relationModel); + $result->setAttr(Loader::parseName($relation), $relationModel); } } diff --git a/library/think/model/relation/MorphMany.php b/library/think/model/relation/MorphMany.php index 68eb0834..82b429ff 100644 --- a/library/think/model/relation/MorphMany.php +++ b/library/think/model/relation/MorphMany.php @@ -13,6 +13,7 @@ namespace think\model\relation; use think\Db; use think\db\Query; +use think\Loader; use think\Model; use think\model\Relation; @@ -94,7 +95,7 @@ class MorphMany extends Relation if (!isset($data[$result->$pk])) { $data[$result->$pk] = []; } - $result->setAttr($relation, $this->resultSetBuild($data[$result->$pk], $class)); + $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$pk], $class)); } } } @@ -114,7 +115,7 @@ class MorphMany extends Relation $pk = $result->getPk(); if (isset($result->$pk)) { $data = $this->eagerlyMorphToMany([$this->morphKey => $result->$pk, $this->morphType => $this->type], $relation, $subRelation, $closure); - $result->setAttr($relation, $this->resultSetBuild($data[$result->$pk], $class)); + $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$pk], $class)); } } diff --git a/library/think/model/relation/MorphTo.php b/library/think/model/relation/MorphTo.php index a4672233..d6a7a933 100644 --- a/library/think/model/relation/MorphTo.php +++ b/library/think/model/relation/MorphTo.php @@ -126,7 +126,7 @@ class MorphTo extends Relation if (!isset($data[$result->$morphKey])) { $data[$result->$morphKey] = []; } - $result->setAttr($relation, $this->resultSetBuild($data[$result->$morphKey], $class)); + $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$morphKey], $class)); } } } @@ -179,7 +179,7 @@ class MorphTo extends Relation if ($data) { $data->isUpdate(true); } - $result->setAttr($relation, $data ?: null); + $result->setAttr(Loader::parseName($relation), $data ?: null); } /** diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index c07cce05..fd11733d 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -228,7 +228,7 @@ abstract class OneToOne extends Relation $this->bindAttr($relationModel, $result, $this->bindAttr); } } - $result->setAttr($relation, !isset($relationModel) ? null : $relationModel->isUpdate(true)); + $result->setAttr(Loader::parseName($relation), !isset($relationModel) ? null : $relationModel->isUpdate(true)); } /** From a7a0051a7808bf29e73fedb41e1bdda39ac4d7fa Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 21 Jan 2017 23:01:31 +0800 Subject: [PATCH 056/125] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E5=85=B3=E8=81=94?= =?UTF-8?q?=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/BelongsTo.php | 5 +++-- library/think/model/relation/BelongsToMany.php | 7 ++++--- library/think/model/relation/HasMany.php | 5 +++-- library/think/model/relation/HasOne.php | 5 +++-- library/think/model/relation/MorphMany.php | 5 +++-- library/think/model/relation/MorphTo.php | 4 +++- 6 files changed, 19 insertions(+), 12 deletions(-) diff --git a/library/think/model/relation/BelongsTo.php b/library/think/model/relation/BelongsTo.php index 8bf4cc4a..85b04b7a 100644 --- a/library/think/model/relation/BelongsTo.php +++ b/library/think/model/relation/BelongsTo.php @@ -81,7 +81,8 @@ class BelongsTo extends OneToOne $range, ], ], $localKey, $relation, $subRelation, $closure); - + // 关联属性名 + $attr = Loader::parseName($relation); // 关联数据封装 foreach ($resultSet as $result) { if (!isset($data[$result->$foreignKey])) { @@ -93,7 +94,7 @@ class BelongsTo extends OneToOne $this->bindAttr($relationModel, $result, $this->bindAttr); } // 设置关联属性 - $result->setAttr(Loader::parseName($relation), $relationModel); + $result->setAttr($attr, $relationModel); } } } diff --git a/library/think/model/relation/BelongsToMany.php b/library/think/model/relation/BelongsToMany.php index fba77a31..03992470 100644 --- a/library/think/model/relation/BelongsToMany.php +++ b/library/think/model/relation/BelongsToMany.php @@ -109,14 +109,15 @@ class BelongsToMany extends Relation $range, ], ], $relation, $subRelation); - + // 关联属性名 + $attr = Loader::parseName($relation); // 关联数据封装 foreach ($resultSet as $result) { if (!isset($data[$result->$pk])) { $data[$result->$pk] = []; } - $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$pk], $class)); + $result->setAttr($attr, $this->resultSetBuild($data[$result->$pk], $class)); } } } @@ -143,7 +144,7 @@ class BelongsToMany extends Relation if (!isset($data[$pk])) { $data[$pk] = []; } - $result->setAttr($relation, $this->resultSetBuild($data[$pk], $class)); + $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$pk], $class)); } } diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index 0b145f77..49ea2e78 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -79,13 +79,14 @@ class HasMany extends Relation $range, ], ], $relation, $subRelation, $closure); - + // 关联属性名 + $attr = Loader::parseName($relation); // 关联数据封装 foreach ($resultSet as $result) { if (!isset($data[$result->$localKey])) { $data[$result->$localKey] = []; } - $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$localKey], $class)); + $result->setAttr($attr, $this->resultSetBuild($data[$result->$localKey], $class)); } } } diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index 23ffecd2..5c7ecd23 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -108,7 +108,8 @@ class HasOne extends OneToOne $range, ], ], $foreignKey, $relation, $subRelation, $closure); - + // 关联属性名 + $attr = Loader::parseName($relation); // 关联数据封装 foreach ($resultSet as $result) { if (!isset($data[$result->$localKey])) { @@ -120,7 +121,7 @@ class HasOne extends OneToOne $this->bindAttr($relationModel, $result, $this->bindAttr); } // 设置关联属性 - $result->setAttr(Loader::parseName($relation), $relationModel); + $result->setAttr($attr, $relationModel); } } } diff --git a/library/think/model/relation/MorphMany.php b/library/think/model/relation/MorphMany.php index 82b429ff..71719c01 100644 --- a/library/think/model/relation/MorphMany.php +++ b/library/think/model/relation/MorphMany.php @@ -89,13 +89,14 @@ class MorphMany extends Relation $morphKey => ['in', $range], $morphType => $type, ], $relation, $subRelation, $closure); - + // 关联属性名 + $attr = Loader::parseName($relation); // 关联数据封装 foreach ($resultSet as $result) { if (!isset($data[$result->$pk])) { $data[$result->$pk] = []; } - $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$pk], $class)); + $result->setAttr($attr, $this->resultSetBuild($data[$result->$pk], $class)); } } } diff --git a/library/think/model/relation/MorphTo.php b/library/think/model/relation/MorphTo.php index d6a7a933..8051862d 100644 --- a/library/think/model/relation/MorphTo.php +++ b/library/think/model/relation/MorphTo.php @@ -111,6 +111,8 @@ class MorphTo extends Relation } if (!empty($range)) { + // 关联属性名 + $attr = Loader::parseName($relation); foreach ($range as $key => $val) { // 多态类型映射 $model = $this->parseModel($key); @@ -126,7 +128,7 @@ class MorphTo extends Relation if (!isset($data[$result->$morphKey])) { $data[$result->$morphKey] = []; } - $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$morphKey], $class)); + $result->setAttr($attr, $this->resultSetBuild($data[$result->$morphKey], $class)); } } } From 6f02e4b1276c291fec0198332b7809b858ff7d53 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 22 Jan 2017 12:42:24 +0800 Subject: [PATCH 057/125] =?UTF-8?q?=E5=85=B3=E8=81=94=E9=A2=84=E8=BD=BD?= =?UTF-8?q?=E5=85=A5=E6=96=B9=E6=B3=95=E8=B0=83=E6=95=B4=20=E5=8F=96?= =?UTF-8?q?=E6=B6=88class=E5=8F=82=E6=95=B0=20=E5=85=B3=E8=81=94=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E9=9B=86=E6=9F=A5=E8=AF=A2=E6=A0=B9=E6=8D=AE?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E7=9A=84=E6=95=B0=E6=8D=AE=E9=9B=86=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E7=B1=BB=E5=9E=8B=E8=AE=BE=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lang/zh-cn.php | 1 + library/think/Model.php | 13 +++++---- library/think/db/Connection.php | 4 +-- library/think/db/Query.php | 22 ++++++++------- library/think/model/Relation.php | 7 ++--- library/think/model/relation/BelongsTo.php | 28 ++++++++++--------- .../think/model/relation/BelongsToMany.php | 10 +++---- library/think/model/relation/HasMany.php | 15 ++++------ library/think/model/relation/HasOne.php | 25 +++++++++-------- library/think/model/relation/MorphMany.php | 14 ++++------ library/think/model/relation/MorphTo.php | 13 +++++---- library/think/model/relation/OneToOne.php | 10 +++---- 12 files changed, 79 insertions(+), 83 deletions(-) diff --git a/lang/zh-cn.php b/lang/zh-cn.php index 6d33b0ce..b837bc41 100644 --- a/lang/zh-cn.php +++ b/lang/zh-cn.php @@ -63,4 +63,5 @@ return [ 'route name not exists' => '路由标识不存在(或参数不够)', 'invalid request' => '非法请求', 'bind attr has exists' => '模型的属性已经存在', + 'relation data not exists' => '关联数据不存在', ]; diff --git a/library/think/Model.php b/library/think/Model.php index eb01a3f6..8cf92f73 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -155,6 +155,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $this->dateFormat = $this->db(false)->getConfig('datetime_format'); } + if (is_null($this->resultSetType)) { + $this->resultSetType = $this->db(false)->getConfig('resultset_type'); + } // 执行初始化操作 $this->initialize(); } @@ -1404,10 +1407,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @access public * @param array $resultSet 数据集 * @param string $relation 关联名 - * @param string $class 数据集对象名 为空表示数组 * @return array */ - public function eagerlyResultSet(&$resultSet, $relation, $class = '') + public function eagerlyResultSet(&$resultSet, $relation) { $relations = is_string($relation) ? explode(',', $relation) : $relation; foreach ($relations as $key => $relation) { @@ -1421,7 +1423,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess list($relation, $subRelation) = explode('.', $relation, 2); } $relation = Loader::parseName($relation, 1, false); - $this->$relation()->eagerlyResultSet($resultSet, $relation, $subRelation, $closure, $class); + $this->$relation()->eagerlyResultSet($resultSet, $relation, $subRelation, $closure); } } @@ -1430,10 +1432,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @access public * @param Model $result 数据对象 * @param string $relation 关联名 - * @param string $class 数据集对象名 为空表示数组 * @return Model */ - public function eagerlyResult(&$result, $relation, $class = '') + public function eagerlyResult(&$result, $relation) { $relations = is_string($relation) ? explode(',', $relation) : $relation; @@ -1448,7 +1449,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess list($relation, $subRelation) = explode('.', $relation, 2); } $relation = Loader::parseName($relation, 1, false); - $this->$relation()->eagerlyResult($result, $relation, $subRelation, $closure, $class); + $this->$relation()->eagerlyResult($result, $relation, $subRelation, $closure); } } diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index 9c66ddd1..6198f924 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -528,11 +528,11 @@ abstract class Connection } /** - * 获得数据集 + * 获得数据集数组 * @access protected * @param bool $pdo 是否返回PDOStatement * @param bool $procedure 是否存储过程 - * @return mixed + * @return array */ protected function getResult($pdo = false, $procedure = false) { diff --git a/library/think/db/Query.php b/library/think/db/Query.php index dc05ddaf..b7288c93 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -2289,29 +2289,31 @@ class Query // 数据列表读取后的处理 if (!empty($this->model)) { // 生成模型对象 - $model = $this->model; + $modelName = $this->model; if (count($resultSet) > 0) { foreach ($resultSet as $key => $result) { /** @var Model $result */ - $result = new $model($result); - $result->isUpdate(true); + $model = new $modelName($result); + $model->isUpdate(true); // 关联查询 if (!empty($options['relation'])) { - $result->relationQuery($options['relation']); + $model->relationQuery($options['relation']); } // 关联统计 if (!empty($options['with_count'])) { - $result->relationCount($result, $options['with_count']); + $model->relationCount($model, $options['with_count']); } - $resultSet[$key] = $result; + $resultSet[$key] = $model; } if (!empty($options['with'])) { // 预载入 - $result->eagerlyResultSet($resultSet, $options['with'], is_object($resultSet) ? get_class($resultSet) : ''); + $model->eagerlyResultSet($resultSet, $options['with']); } + // 模型数据集转换 + $resultSet = $model->toCollection($resultSet); + } else { + $resultSet = (new $modelName)->toCollection($resultSet); } - // 模型数据集转换 - $resultSet = (new $model)->toCollection($resultSet); } elseif ('collection' == $this->connection->getConfig('resultset_type')) { // 返回Collection对象 $resultSet = new Collection($resultSet); @@ -2406,7 +2408,7 @@ class Query } // 预载入查询 if (!empty($options['with'])) { - $data->eagerlyResult($data, $options['with'], is_object($result) ? get_class($result) : ''); + $data->eagerlyResult($data, $options['with']); } // 关联统计 if (!empty($options['with_count'])) { diff --git a/library/think/model/Relation.php b/library/think/model/Relation.php index b16d08e4..5c2db75e 100644 --- a/library/think/model/Relation.php +++ b/library/think/model/Relation.php @@ -27,8 +27,6 @@ abstract class Relation protected $foreignKey; // 关联表主键 protected $localKey; - // 关联查询条件 - protected $where; // 关联查询参数 protected $option; // 基础查询 @@ -68,12 +66,11 @@ abstract class Relation * 封装关联数据集 * @access public * @param array $resultSet 数据集 - * @param string $class 数据集类名 * @return mixed */ - protected function resultSetBuild($resultSet, $class = '') + protected function resultSetBuild($resultSet) { - return $class ? new $class($resultSet) : $resultSet; + return (new $this->model)->toCollection($resultSet); } /** diff --git a/library/think/model/relation/BelongsTo.php b/library/think/model/relation/BelongsTo.php index 85b04b7a..1a3cad6f 100644 --- a/library/think/model/relation/BelongsTo.php +++ b/library/think/model/relation/BelongsTo.php @@ -11,6 +11,7 @@ namespace think\model\relation; +use think\Exception; use think\Loader; use think\Model; @@ -57,10 +58,9 @@ class BelongsTo extends OneToOne * @param string $relation 当前关联名 * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 * @return void */ - protected function eagerlySet(&$resultSet, $relation, $subRelation, $closure, $class) + protected function eagerlySet(&$resultSet, $relation, $subRelation, $closure) { $localKey = $this->localKey; $foreignKey = $this->foreignKey; @@ -74,8 +74,7 @@ class BelongsTo extends OneToOne } if (!empty($range)) { - $this->where[$localKey] = ['in', $range]; - $data = $this->eagerlyWhere($this, [ + $data = $this->eagerlyWhere($this, [ $localKey => [ 'in', $range, @@ -85,10 +84,13 @@ class BelongsTo extends OneToOne $attr = Loader::parseName($relation); // 关联数据封装 foreach ($resultSet as $result) { - if (!isset($data[$result->$foreignKey])) { - $data[$result->$foreignKey] = []; + // 关联模型 + if (!isset($data[$result->$localKey])) { + throw new Exception('relation data not exists :' . $this->model); + } else { + $relationModel = $data[$result->$localKey]; } - $relationModel = $this->resultSetBuild($data[$result->$foreignKey], $class); + if (!empty($this->bindAttr)) { // 绑定关联属性 $this->bindAttr($relationModel, $result, $this->bindAttr); @@ -106,19 +108,19 @@ class BelongsTo extends OneToOne * @param string $relation 当前关联名 * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 * @return void */ - protected function eagerlyOne(&$result, $relation, $subRelation, $closure, $class) + protected function eagerlyOne(&$result, $relation, $subRelation, $closure) { $localKey = $this->localKey; $foreignKey = $this->foreignKey; $data = $this->eagerlyWhere($this, [$localKey => $result->$foreignKey], $localKey, $relation, $subRelation, $closure); - // 关联数据封装 - if (!isset($data[$result->$foreignKey])) { - $data[$result->$foreignKey] = []; + // 关联模型 + if (!isset($data[$result->$localKey])) { + throw new Exception('relation data not exists :' . $this->model); + } else { + $relationModel = $data[$result->$localKey]; } - $relationModel = $this->resultSetBuild($data[$result->$foreignKey], $class); if (!empty($this->bindAttr)) { // 绑定关联属性 $this->bindAttr($relationModel, $result, $this->bindAttr); diff --git a/library/think/model/relation/BelongsToMany.php b/library/think/model/relation/BelongsToMany.php index 03992470..9c6337dc 100644 --- a/library/think/model/relation/BelongsToMany.php +++ b/library/think/model/relation/BelongsToMany.php @@ -84,10 +84,9 @@ class BelongsToMany extends Relation * @param string $relation 当前关联名 * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 * @return void */ - public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class) + public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure) { $localKey = $this->localKey; $foreignKey = $this->foreignKey; @@ -117,7 +116,7 @@ class BelongsToMany extends Relation $data[$result->$pk] = []; } - $result->setAttr($attr, $this->resultSetBuild($data[$result->$pk], $class)); + $result->setAttr($attr, $this->resultSetBuild($data[$result->$pk])); } } } @@ -129,10 +128,9 @@ class BelongsToMany extends Relation * @param string $relation 当前关联名 * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 * @return void */ - public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class) + public function eagerlyResult(&$result, $relation, $subRelation, $closure) { $pk = $result->getPk(); if (isset($result->$pk)) { @@ -144,7 +142,7 @@ class BelongsToMany extends Relation if (!isset($data[$pk])) { $data[$pk] = []; } - $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$pk], $class)); + $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$pk])); } } diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index 49ea2e78..32cf1b76 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -60,7 +60,7 @@ class HasMany extends Relation * @param string $class 数据集对象名 为空表示数组 * @return void */ - public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class) + public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure) { $localKey = $this->localKey; $range = []; @@ -72,8 +72,7 @@ class HasMany extends Relation } if (!empty($range)) { - $this->where[$this->foreignKey] = ['in', $range]; - $data = $this->eagerlyOneToMany($this, [ + $data = $this->eagerlyOneToMany($this, [ $this->foreignKey => [ 'in', $range, @@ -86,7 +85,7 @@ class HasMany extends Relation if (!isset($data[$result->$localKey])) { $data[$result->$localKey] = []; } - $result->setAttr($attr, $this->resultSetBuild($data[$result->$localKey], $class)); + $result->setAttr($attr, $this->resultSetBuild($data[$result->$localKey])); } } } @@ -101,7 +100,7 @@ class HasMany extends Relation * @param string $class 数据集对象名 为空表示数组 * @return void */ - public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class) + public function eagerlyResult(&$result, $relation, $subRelation, $closure) { $localKey = $this->localKey; @@ -111,7 +110,7 @@ class HasMany extends Relation if (!isset($data[$result->$localKey])) { $data[$result->$localKey] = []; } - $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$localKey], $class)); + $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$localKey])); } } @@ -259,9 +258,7 @@ class HasMany extends Relation protected function baseQuery() { if (empty($this->baseQuery)) { - if (isset($this->where)) { - $this->query->where($this->where); - } elseif (isset($this->parent->{$this->localKey})) { + if (isset($this->parent->{$this->localKey})) { // 关联查询带入关联条件 $this->query->where($this->foreignKey, $this->parent->{$this->localKey}); } diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index 5c7ecd23..751f53d5 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -11,6 +11,7 @@ namespace think\model\relation; +use think\Exception; use think\Loader; use think\Model; @@ -84,10 +85,9 @@ class HasOne extends OneToOne * @param string $relation 当前关联名 * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 * @return void */ - protected function eagerlySet(&$resultSet, $relation, $subRelation, $closure, $class) + protected function eagerlySet(&$resultSet, $relation, $subRelation, $closure) { $localKey = $this->localKey; $foreignKey = $this->foreignKey; @@ -101,8 +101,7 @@ class HasOne extends OneToOne } if (!empty($range)) { - $this->where[$foreignKey] = ['in', $range]; - $data = $this->eagerlyWhere($this, [ + $data = $this->eagerlyWhere($this, [ $foreignKey => [ 'in', $range, @@ -112,10 +111,12 @@ class HasOne extends OneToOne $attr = Loader::parseName($relation); // 关联数据封装 foreach ($resultSet as $result) { + // 关联模型 if (!isset($data[$result->$localKey])) { - $data[$result->$localKey] = []; + throw new Exception('relation data not exists : ' . $this->model); + } else { + $relationModel = $data[$result->$localKey]; } - $relationModel = $this->resultSetBuild($data[$result->$localKey], $class); if (!empty($this->bindAttr)) { // 绑定关联属性 $this->bindAttr($relationModel, $result, $this->bindAttr); @@ -133,19 +134,21 @@ class HasOne extends OneToOne * @param string $relation 当前关联名 * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 * @return void */ - protected function eagerlyOne(&$result, $relation, $subRelation, $closure, $class) + protected function eagerlyOne(&$result, $relation, $subRelation, $closure) { $localKey = $this->localKey; $foreignKey = $this->foreignKey; $data = $this->eagerlyWhere($this, [$foreignKey => $result->$localKey], $foreignKey, $relation, $subRelation, $closure); - // 关联数据封装 + + // 关联模型 if (!isset($data[$result->$localKey])) { - $data[$result->$localKey] = []; + throw new Exception('relation data not exists :' . $this->model); + } else { + $relationModel = $data[$result->$localKey]; } - $relationModel = $this->resultSetBuild($data[$result->$localKey], $class); + if (!empty($this->bindAttr)) { // 绑定关联属性 $this->bindAttr($relationModel, $result, $this->bindAttr); diff --git a/library/think/model/relation/MorphMany.php b/library/think/model/relation/MorphMany.php index 71719c01..e5ed1701 100644 --- a/library/think/model/relation/MorphMany.php +++ b/library/think/model/relation/MorphMany.php @@ -65,10 +65,9 @@ class MorphMany extends Relation * @param string $relation 当前关联名 * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 * @return void */ - public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class) + public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure) { $morphType = $this->morphType; $morphKey = $this->morphKey; @@ -83,9 +82,7 @@ class MorphMany extends Relation } if (!empty($range)) { - $this->where[$morphKey] = ['in', $range]; - $this->where[$morphType] = $type; - $data = $this->eagerlyMorphToMany([ + $data = $this->eagerlyMorphToMany([ $morphKey => ['in', $range], $morphType => $type, ], $relation, $subRelation, $closure); @@ -96,7 +93,7 @@ class MorphMany extends Relation if (!isset($data[$result->$pk])) { $data[$result->$pk] = []; } - $result->setAttr($attr, $this->resultSetBuild($data[$result->$pk], $class)); + $result->setAttr($attr, $this->resultSetBuild($data[$result->$pk])); } } } @@ -108,15 +105,14 @@ class MorphMany extends Relation * @param string $relation 当前关联名 * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 * @return void */ - public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class) + public function eagerlyResult(&$result, $relation, $subRelation, $closure) { $pk = $result->getPk(); if (isset($result->$pk)) { $data = $this->eagerlyMorphToMany([$this->morphKey => $result->$pk, $this->morphType => $this->type], $relation, $subRelation, $closure); - $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$pk], $class)); + $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$pk])); } } diff --git a/library/think/model/relation/MorphTo.php b/library/think/model/relation/MorphTo.php index 8051862d..ffad707b 100644 --- a/library/think/model/relation/MorphTo.php +++ b/library/think/model/relation/MorphTo.php @@ -11,6 +11,7 @@ namespace think\model\relation; +use think\Exception; use think\Loader; use think\Model; use think\model\Relation; @@ -95,10 +96,9 @@ class MorphTo extends Relation * @param string $relation 当前关联名 * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 * @return void */ - public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class) + public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure) { $morphKey = $this->morphKey; $morphType = $this->morphType; @@ -125,10 +125,12 @@ class MorphTo extends Relation } foreach ($resultSet as $result) { if ($key == $result->$morphType) { + // 关联模型 if (!isset($data[$result->$morphKey])) { - $data[$result->$morphKey] = []; + throw new Exception('relation data not exists :' . $this->model); + } else { + $result->setAttr($attr, $data[$result->$morphKey]); } - $result->setAttr($attr, $this->resultSetBuild($data[$result->$morphKey], $class)); } } } @@ -142,10 +144,9 @@ class MorphTo extends Relation * @param string $relation 当前关联名 * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 * @return void */ - public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class) + public function eagerlyResult(&$result, $relation, $subRelation, $closure) { $morphKey = $this->morphKey; $morphType = $this->morphType; diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index fd11733d..0ec28987 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -100,14 +100,13 @@ abstract class OneToOne extends Relation * @param string $relation 当前关联名 * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 * @return void */ - public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class) + public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure) { if (1 == $this->eagerlyType) { // IN查询 - $this->eagerlySet($resultSet, $relation, $subRelation, $closure, $class); + $this->eagerlySet($resultSet, $relation, $subRelation, $closure); } else { // 模型关联组装 foreach ($resultSet as $result) { @@ -123,14 +122,13 @@ abstract class OneToOne extends Relation * @param string $relation 当前关联名 * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 * @return void */ - public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class) + public function eagerlyResult(&$result, $relation, $subRelation, $closure) { if (1 == $this->eagerlyType) { // IN查询 - $this->eagerlyOne($result, $relation, $subRelation, $closure, $class); + $this->eagerlyOne($result, $relation, $subRelation, $closure); } else { // 模型关联组装 $this->match($this->model, $relation, $result); From 1b17858887d519cbb01c17d02b14d7aa14c16e3e Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 22 Jan 2017 13:45:54 +0800 Subject: [PATCH 058/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BModel=E7=B1=BBtoColle?= =?UTF-8?q?ction=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/think/Model.php b/library/think/Model.php index 8cf92f73..4a82c5c6 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -654,7 +654,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess if ($this->resultSetType) { if ('collection' == $this->resultSetType) { $collection = new Collection($collection); - } else { + } elseif (false !== strpos($this->resultSetType, '\\')) { $class = $this->resultSetType; $collection = new $class($collection); } From b1fa683e3d9cbb4efd6da5ab0bf5eaa35c88403a Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 22 Jan 2017 17:13:41 +0800 Subject: [PATCH 059/125] =?UTF-8?q?Query=E7=B1=BB=E5=A2=9E=E5=8A=A0hidden?= =?UTF-8?q?=E5=92=8Cvisible=E6=96=B9=E6=B3=95=20=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E8=BE=93=E5=87=BA=E7=9A=84=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E9=9A=90=E8=97=8F=E5=92=8C=E6=98=BE=E7=A4=BA=20with=E5=92=8Cre?= =?UTF-8?q?lation=E6=96=B9=E6=B3=95=E6=94=AF=E6=8C=81=E5=A4=9A=E6=AC=A1?= =?UTF-8?q?=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 59 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index b7288c93..d86834d6 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1503,6 +1503,30 @@ class Query return $this; } + /** + * 设置模型输出显示字段 + * @access public + * @param mixed $visible + * @return $this + */ + public function visible($visible) + { + $this->options['visible'] = $visible; + return $this; + } + + /** + * 设置模型输出隐藏字段 + * @access public + * @param mixed $hidden + * @return $this + */ + public function hidden($hidden) + { + $this->options['hidden'] = $hidden; + return $this; + } + /** * 指定数据表别名 * @access public @@ -1911,7 +1935,11 @@ class Query } } $this->via(); - $this->options['with'] = $with; + if (isset($this->options['with'])) { + $this->options['with'] = array_merge($this->options['with'], $with); + } else { + $this->options['with'] = $with; + } return $this; } @@ -1976,12 +2004,22 @@ class Query /** * 设置关联查询 * @access public - * @param string $relation 关联名称 + * @param string|array $relation 关联名称 * @return $this */ public function relation($relation) { - $this->options['relation'] = $relation; + if (empty($relation)) { + return $this; + } + if (is_string($relation)) { + $relation = explode(',', $relation); + } + if (isset($this->options['relation'])) { + $this->options['relation'] = array_mrege($this->options['relation'], $relation); + } else { + $this->options['relation'] = $relation; + } return $this; } @@ -2295,6 +2333,7 @@ class Query /** @var Model $result */ $model = new $modelName($result); $model->isUpdate(true); + // 关联查询 if (!empty($options['relation'])) { $model->relationQuery($options['relation']); @@ -2303,6 +2342,14 @@ class Query if (!empty($options['with_count'])) { $model->relationCount($model, $options['with_count']); } + + // 显式或隐藏属性 + if (!empty($options['visible'])) { + $model->visible($options['visible']); + } elseif (!empty($options['hidden'])) { + $model->hidden($options['hidden']); + } + $resultSet[$key] = $model; } if (!empty($options['with'])) { @@ -2414,6 +2461,12 @@ class Query if (!empty($options['with_count'])) { $data->relationCount($data, $options['with_count']); } + // 显式或隐藏属性 + if (!empty($options['visible'])) { + $data->visible($options['visible']); + } elseif (!empty($options['hidden'])) { + $data->hidden($options['hidden']); + } } } elseif (!empty($options['fail'])) { $this->throwNotFound($options); From e2e217f1d693d39403bb68b96736e34957ff7677 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 22 Jan 2017 17:36:56 +0800 Subject: [PATCH 060/125] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E4=B8=80=E5=AF=B9?= =?UTF-8?q?=E4=B8=80=E5=85=B3=E8=81=94=E9=A2=84=E8=BD=BD=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/BelongsTo.php | 9 ++++----- library/think/model/relation/HasOne.php | 9 ++++----- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/library/think/model/relation/BelongsTo.php b/library/think/model/relation/BelongsTo.php index 1a3cad6f..2f067c30 100644 --- a/library/think/model/relation/BelongsTo.php +++ b/library/think/model/relation/BelongsTo.php @@ -11,7 +11,6 @@ namespace think\model\relation; -use think\Exception; use think\Loader; use think\Model; @@ -86,12 +85,12 @@ class BelongsTo extends OneToOne foreach ($resultSet as $result) { // 关联模型 if (!isset($data[$result->$localKey])) { - throw new Exception('relation data not exists :' . $this->model); + $relationModel = null; } else { $relationModel = $data[$result->$localKey]; } - if (!empty($this->bindAttr)) { + if ($relationModel && !empty($this->bindAttr)) { // 绑定关联属性 $this->bindAttr($relationModel, $result, $this->bindAttr); } @@ -117,11 +116,11 @@ class BelongsTo extends OneToOne $data = $this->eagerlyWhere($this, [$localKey => $result->$foreignKey], $localKey, $relation, $subRelation, $closure); // 关联模型 if (!isset($data[$result->$localKey])) { - throw new Exception('relation data not exists :' . $this->model); + $relationModel = null; } else { $relationModel = $data[$result->$localKey]; } - if (!empty($this->bindAttr)) { + if ($relationModel && !empty($this->bindAttr)) { // 绑定关联属性 $this->bindAttr($relationModel, $result, $this->bindAttr); } diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index 751f53d5..9882667e 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -11,7 +11,6 @@ namespace think\model\relation; -use think\Exception; use think\Loader; use think\Model; @@ -113,11 +112,11 @@ class HasOne extends OneToOne foreach ($resultSet as $result) { // 关联模型 if (!isset($data[$result->$localKey])) { - throw new Exception('relation data not exists : ' . $this->model); + $relationModel = null; } else { $relationModel = $data[$result->$localKey]; } - if (!empty($this->bindAttr)) { + if ($relationModel && !empty($this->bindAttr)) { // 绑定关联属性 $this->bindAttr($relationModel, $result, $this->bindAttr); } @@ -144,12 +143,12 @@ class HasOne extends OneToOne // 关联模型 if (!isset($data[$result->$localKey])) { - throw new Exception('relation data not exists :' . $this->model); + $relationModel = null; } else { $relationModel = $data[$result->$localKey]; } - if (!empty($this->bindAttr)) { + if ($relationModel && !empty($this->bindAttr)) { // 绑定关联属性 $this->bindAttr($relationModel, $result, $this->bindAttr); } From d96ec0c3500782087464c55c0baa5a90b704ab63 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 22 Jan 2017 18:04:36 +0800 Subject: [PATCH 061/125] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E5=A3=B0=E6=98=8E=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/OneToOne.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index 0ec28987..c89214c4 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -17,6 +17,13 @@ use think\Loader; use think\Model; use think\model\Relation; +/** + * Class OneToOne + * @package think\model\relation + * @method void eagerlySete($resultSet, $relation, $subRelation, $closure) 预载入关联查询(数据集) + * @method void eagerlyOne($result, $relation, $subRelation, $closure) 预载入关联查询(数据) + * + */ abstract class OneToOne extends Relation { // 预载入方式 0 -JOIN 1 -IN From d7dda49cbf0c7bcce6f268413c9fe19d613e6ff2 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 22 Jan 2017 18:07:13 +0800 Subject: [PATCH 062/125] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/OneToOne.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index c89214c4..ad5982d7 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -20,8 +20,8 @@ use think\model\Relation; /** * Class OneToOne * @package think\model\relation - * @method void eagerlySete($resultSet, $relation, $subRelation, $closure) 预载入关联查询(数据集) - * @method void eagerlyOne($result, $relation, $subRelation, $closure) 预载入关联查询(数据) + * @method void eagerlySet(array $resultSet, string $relation, string $subRelation, \Closure $closure) 预载入关联查询(数据集) + * @method void eagerlyOne(Model $result, string $relation, string $subRelation, \Closure $closure) 预载入关联查询(数据) * */ abstract class OneToOne extends Relation From 44654c76ef286f097a943830cc73f61415d1b690 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 22 Jan 2017 20:46:34 +0800 Subject: [PATCH 063/125] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E8=BD=AF=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=9A=84delete(true)=E7=94=A8=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/traits/model/SoftDelete.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/traits/model/SoftDelete.php b/library/traits/model/SoftDelete.php index 1a69c2c5..8781544f 100644 --- a/library/traits/model/SoftDelete.php +++ b/library/traits/model/SoftDelete.php @@ -63,7 +63,7 @@ trait SoftDelete $this->data[$name] = $this->autoWriteTimestamp($name); $result = $this->isUpdate()->save(); } else { - $result = $this->db()->delete($this->data); + $result = $this->db(false)->delete($this->data); } $this->trigger('after_delete', $this); From 7348729b4065d31956de317d1109a799d1694d8c Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 22 Jan 2017 23:21:46 +0800 Subject: [PATCH 064/125] =?UTF-8?q?Query=E7=B1=BB=E7=9A=84hidden=E5=92=8Cv?= =?UTF-8?q?isible=E6=96=B9=E6=B3=95=E5=8F=96=E6=B6=88=20=E6=94=B9=E8=BF=9B?= =?UTF-8?q?Model=E7=B1=BB=E7=9A=84hidden=E5=92=8Cvisible=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=93=8D=E4=BD=9C=E5=85=B3=E8=81=94=E5=B1=9E?= =?UTF-8?q?=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Collection.php | 48 +++++++++++++++++++++++++++--- library/think/Model.php | 38 +++++++++++++++++++---- library/think/db/Query.php | 38 ----------------------- library/think/model/Collection.php | 1 + 4 files changed, 78 insertions(+), 47 deletions(-) diff --git a/library/think/Collection.php b/library/think/Collection.php index c23395a9..b450c48c 100644 --- a/library/think/Collection.php +++ b/library/think/Collection.php @@ -20,6 +20,10 @@ use JsonSerializable; class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable { protected $items = []; + // 显示属性 + protected $visible = []; + // 隐藏属性 + protected $hidden = []; public function __construct($items = []) { @@ -40,11 +44,47 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria return empty($this->items); } + /** + * 设置需要隐藏的输出属性 + * @access public + * @param array $hidden 属性列表 + * @param bool $override 是否覆盖 + * @return $this + */ + public function hidden($hidden = [], $override = false) + { + $this->hidden = [$hidden, $override]; + return $this; + } + + /** + * 设置需要输出的属性 + * @param array $visible + * @param bool $override 是否覆盖 + * @return $this + */ + public function visible($visible = [], $override = false) + { + $this->visible = [$visible, $override]; + return $this; + } + public function toArray() { - return array_map(function ($value) { - return ($value instanceof Model || $value instanceof self) ? $value->toArray() : $value; - }, $this->items); + $result = []; + foreach ($this->items as $key => $item) { + if ($item instanceof Model || $item instanceof self) { + if (!empty($this->visible)) { + $item->visible($this->visible[0], $this->visible[1]); + } elseif (!empty($this->hidden)) { + $item->hidden($this->hidden[0], $this->hidden[1]); + } + $result[$key] = $item->toArray(); + } else { + $result[$key] = $item; + } + } + return $result; } public function all() @@ -225,7 +265,7 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria $result = []; foreach ($this->items as $row) { - $key = $value = null; + $key = $value = null; $keySet = $valueSet = false; if (null !== $index_key && array_key_exists($index_key, $row)) { $keySet = true; diff --git a/library/think/Model.php b/library/think/Model.php index 4a82c5c6..772e6f12 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -596,13 +596,31 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public function toArray() { - $item = []; - - //过滤属性 + $item = []; + $visible = []; + $hidden = []; + // 过滤属性 if (!empty($this->visible)) { - $data = array_intersect_key($this->data, array_flip($this->visible)); + $array = []; + foreach ($this->visible as $key => $val) { + if (is_array($val)) { + $array[] = $key; + $visible[$key] = $val; + } else { + $array[] = $val; + } + } + $data = array_intersect_key($this->data, array_flip($array)); } elseif (!empty($this->hidden)) { - $data = array_diff_key($this->data, array_flip($this->hidden)); + $array = []; + foreach ($this->hidden as $key => $val) { + if (is_array($val)) { + $hidden[$key] = $val; + } else { + $array[] = $val; + } + } + $data = array_diff_key($this->data, array_flip($array)); } else { $data = $this->data; } @@ -610,11 +628,21 @@ abstract class Model implements \JsonSerializable, \ArrayAccess foreach ($data as $key => $val) { if ($val instanceof Model || $val instanceof Collection) { // 关联模型对象 + if (isset($visible[$key])) { + $val->visible($visible[$key]); + } elseif (isset($hidden[$key])) { + $val->hidden($hidden[$key]); + } $item[$key] = $val->toArray(); } elseif (is_array($val) && reset($val) instanceof Model) { // 关联模型数据集 $arr = []; foreach ($val as $k => $value) { + if (isset($visible[$k])) { + $value->visible($visible[$k]); + } elseif (isset($hidden[$k])) { + $value->hidden($hidden[$k]); + } $arr[$k] = $value->toArray(); } $item[$key] = $arr; diff --git a/library/think/db/Query.php b/library/think/db/Query.php index d86834d6..45cdd432 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1503,30 +1503,6 @@ class Query return $this; } - /** - * 设置模型输出显示字段 - * @access public - * @param mixed $visible - * @return $this - */ - public function visible($visible) - { - $this->options['visible'] = $visible; - return $this; - } - - /** - * 设置模型输出隐藏字段 - * @access public - * @param mixed $hidden - * @return $this - */ - public function hidden($hidden) - { - $this->options['hidden'] = $hidden; - return $this; - } - /** * 指定数据表别名 * @access public @@ -2342,14 +2318,6 @@ class Query if (!empty($options['with_count'])) { $model->relationCount($model, $options['with_count']); } - - // 显式或隐藏属性 - if (!empty($options['visible'])) { - $model->visible($options['visible']); - } elseif (!empty($options['hidden'])) { - $model->hidden($options['hidden']); - } - $resultSet[$key] = $model; } if (!empty($options['with'])) { @@ -2461,12 +2429,6 @@ class Query if (!empty($options['with_count'])) { $data->relationCount($data, $options['with_count']); } - // 显式或隐藏属性 - if (!empty($options['visible'])) { - $data->visible($options['visible']); - } elseif (!empty($options['hidden'])) { - $data->hidden($options['hidden']); - } } } elseif (!empty($options['fail'])) { $this->throwNotFound($options); diff --git a/library/think/model/Collection.php b/library/think/model/Collection.php index e44ffca6..5dd1da7e 100644 --- a/library/think/model/Collection.php +++ b/library/think/model/Collection.php @@ -27,4 +27,5 @@ class Collection extends BaseCollection $item->eagerlyResultSet($this->items, $relation); return $this; } + } From 9d1b13b66672c7d275196bf50e10a07739a16b3d Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 23 Jan 2017 08:42:20 +0800 Subject: [PATCH 065/125] =?UTF-8?q?Model=E7=B1=BBappend=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=BF=BD=E5=8A=A0=E5=85=B3=E8=81=94=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Collection.php | 18 ++++++++++++++++++ library/think/Model.php | 20 +++++++++++++------- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/library/think/Collection.php b/library/think/Collection.php index b450c48c..20bbe6a2 100644 --- a/library/think/Collection.php +++ b/library/think/Collection.php @@ -24,6 +24,8 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria protected $visible = []; // 隐藏属性 protected $hidden = []; + // 追加属性 + protected $append = []; public function __construct($items = []) { @@ -69,6 +71,19 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria return $this; } + /** + * 设置需要追加的输出属性 + * @access public + * @param array $append 属性列表 + * @param bool $override 是否覆盖 + * @return $this + */ + public function append($append = [], $override = false) + { + $this->append = [$append, $override]; + return $this; + } + public function toArray() { $result = []; @@ -79,6 +94,9 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria } elseif (!empty($this->hidden)) { $item->hidden($this->hidden[0], $this->hidden[1]); } + if (!empty($this->append)) { + $item->append($this->append[0], $this->append[1]); + } $result[$key] = $item->toArray(); } else { $result[$key] = $item; diff --git a/library/think/Model.php b/library/think/Model.php index 772e6f12..3945269f 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -596,12 +596,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public function toArray() { - $item = []; - $visible = []; - $hidden = []; + $item = []; // 过滤属性 if (!empty($this->visible)) { - $array = []; + $visible = []; + $array = []; foreach ($this->visible as $key => $val) { if (is_array($val)) { $array[] = $key; @@ -612,7 +611,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } $data = array_intersect_key($this->data, array_flip($array)); } elseif (!empty($this->hidden)) { - $array = []; + $hidden = []; + $array = []; foreach ($this->hidden as $key => $val) { if (is_array($val)) { $hidden[$key] = $val; @@ -653,8 +653,14 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } // 追加属性(必须定义获取器) if (!empty($this->append)) { - foreach ($this->append as $name) { - $item[$name] = $this->getAttr($name); + foreach ($this->append as $key => $name) { + if (is_array($name)) { + // 追加关联对象属性 + $relation = $this->getAttr($key); + $item[$key] = $relation->append($name)->toArray(); + } else { + $item[$name] = $this->getAttr($name); + } } } return !empty($item) ? $item : []; From 6ade10482aecbe4874829cad9f092e83e8727a11 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 23 Jan 2017 09:13:52 +0800 Subject: [PATCH 066/125] =?UTF-8?q?=E6=94=B9=E8=BF=9Bhidden=20visible=20ap?= =?UTF-8?q?pend=20=E6=94=AF=E6=8C=81=E5=8D=95=E4=B8=AA=E7=82=B9=E8=AF=AD?= =?UTF-8?q?=E6=B3=95=E7=94=A8=E4=BA=8E=E5=85=B3=E8=81=94=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 94 ++++++++++++++++++++++++++--------------- 1 file changed, 60 insertions(+), 34 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 3945269f..06d95d0e 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -579,6 +579,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 设置需要输出的属性 + * @access public * @param array $visible * @param bool $override 是否覆盖 * @return $this @@ -589,6 +590,51 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return $this; } + /** + * 解析隐藏及显示属性 + * @access protected + * @param array $attrs 属性 + * @param array $result 结果集 + * @return array + */ + protected function parseAttr($attrs, &$result, $visible = true) + { + $array = []; + foreach ($attrs as $key => $val) { + if (is_array($val)) { + if ($visible) { + $array[] = $key; + } + $result[$key] = $val; + } elseif (strpos($val, '.')) { + list($key, $name) = explode('.', $val); + if ($visible) { + $array[] = $key; + } + $result[$key][] = $name; + } else { + $array[] = $val; + } + } + return $array; + } + + /** + * 转换子模型对象 + * @access protected + * @return array + */ + protected function subToArray($model, $visible, $hidden, $key) + { + // 关联模型对象 + if (isset($visible[$key])) { + $model->visible($visible[$key]); + } elseif (isset($hidden[$key])) { + $model->hidden($hidden[$key]); + } + return $model->toArray(); + } + /** * 转换当前模型对象为数组 * @access public @@ -596,31 +642,16 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public function toArray() { - $item = []; + $item = []; + $visible = []; + $hidden = []; // 过滤属性 if (!empty($this->visible)) { - $visible = []; - $array = []; - foreach ($this->visible as $key => $val) { - if (is_array($val)) { - $array[] = $key; - $visible[$key] = $val; - } else { - $array[] = $val; - } - } - $data = array_intersect_key($this->data, array_flip($array)); + $array = $this->parseAttr($this->visible, $visible); + $data = array_intersect_key($this->data, array_flip($array)); } elseif (!empty($this->hidden)) { - $hidden = []; - $array = []; - foreach ($this->hidden as $key => $val) { - if (is_array($val)) { - $hidden[$key] = $val; - } else { - $array[] = $val; - } - } - $data = array_diff_key($this->data, array_flip($array)); + $array = $this->parseAttr($this->hidden, $hidden, false); + $data = array_diff_key($this->data, array_flip($array)); } else { $data = $this->data; } @@ -628,22 +659,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess foreach ($data as $key => $val) { if ($val instanceof Model || $val instanceof Collection) { // 关联模型对象 - if (isset($visible[$key])) { - $val->visible($visible[$key]); - } elseif (isset($hidden[$key])) { - $val->hidden($hidden[$key]); - } - $item[$key] = $val->toArray(); + $item[$key] = $this->subToArray($val, $visible, $hidden, $key); } elseif (is_array($val) && reset($val) instanceof Model) { // 关联模型数据集 $arr = []; foreach ($val as $k => $value) { - if (isset($visible[$k])) { - $value->visible($visible[$k]); - } elseif (isset($hidden[$k])) { - $value->hidden($hidden[$k]); - } - $arr[$k] = $value->toArray(); + $arr[$k] = $this->subToArray($value, $visible, $hidden, $k); } $item[$key] = $arr; } else { @@ -658,6 +679,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 追加关联对象属性 $relation = $this->getAttr($key); $item[$key] = $relation->append($name)->toArray(); + } elseif (strpos($name, '.')) { + list($key, $attr) = explode('.', $name); + // 追加关联对象属性 + $relation = $this->getAttr($key); + $item[$key] = $relation->append([$attr])->toArray(); } else { $item[$name] = $this->getAttr($name); } From c45229c76267c059c1fa61c78709daca0b054b15 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 23 Jan 2017 11:06:07 +0800 Subject: [PATCH 067/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BMerge=E7=B1=BBsave?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/Merge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/think/model/Merge.php b/library/think/model/Merge.php index b876c2f3..35bf54c0 100644 --- a/library/think/model/Merge.php +++ b/library/think/model/Merge.php @@ -158,7 +158,7 @@ class Merge extends Model } // 数据对象赋值 foreach ($data as $key => $value) { - $this->setAttr($key, $value); + $this->setAttr($key, $value, $data); } if (!empty($where)) { $this->isUpdate = true; From 8311a4e39bb6f2c704e66fad74e868f5d1b6cdfd Mon Sep 17 00:00:00 2001 From: yunwuxin <448901948@qq.com> Date: Mon, 23 Jan 2017 13:02:34 +0800 Subject: [PATCH 068/125] =?UTF-8?q?=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Collection.php | 64 ++---------------------------- library/think/Model.php | 9 ++++- library/think/model/Collection.php | 48 ++++++++++++++++++++++ 3 files changed, 58 insertions(+), 63 deletions(-) diff --git a/library/think/Collection.php b/library/think/Collection.php index 20bbe6a2..41b42759 100644 --- a/library/think/Collection.php +++ b/library/think/Collection.php @@ -20,12 +20,6 @@ use JsonSerializable; class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable { protected $items = []; - // 显示属性 - protected $visible = []; - // 隐藏属性 - protected $hidden = []; - // 追加属性 - protected $append = []; public function __construct($items = []) { @@ -46,63 +40,11 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria return empty($this->items); } - /** - * 设置需要隐藏的输出属性 - * @access public - * @param array $hidden 属性列表 - * @param bool $override 是否覆盖 - * @return $this - */ - public function hidden($hidden = [], $override = false) - { - $this->hidden = [$hidden, $override]; - return $this; - } - - /** - * 设置需要输出的属性 - * @param array $visible - * @param bool $override 是否覆盖 - * @return $this - */ - public function visible($visible = [], $override = false) - { - $this->visible = [$visible, $override]; - return $this; - } - - /** - * 设置需要追加的输出属性 - * @access public - * @param array $append 属性列表 - * @param bool $override 是否覆盖 - * @return $this - */ - public function append($append = [], $override = false) - { - $this->append = [$append, $override]; - return $this; - } - public function toArray() { - $result = []; - foreach ($this->items as $key => $item) { - if ($item instanceof Model || $item instanceof self) { - if (!empty($this->visible)) { - $item->visible($this->visible[0], $this->visible[1]); - } elseif (!empty($this->hidden)) { - $item->hidden($this->hidden[0], $this->hidden[1]); - } - if (!empty($this->append)) { - $item->append($this->append[0], $this->append[1]); - } - $result[$key] = $item->toArray(); - } else { - $result[$key] = $item; - } - } - return $result; + return array_map(function ($value) { + return ($value instanceof Model || $value instanceof self) ? $value->toArray() : $value; + }, $this->items); } public function all() diff --git a/library/think/Model.php b/library/think/Model.php index 06d95d0e..235fb2d1 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -593,8 +593,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 解析隐藏及显示属性 * @access protected - * @param array $attrs 属性 - * @param array $result 结果集 + * @param array $attrs 属性 + * @param array $result 结果集 + * @param bool $visible * @return array */ protected function parseAttr($attrs, &$result, $visible = true) @@ -622,6 +623,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 转换子模型对象 * @access protected + * @param Model|Collection $model + * @param $visible + * @param $hidden + * @param $key * @return array */ protected function subToArray($model, $visible, $hidden, $key) diff --git a/library/think/model/Collection.php b/library/think/model/Collection.php index 5dd1da7e..08e11ad8 100644 --- a/library/think/model/Collection.php +++ b/library/think/model/Collection.php @@ -12,6 +12,7 @@ namespace think\model; use think\Collection as BaseCollection; +use think\Model; class Collection extends BaseCollection { @@ -28,4 +29,51 @@ class Collection extends BaseCollection return $this; } + /** + * 设置需要隐藏的输出属性 + * @access public + * @param array $hidden 属性列表 + * @param bool $override 是否覆盖 + * @return $this + */ + public function hidden($hidden = [], $override = false) + { + $this->each(function ($model) use ($hidden, $override) { + /** @var Model $model */ + $model->hidden($hidden, $override); + }); + return $this; + } + + /** + * 设置需要输出的属性 + * @param array $visible + * @param bool $override 是否覆盖 + * @return $this + */ + public function visible($visible = [], $override = false) + { + $this->each(function ($model) use ($visible, $override) { + /** @var Model $model */ + $model->visible($visible, $override); + }); + return $this; + } + + /** + * 设置需要追加的输出属性 + * @access public + * @param array $append 属性列表 + * @param bool $override 是否覆盖 + * @return $this + */ + public function append($append = [], $override = false) + { + $this->each(function ($model) use ($append, $override) { + /** @var Model $model */ + $model->append($append, $override); + }); + return $this; + } + } From 6078d0ea39adac747357c3a68c71aef1cf4483c9 Mon Sep 17 00:00:00 2001 From: yunwuxin <448901948@qq.com> Date: Mon, 23 Jan 2017 13:46:17 +0800 Subject: [PATCH 069/125] =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 214 +++++++++--------- library/think/model/Merge.php | 42 ++-- library/think/model/Relation.php | 17 +- library/think/model/relation/BelongsTo.php | 5 +- .../think/model/relation/BelongsToMany.php | 81 +++---- library/think/model/relation/HasMany.php | 77 ++++--- .../think/model/relation/HasManyThrough.php | 55 ++--- library/think/model/relation/HasOne.php | 35 +-- library/think/model/relation/MorphMany.php | 72 +++--- library/think/model/relation/MorphTo.php | 48 ++-- library/think/model/relation/OneToOne.php | 96 ++++---- 11 files changed, 402 insertions(+), 340 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 235fb2d1..085ca053 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -27,18 +27,7 @@ use think\model\relation\MorphTo; /** * Class Model * @package think - * @method static Paginator paginate(integer $listRows = 15, boolean $simple = false, array $config = []) 分页查询 - * @method static mixed value($field, $default = null) 得到某个字段的值 - * @method static array column($field, $key = '') 得到某个列的数组 - * @method static integer count($field = '*') COUNT查询 - * @method static integer sum($field = '*') SUM查询 - * @method static integer min($field = '*') MIN查询 - * @method static integer max($field = '*') MAX查询 - * @method static integer avg($field = '*') AVG查询 - * @method static setField($field, $value = '') - * @method static Query where($field, $op = null, $condition = null) 指定AND查询条件 - * @method static static findOrFail($data = null) 查找单条记录 如果不存在则抛出异常 - * + * @mixin Query */ abstract class Model implements \JsonSerializable, \ArrayAccess { @@ -200,7 +189,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } // 全局作用域 if ($baseQuery && method_exists($this, 'base')) { - call_user_func_array([$this, 'base'], [ & self::$links[$model]]); + call_user_func_array([$this, 'base'], [& self::$links[$model]]); } // 返回当前模型的数据库查询对象 return self::$links[$model]; @@ -226,12 +215,13 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @return void */ protected static function init() - {} + { + } /** * 设置数据对象值 * @access public - * @param mixed $data 数据或者属性名 + * @param mixed $data 数据或者属性名 * @param mixed $value 值 * @return $this */ @@ -278,9 +268,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 修改器 设置数据对象值 * @access public - * @param string $name 属性名 - * @param mixed $value 属性值 - * @param array $data 数据 + * @param string $name 属性名 + * @param mixed $value 属性值 + * @param array $data 数据 * @return $this */ public function setAttr($name, $value, $data = []) @@ -313,7 +303,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 自动写入时间戳 * @access public - * @param string $name 时间戳字段 + * @param string $name 时间戳字段 * @return mixed */ protected function autoWriteTimestamp($name) @@ -335,7 +325,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $value = $_SERVER['REQUEST_TIME']; break; } - } elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), ['datetime', 'date', 'timestamp'])) { + } elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), [ + 'datetime', + 'date', + 'timestamp' + ]) + ) { $value = $this->formatDateTime($_SERVER['REQUEST_TIME'], $this->dateFormat); } else { $value = $this->formatDateTime($_SERVER['REQUEST_TIME'], $this->dateFormat, true); @@ -346,9 +341,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 时间日期字段格式化处理 * @access public - * @param mixed $time 时间日期表达式 - * @param mixed $format 日期格式 - * @param bool $timestamp 是否进行时间戳转换 + * @param mixed $time 时间日期表达式 + * @param mixed $format 日期格式 + * @param bool $timestamp 是否进行时间戳转换 * @return mixed */ protected function formatDateTime($time, $format, $timestamp = false) @@ -364,8 +359,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 数据写入 类型转换 * @access public - * @param mixed $value 值 - * @param string|array $type 要转换的类型 + * @param mixed $value 值 + * @param string|array $type 要转换的类型 * @return mixed */ protected function writeTransform($value, $type) @@ -443,7 +438,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 类型转换 $value = $this->readTransform($value, $this->type[$name]); } elseif (in_array($name, [$this->createTime, $this->updateTime])) { - if (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), ['datetime', 'date', 'timestamp'])) { + if (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), [ + 'datetime', + 'date', + 'timestamp' + ]) + ) { $value = $this->formatDateTime(strtotime($value), $this->dateFormat); } else { $value = $this->formatDateTime($value, $this->dateFormat); @@ -467,8 +467,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 数据读取 类型转换 * @access public - * @param mixed $value 值 - * @param string|array $type 要转换的类型 + * @param mixed $value 值 + * @param string|array $type 要转换的类型 * @return mixed */ protected function readTransform($value, $type) @@ -528,8 +528,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 设置需要追加的输出属性 * @access public - * @param array $append 属性列表 - * @param bool $override 是否覆盖 + * @param array $append 属性列表 + * @param bool $override 是否覆盖 * @return $this */ public function append($append = [], $override = false) @@ -541,9 +541,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 设置附加关联对象的属性 * @access public - * @param string $relation 关联方法 - * @param string|array $append 追加属性名 + * @param string $relation 关联方法 + * @param string|array $append 追加属性名 * @return $this + * @throws Exception */ public function appendRelationAttr($relation, $append) { @@ -567,8 +568,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 设置需要隐藏的输出属性 * @access public - * @param array $hidden 属性列表 - * @param bool $override 是否覆盖 + * @param array $hidden 属性列表 + * @param bool $override 是否覆盖 * @return $this */ public function hidden($hidden = [], $override = false) @@ -581,7 +582,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * 设置需要输出的属性 * @access public * @param array $visible - * @param bool $override 是否覆盖 + * @param bool $override 是否覆盖 * @return $this */ public function visible($visible = [], $override = false) @@ -624,9 +625,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * 转换子模型对象 * @access protected * @param Model|Collection $model - * @param $visible - * @param $hidden - * @param $key + * @param $visible + * @param $hidden + * @param $key * @return array */ protected function subToArray($model, $visible, $hidden, $key) @@ -700,7 +701,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 转换当前模型对象为JSON字符串 * @access public - * @param integer $options json参数 + * @param integer $options json参数 * @return string */ public function toJson($options = JSON_UNESCAPED_UNICODE) @@ -711,7 +712,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 转换当前模型数据集为数据集对象 * @access public - * @param array|Collection $collection 数据集 + * @param array|Collection $collection 数据集 * @return Collection */ public function toCollection($collection) @@ -730,7 +731,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 关联数据一起更新 * @access public - * @param mixed $relation 关联 + * @param mixed $relation 关联 * @return $this */ public function together($relation) @@ -779,9 +780,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 保存当前数据对象 * @access public - * @param array $data 数据 - * @param array $where 更新条件 - * @param string $sequence 自增序列名 + * @param array $data 数据 + * @param array $where 更新条件 + * @param string $sequence 自增序列名 * @return integer|false */ public function save($data = [], $where = [], $sequence = null) @@ -962,9 +963,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 保存多个数据到当前数据对象 * @access public - * @param array $dataSet 数据 - * @param boolean $replace 是否自动识别更新和写入 + * @param array $dataSet 数据 + * @param boolean $replace 是否自动识别更新和写入 * @return array|false + * @throws \Exception */ public function saveAll($dataSet, $replace = true) { @@ -1105,8 +1107,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 设置字段验证 * @access public - * @param array|string|bool $rule 验证规则 true表示自动读取验证器类 - * @param array $msg 提示信息 + * @param array|string|bool $rule 验证规则 true表示自动读取验证器类 + * @param array $msg 提示信息 * @param bool $batch 批量验证 * @return $this */ @@ -1139,8 +1141,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 自动验证数据 * @access protected - * @param array $data 验证数据 - * @param mixed $rule 验证规则 + * @param array $data 验证数据 + * @param mixed $rule 验证规则 * @param bool $batch 批量验证 * @return bool */ @@ -1191,9 +1193,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 注册回调方法 * @access public - * @param string $event 事件名 - * @param callable $callback 回调方法 - * @param bool $override 是否覆盖 + * @param string $event 事件名 + * @param callable $callback 回调方法 + * @param bool $override 是否覆盖 * @return void */ public static function event($event, $callback, $override = false) @@ -1208,8 +1210,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 触发事件 * @access protected - * @param string $event 事件名 - * @param mixed $params 传入参数(引用) + * @param string $event 事件名 + * @param mixed $params 传入参数(引用) * @return bool */ protected function trigger($event, &$params) @@ -1217,7 +1219,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess if (isset(self::$event[$this->class][$event])) { foreach (self::$event[$this->class][$event] as $callback) { if (is_callable($callback)) { - $result = call_user_func_array($callback, [ & $params]); + $result = call_user_func_array($callback, [& $params]); if (false === $result) { return false; } @@ -1230,8 +1232,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 写入数据 * @access public - * @param array $data 数据数组 - * @param array|true $field 允许字段 + * @param array $data 数据数组 + * @param array|true $field 允许字段 * @return $this */ public static function create($data = [], $field = null) @@ -1247,9 +1249,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 更新数据 * @access public - * @param array $data 数据数组 - * @param array $where 更新条件 - * @param array|true $field 允许字段 + * @param array $data 数据数组 + * @param array $where 更新条件 + * @param array|true $field 允许字段 * @return $this */ public static function update($data = [], $where = [], $field = null) @@ -1295,9 +1297,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 分析查询表达式 * @access public - * @param mixed $data 主键列表或者查询条件(闭包) - * @param string $with 关联预查询 - * @param bool $cache 是否缓存 + * @param mixed $data 主键列表或者查询条件(闭包) + * @param string $with 关联预查询 + * @param bool $cache 是否缓存 * @return Query */ protected static function parseQuery(&$data, $with, $cache) @@ -1307,7 +1309,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $result = $result->where($data); $data = null; } elseif ($data instanceof \Closure) { - call_user_func_array($data, [ & $result]); + call_user_func_array($data, [& $result]); $data = null; } elseif ($data instanceof Query) { $result = $data->with($with)->cache($cache); @@ -1330,7 +1332,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $query->where($data); $data = null; } elseif ($data instanceof \Closure) { - call_user_func_array($data, [ & $query]); + call_user_func_array($data, [& $query]); $data = null; } elseif (is_null($data)) { return 0; @@ -1349,9 +1351,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 命名范围 * @access public - * @param string|array|Closure $name 命名范围名称 逗号分隔 - * @param mixed ...$params 参数调用 - * @return Model + * @param string|array|\Closure $name 命名范围名称 逗号分隔 + * @internal mixed ...$params 参数调用 + * @return Model|Query */ public static function scope($name) { @@ -1379,7 +1381,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 设置是否使用全局查询范围 - * @param bool $use 是否启用全局查询范围 + * @param bool $use 是否启用全局查询范围 * @access public * @return Model */ @@ -1393,10 +1395,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 根据关联条件查询当前模型 * @access public - * @param string $relation 关联方法名 - * @param mixed $operator 比较操作符 - * @param integer $count 个数 - * @param string $id 关联表的统计字段 + * @param string $relation 关联方法名 + * @param mixed $operator 比较操作符 + * @param integer $count 个数 + * @param string $id 关联表的统计字段 * @return Model */ public static function has($relation, $operator = '>=', $count = 1, $id = '*') @@ -1411,8 +1413,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 根据关联条件查询当前模型 * @access public - * @param string $relation 关联方法名 - * @param mixed $where 查询条件(数组或者闭包) + * @param string $relation 关联方法名 + * @param mixed $where 查询条件(数组或者闭包) * @return Model */ public static function hasWhere($relation, $where = []) @@ -1470,8 +1472,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 预载入关联查询 返回数据集 * @access public - * @param array $resultSet 数据集 - * @param string $relation 关联名 + * @param array $resultSet 数据集 + * @param string $relation 关联名 * @return array */ public function eagerlyResultSet(&$resultSet, $relation) @@ -1495,8 +1497,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 预载入关联查询 返回模型对象 * @access public - * @param Model $result 数据对象 - * @param string $relation 关联名 + * @param Model $result 数据对象 + * @param string $relation 关联名 * @return Model */ public function eagerlyResult(&$result, $relation) @@ -1521,8 +1523,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 关联统计 * @access public - * @param Model $result 数据对象 - * @param string|array $relation 关联名 + * @param Model $result 数据对象 + * @param string|array $relation 关联名 * @return void */ public function relationCount(&$result, $relation) @@ -1558,11 +1560,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * HAS ONE 关联定义 * @access public - * @param string $model 模型名 + * @param string $model 模型名 * @param string $foreignKey 关联外键 - * @param string $localKey 关联主键 - * @param array $alias 别名定义(已经废弃) - * @param string $joinType JOIN类型 + * @param string $localKey 关联主键 + * @param array $alias 别名定义(已经废弃) + * @param string $joinType JOIN类型 * @return HasOne */ public function hasOne($model, $foreignKey = '', $localKey = '', $alias = [], $joinType = 'INNER') @@ -1577,11 +1579,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * BELONGS TO 关联定义 * @access public - * @param string $model 模型名 + * @param string $model 模型名 * @param string $foreignKey 关联外键 - * @param string $localKey 关联主键 - * @param array $alias 别名定义(已经废弃) - * @param string $joinType JOIN类型 + * @param string $localKey 关联主键 + * @param array $alias 别名定义(已经废弃) + * @param string $joinType JOIN类型 * @return BelongsTo */ public function belongsTo($model, $foreignKey = '', $localKey = '', $alias = [], $joinType = 'INNER') @@ -1596,9 +1598,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * HAS MANY 关联定义 * @access public - * @param string $model 模型名 + * @param string $model 模型名 * @param string $foreignKey 关联外键 - * @param string $localKey 关联主键 + * @param string $localKey 关联主键 * @return HasMany */ public function hasMany($model, $foreignKey = '', $localKey = '') @@ -1613,11 +1615,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * HAS MANY 远程关联定义 * @access public - * @param string $model 模型名 - * @param string $through 中间模型名 + * @param string $model 模型名 + * @param string $through 中间模型名 * @param string $foreignKey 关联外键 * @param string $throughKey 关联外键 - * @param string $localKey 关联主键 + * @param string $localKey 关联主键 * @return HasManyThrough */ public function hasManyThrough($model, $through, $foreignKey = '', $throughKey = '', $localKey = '') @@ -1634,10 +1636,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * BELONGS TO MANY 关联定义 * @access public - * @param string $model 模型名 - * @param string $table 中间表名 + * @param string $model 模型名 + * @param string $table 中间表名 * @param string $foreignKey 关联外键 - * @param string $localKey 当前模型关联键 + * @param string $localKey 当前模型关联键 * @return BelongsToMany */ public function belongsToMany($model, $table = '', $foreignKey = '', $localKey = '') @@ -1654,9 +1656,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * MORPH MANY 关联定义 * @access public - * @param string $model 模型名 - * @param string|array $morph 多态字段信息 - * @param string $type 多态类型 + * @param string $model 模型名 + * @param string|array $morph 多态字段信息 + * @param string $type 多态类型 * @return MorphMany */ public function morphMany($model, $morph = null, $type = '') @@ -1680,8 +1682,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * MORPH TO 关联定义 * @access public - * @param string|array $morph 多态字段信息 - * @param array $alias 多态别名定义 + * @param string|array $morph 多态字段信息 + * @param array $alias 多态别名定义 * @return MorphTo */ public function morphTo($morph = null, $alias = []) @@ -1735,8 +1737,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 修改器 设置数据对象的值 * @access public - * @param string $name 名称 - * @param mixed $value 值 + * @param string $name 名称 + * @param mixed $value 值 * @return void */ public function __set($name, $value) @@ -1829,6 +1831,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 模型事件快捷方法 + * @param $callback + * @param bool $override */ protected static function beforeInsert($callback, $override = false) { diff --git a/library/think/model/Merge.php b/library/think/model/Merge.php index 35bf54c0..47d54923 100644 --- a/library/think/model/Merge.php +++ b/library/think/model/Merge.php @@ -11,7 +11,6 @@ namespace think\model; -use think\Db; use think\db\Query; use think\Model; @@ -40,9 +39,9 @@ class Merge extends Model /** * 查找单条记录 * @access public - * @param mixed $data 主键值或者查询条件(闭包) - * @param string $with 关联预查询 - * @param bool $cache 是否缓存 + * @param mixed $data 主键值或者查询条件(闭包) + * @param string|array $with 关联预查询 + * @param bool $cache 是否缓存 * @return \think\Model */ public static function get($data = null, $with = [], $cache = false) @@ -78,11 +77,11 @@ class Merge extends Model /** * 获取关联模型的字段 并解决混淆 * @access protected - * @param \think\db\Query $query 查询对象 - * @param string $name 模型名称 - * @param string $table 关联表名称 - * @param array $map 字段映射 - * @param array $fields 查询字段 + * @param \think\db\Query $query 查询对象 + * @param string $name 模型名称 + * @param string $table 关联表名称 + * @param array $map 字段映射 + * @param array $fields 查询字段 * @return array */ protected static function getModelField($query, $name, $table = '', $map = [], $fields = []) @@ -104,8 +103,9 @@ class Merge extends Model /** * 查找所有记录 * @access public - * @param mixed $data 主键列表或者查询条件(闭包) - * @param string $with 关联预查询 + * @param mixed $data 主键列表或者查询条件(闭包) + * @param array|string $with 关联预查询 + * @param bool $cache * @return array|false|string */ public static function all($data = null, $with = [], $cache = false) @@ -118,10 +118,10 @@ class Merge extends Model /** * 处理写入的模型数据 * @access public - * @param string $model 模型名称 - * @param array $data 数据 - * @param bool $insert 是否新增 - * @return void + * @param string $model 模型名称 + * @param array $data 数据 + * @param bool $insert 是否新增 + * @return array */ protected function parseData($model, $data, $insert = false) { @@ -144,10 +144,11 @@ class Merge extends Model /** * 保存模型数据 以及关联数据 * @access public - * @param mixed $data 数据 - * @param array $where 更新条件 - * @param string $sequence 自增序列名 - * @return integer|false + * @param mixed $data 数据 + * @param array $where 更新条件 + * @param string $sequence 自增序列名 + * @return false|int + * @throws \Exception */ public function save($data = [], $where = [], $sequence = null) { @@ -278,7 +279,8 @@ class Merge extends Model /** * 删除当前的记录 并删除关联数据 * @access public - * @return integer + * @return int + * @throws \Exception */ public function delete() { diff --git a/library/think/model/Relation.php b/library/think/model/Relation.php index 5c2db75e..3d56091b 100644 --- a/library/think/model/Relation.php +++ b/library/think/model/Relation.php @@ -15,13 +15,19 @@ use think\db\Query; use think\Exception; use think\Model; +/** + * Class Relation + * @package think\model + * + * @mixin Query + */ abstract class Relation { // 父模型对象 protected $parent; /** @var Model 当前关联的模型类 */ protected $model; - // 关联模型查询对象 + /** @var Query 关联模型查询对象 */ protected $query; // 关联表外键 protected $foreignKey; @@ -65,7 +71,7 @@ abstract class Relation /** * 封装关联数据集 * @access public - * @param array $resultSet 数据集 + * @param array $resultSet 数据集 * @return mixed */ protected function resultSetBuild($resultSet) @@ -84,6 +90,13 @@ abstract class Relation return $this; } + /** + * 执行基础查询(进执行一次) + * @access protected + * @return void + */ + abstract protected function baseQuery(); + public function __call($method, $args) { if ($this->query) { diff --git a/library/think/model/relation/BelongsTo.php b/library/think/model/relation/BelongsTo.php index 2f067c30..d1c4b9bd 100644 --- a/library/think/model/relation/BelongsTo.php +++ b/library/think/model/relation/BelongsTo.php @@ -37,9 +37,10 @@ class BelongsTo extends OneToOne /** * 延迟获取关联数据 - * @param string $subRelation 子关联名 - * @param \Closure $closure 闭包查询条件 + * @param string $subRelation 子关联名 + * @param \Closure $closure 闭包查询条件 * @access public + * @return array|false|\PDOStatement|string|Model */ public function getRelation($subRelation = '', $closure = null) { diff --git a/library/think/model/relation/BelongsToMany.php b/library/think/model/relation/BelongsToMany.php index 9c6337dc..c85a8c0c 100644 --- a/library/think/model/relation/BelongsToMany.php +++ b/library/think/model/relation/BelongsToMany.php @@ -11,7 +11,6 @@ namespace think\model\relation; -use think\Db; use think\db\Query; use think\Exception; use think\Loader; @@ -27,11 +26,11 @@ class BelongsToMany extends Relation /** * 架构函数 * @access public - * @param Model $parent 上级模型对象 - * @param string $model 模型名 - * @param string $table 中间表名 + * @param Model $parent 上级模型对象 + * @param string $model 模型名 + * @param string $table 中间表名 * @param string $foreignKey 关联模型外键 - * @param string $localKey 当前模型关联键 + * @param string $localKey 当前模型关联键 */ public function __construct(Model $parent, $model, $table, $foreignKey, $localKey) { @@ -45,9 +44,9 @@ class BelongsToMany extends Relation /** * 延迟获取关联数据 - * @param string $subRelation 子关联名 - * @param \Closure $closure 闭包查询条件 - * @access public + * @param string $subRelation 子关联名 + * @param \Closure $closure 闭包查询条件 + * @return false|\PDOStatement|string|\think\Collection */ public function getRelation($subRelation = '', $closure = null) { @@ -55,7 +54,7 @@ class BelongsToMany extends Relation $localKey = $this->localKey; $middle = $this->middle; if ($closure) { - call_user_func_array($closure, [ & $this->query]); + call_user_func_array($closure, [& $this->query]); } // 关联查询 $pk = $this->parent->getPk(); @@ -80,10 +79,10 @@ class BelongsToMany extends Relation /** * 预载入关联查询(数据集) * @access public - * @param array $resultSet 数据集 - * @param string $relation 当前关联名 - * @param string $subRelation 子关联名 - * @param \Closure $closure 闭包 + * @param array $resultSet 数据集 + * @param string $relation 当前关联名 + * @param string $subRelation 子关联名 + * @param \Closure $closure 闭包 * @return void */ public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure) @@ -124,10 +123,10 @@ class BelongsToMany extends Relation /** * 预载入关联查询(单个数据) * @access public - * @param Model $result 数据对象 - * @param string $relation 当前关联名 - * @param string $subRelation 子关联名 - * @param \Closure $closure 闭包 + * @param Model $result 数据对象 + * @param string $relation 当前关联名 + * @param string $subRelation 子关联名 + * @param \Closure $closure 闭包 * @return void */ public function eagerlyResult(&$result, $relation, $subRelation, $closure) @@ -149,8 +148,8 @@ class BelongsToMany extends Relation /** * 关联统计 * @access public - * @param Model $result 数据对象 - * @param \Closure $closure 闭包 + * @param Model $result 数据对象 + * @param \Closure $closure 闭包 * @return integer */ public function relationCount($result, $closure) @@ -167,20 +166,25 @@ class BelongsToMany extends Relation /** * 获取关联统计子查询 * @access public - * @param \Closure $closure 闭包 + * @param \Closure $closure 闭包 * @return string */ public function getRelationCountQuery($closure) { - return $this->belongsToManyQuery($this->middle, $this->foreignKey, $this->localKey, ['pivot.' . $this->localKey => ['exp', '=' . $this->parent->getTable() . '.' . $this->parent->getPk()]])->fetchSql()->count(); + return $this->belongsToManyQuery($this->middle, $this->foreignKey, $this->localKey, [ + 'pivot.' . $this->localKey => [ + 'exp', + '=' . $this->parent->getTable() . '.' . $this->parent->getPk() + ] + ])->fetchSql()->count(); } /** * 多对多 关联模型预查询 * @access public - * @param array $where 关联预查询条件 - * @param string $relation 关联名 - * @param string $subRelation 子关联 + * @param array $where 关联预查询条件 + * @param string $relation 关联名 + * @param string $subRelation 子关联 * @return array */ protected function eagerlyManyToMany($where, $relation, $subRelation = '') @@ -210,10 +214,10 @@ class BelongsToMany extends Relation /** * BELONGS TO MANY 关联查询 * @access public - * @param string $table 中间表名 - * @param string $foreignKey 关联模型关联键 - * @param string $localKey 当前模型关联键 - * @param array $condition 关联查询条件 + * @param string $table 中间表名 + * @param string $foreignKey 关联模型关联键 + * @param string $localKey 当前模型关联键 + * @param array $condition 关联查询条件 * @return Query */ protected function belongsToManyQuery($table, $foreignKey, $localKey, $condition = []) @@ -230,8 +234,8 @@ class BelongsToMany extends Relation /** * 保存(新增)当前关联数据对象 * @access public - * @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键 - * @param array $pivot 中间表额外数据 + * @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键 + * @param array $pivot 中间表额外数据 * @return integer */ public function save($data, array $pivot = []) @@ -243,9 +247,9 @@ class BelongsToMany extends Relation /** * 批量保存当前关联数据对象 * @access public - * @param array $dataSet 数据集 - * @param array $pivot 中间表额外数据 - * @param bool $samePivot 额外数据是否相同 + * @param array $dataSet 数据集 + * @param array $pivot 中间表额外数据 + * @param bool $samePivot 额外数据是否相同 * @return integer */ public function saveAll(array $dataSet, array $pivot = [], $samePivot = false) @@ -265,9 +269,10 @@ class BelongsToMany extends Relation /** * 附加关联的一个中间表数据 * @access public - * @param mixed $data 数据 可以使用数组、关联模型对象 或者 关联对象的主键 - * @param array $pivot 中间表额外数据 - * @return integer + * @param mixed $data 数据 可以使用数组、关联模型对象 或者 关联对象的主键 + * @param array $pivot 中间表额外数据 + * @return int + * @throws Exception */ public function attach($data, $pivot = []) { @@ -307,8 +312,8 @@ class BelongsToMany extends Relation /** * 解除关联的一个中间表数据 * @access public - * @param integer|array $data 数据 可以使用关联对象的主键 - * @param bool $relationDel 是否同时删除关联表数据 + * @param integer|array $data 数据 可以使用关联对象的主键 + * @param bool $relationDel 是否同时删除关联表数据 * @return integer */ public function detach($data = null, $relationDel = false) diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index 32cf1b76..3094bb55 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -22,10 +22,10 @@ class HasMany extends Relation /** * 架构函数 * @access public - * @param Model $parent 上级模型对象 - * @param string $model 模型名 + * @param Model $parent 上级模型对象 + * @param string $model 模型名 * @param string $foreignKey 关联外键 - * @param string $localKey 关联主键 + * @param string $localKey 关联主键 */ public function __construct(Model $parent, $model, $foreignKey, $localKey) { @@ -38,26 +38,25 @@ class HasMany extends Relation /** * 延迟获取关联数据 - * @param string $subRelation 子关联名 - * @param \Closure $closure 闭包查询条件 - * @access public + * @param string $subRelation 子关联名 + * @param \Closure $closure 闭包查询条件 + * @return false|\PDOStatement|string|\think\Collection */ 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 array $resultSet 数据集 - * @param string $relation 当前关联名 - * @param string $subRelation 子关联名 - * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 + * @access public + * @param array $resultSet 数据集 + * @param string $relation 当前关联名 + * @param string $subRelation 子关联名 + * @param \Closure $closure 闭包 * @return void */ public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure) @@ -92,12 +91,11 @@ class HasMany extends Relation /** * 预载入关联查询 - * @access public - * @param Model $result 数据对象 - * @param string $relation 当前关联名 - * @param string $subRelation 子关联名 - * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 + * @access public + * @param Model $result 数据对象 + * @param string $relation 当前关联名 + * @param string $subRelation 子关联名 + * @param \Closure $closure 闭包 * @return void */ public function eagerlyResult(&$result, $relation, $subRelation, $closure) @@ -117,8 +115,8 @@ class HasMany extends Relation /** * 关联统计 * @access public - * @param Model $result 数据对象 - * @param \Closure $closure 闭包 + * @param Model $result 数据对象 + * @param \Closure $closure 闭包 * @return integer */ public function relationCount($result, $closure) @@ -127,7 +125,7 @@ class HasMany extends Relation $count = 0; if (isset($result->$localKey)) { if ($closure) { - call_user_func_array($closure, [ & $this->query]); + call_user_func_array($closure, [& $this->query]); } $count = $this->query->where([$this->foreignKey => $result->$localKey])->count(); } @@ -137,26 +135,31 @@ class HasMany extends Relation /** * 创建关联统计子查询 * @access public - * @param \Closure $closure 闭包 + * @param \Closure $closure 闭包 * @return string */ 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->foreignKey => ['exp', '=' . $this->parent->getTable() . '.' . $this->parent->getPk()]])->fetchSql()->count(); + return $this->query->where([ + $this->foreignKey => [ + 'exp', + '=' . $this->parent->getTable() . '.' . $this->parent->getPk() + ] + ])->fetchSql()->count(); } /** * 一对多 关联模型预查询 * @access public - * @param object $model 关联模型对象 - * @param array $where 关联预查询条件 - * @param string $relation 关联名 - * @param string $subRelation 子关联 - * @param bool $closure + * @param object $model 关联模型对象 + * @param array $where 关联预查询条件 + * @param string $relation 关联名 + * @param string $subRelation 子关联 + * @param bool $closure * @return array */ protected function eagerlyOneToMany($model, $where, $relation, $subRelation = '', $closure = false) @@ -164,7 +167,7 @@ class HasMany extends Relation $foreignKey = $this->foreignKey; // 预载入关联查询 支持嵌套预载入 if ($closure) { - call_user_func_array($closure, [ & $model]); + call_user_func_array($closure, [& $model]); } $list = $model->where($where)->with($subRelation)->select(); @@ -179,7 +182,7 @@ class HasMany extends Relation /** * 保存(新增)当前关联数据对象 * @access public - * @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键 + * @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键 * @return integer */ public function save($data) @@ -196,7 +199,7 @@ class HasMany extends Relation /** * 批量保存当前关联数据对象 * @access public - * @param array $dataSet 数据集 + * @param array $dataSet 数据集 * @return integer */ public function saveAll(array $dataSet) @@ -211,9 +214,9 @@ class HasMany extends Relation /** * 根据关联条件查询当前模型 * @access public - * @param string $operator 比较操作符 - * @param integer $count 个数 - * @param string $id 关联表的统计字段 + * @param string $operator 比较操作符 + * @param integer $count 个数 + * @param string $id 关联表的统计字段 * @return Query */ public function has($operator = '>=', $count = 1, $id = '*') @@ -228,7 +231,7 @@ class HasMany extends Relation /** * 根据关联条件查询当前模型 * @access public - * @param mixed $where 查询条件(数组或者闭包) + * @param mixed $where 查询条件(数组或者闭包) * @return Query */ public function hasWhere($where = []) diff --git a/library/think/model/relation/HasManyThrough.php b/library/think/model/relation/HasManyThrough.php index 00caa8ad..e1518972 100644 --- a/library/think/model/relation/HasManyThrough.php +++ b/library/think/model/relation/HasManyThrough.php @@ -26,13 +26,13 @@ class HasManyThrough extends Relation /** * 架构函数 - * @access public - * @param Model $parent 上级模型对象 - * @param string $model 模型名 - * @param string $through 中间模型名 - * @param string $firstkey 关联外键 - * @param string $secondKey 关联外键 - * @param string $localKey 关联主键 + * @access public + * @param Model $parent 上级模型对象 + * @param string $model 模型名 + * @param string $through 中间模型名 + * @param string $foreignKey 关联外键 + * @param string $throughKey 关联外键 + * @param string $localKey 关联主键 */ public function __construct(Model $parent, $model, $through, $foreignKey, $throughKey, $localKey) { @@ -47,14 +47,14 @@ class HasManyThrough extends Relation /** * 延迟获取关联数据 - * @param string $subRelation 子关联名 - * @param \Closure $closure 闭包查询条件 - * @access public + * @param string $subRelation 子关联名 + * @param \Closure $closure 闭包查询条件 + * @return false|\PDOStatement|string|\think\Collection */ 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(); } @@ -62,38 +62,41 @@ class HasManyThrough extends Relation /** * 预载入关联查询 * @access public - * @param array $resultSet 数据集 - * @param string $relation 当前关联名 - * @param string $subRelation 子关联名 - * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 + * @param array $resultSet 数据集 + * @param string $relation 当前关联名 + * @param string $subRelation 子关联名 + * @param \Closure $closure 闭包 + * @param string $class 数据集对象名 为空表示数组 * @return void */ public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class) - {} + { + } /** * 预载入关联查询 返回模型对象 * @access public - * @param Model $result 数据对象 - * @param string $relation 当前关联名 - * @param string $subRelation 子关联名 - * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 + * @param Model $result 数据对象 + * @param string $relation 当前关联名 + * @param string $subRelation 子关联名 + * @param \Closure $closure 闭包 + * @param string $class 数据集对象名 为空表示数组 * @return void */ public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class) - {} + { + } /** * 关联统计 * @access public - * @param Model $result 数据对象 - * @param \Closure $closure 闭包 + * @param Model $result 数据对象 + * @param \Closure $closure 闭包 * @return integer */ public function relationCount($result, $closure) - {} + { + } /** * 执行基础查询(进执行一次) diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index 9882667e..2c0cf350 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -11,6 +11,7 @@ namespace think\model\relation; +use think\db\Query; use think\Loader; use think\Model; @@ -19,11 +20,11 @@ class HasOne extends OneToOne /** * 架构函数 * @access public - * @param Model $parent 上级模型对象 - * @param string $model 模型名 + * @param Model $parent 上级模型对象 + * @param string $model 模型名 * @param string $foreignKey 关联外键 - * @param string $localKey 关联主键 - * @param string $joinType JOIN类型 + * @param string $localKey 关联主键 + * @param string $joinType JOIN类型 */ public function __construct(Model $parent, $model, $foreignKey, $localKey, $joinType = 'INNER') { @@ -37,16 +38,16 @@ class HasOne extends OneToOne /** * 延迟获取关联数据 - * @param string $subRelation 子关联名 - * @param \Closure $closure 闭包查询条件 - * @access public + * @param string $subRelation 子关联名 + * @param \Closure $closure 闭包查询条件 + * @return array|false|\PDOStatement|string|Model */ public function getRelation($subRelation = '', $closure = null) { // 执行关联定义方法 $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(); @@ -55,7 +56,7 @@ class HasOne extends OneToOne /** * 根据关联条件查询当前模型 * @access public - * @param mixed $where 查询条件(数组或者闭包) + * @param mixed $where 查询条件(数组或者闭包) * @return Query */ public function hasWhere($where = []) @@ -80,10 +81,10 @@ class HasOne extends OneToOne /** * 预载入关联查询(数据集) * @access public - * @param array $resultSet 数据集 - * @param string $relation 当前关联名 - * @param string $subRelation 子关联名 - * @param \Closure $closure 闭包 + * @param array $resultSet 数据集 + * @param string $relation 当前关联名 + * @param string $subRelation 子关联名 + * @param \Closure $closure 闭包 * @return void */ protected function eagerlySet(&$resultSet, $relation, $subRelation, $closure) @@ -129,10 +130,10 @@ class HasOne extends OneToOne /** * 预载入关联查询(数据) * @access public - * @param Model $result 数据对象 - * @param string $relation 当前关联名 - * @param string $subRelation 子关联名 - * @param \Closure $closure 闭包 + * @param Model $result 数据对象 + * @param string $relation 当前关联名 + * @param string $subRelation 子关联名 + * @param \Closure $closure 闭包 * @return void */ protected function eagerlyOne(&$result, $relation, $subRelation, $closure) diff --git a/library/think/model/relation/MorphMany.php b/library/think/model/relation/MorphMany.php index e5ed1701..3fc8179c 100644 --- a/library/think/model/relation/MorphMany.php +++ b/library/think/model/relation/MorphMany.php @@ -28,11 +28,11 @@ class MorphMany extends Relation /** * 架构函数 * @access public - * @param Model $parent 上级模型对象 - * @param string $model 模型名 - * @param string $morphKey 关联外键 + * @param Model $parent 上级模型对象 + * @param string $model 模型名 + * @param string $morphKey 关联外键 * @param string $morphType 多态字段名 - * @param string $type 多态类型 + * @param string $type 多态类型 */ public function __construct(Model $parent, $model, $morphKey, $morphType, $type) { @@ -46,14 +46,14 @@ class MorphMany extends Relation /** * 延迟获取关联数据 - * @param string $subRelation 子关联名 - * @param \Closure $closure 闭包查询条件 - * @access public + * @param string $subRelation 子关联名 + * @param \Closure $closure 闭包查询条件 + * @return false|\PDOStatement|string|\think\Collection */ 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(); } @@ -61,10 +61,10 @@ class MorphMany extends Relation /** * 预载入关联查询 * @access public - * @param array $resultSet 数据集 - * @param string $relation 当前关联名 - * @param string $subRelation 子关联名 - * @param \Closure $closure 闭包 + * @param array $resultSet 数据集 + * @param string $relation 当前关联名 + * @param string $subRelation 子关联名 + * @param \Closure $closure 闭包 * @return void */ public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure) @@ -101,17 +101,20 @@ class MorphMany extends Relation /** * 预载入关联查询 * @access public - * @param Model $result 数据对象 - * @param string $relation 当前关联名 - * @param string $subRelation 子关联名 - * @param \Closure $closure 闭包 + * @param Model $result 数据对象 + * @param string $relation 当前关联名 + * @param string $subRelation 子关联名 + * @param \Closure $closure 闭包 * @return void */ public function eagerlyResult(&$result, $relation, $subRelation, $closure) { $pk = $result->getPk(); if (isset($result->$pk)) { - $data = $this->eagerlyMorphToMany([$this->morphKey => $result->$pk, $this->morphType => $this->type], $relation, $subRelation, $closure); + $data = $this->eagerlyMorphToMany([ + $this->morphKey => $result->$pk, + $this->morphType => $this->type + ], $relation, $subRelation, $closure); $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$pk])); } } @@ -119,8 +122,8 @@ class MorphMany extends Relation /** * 关联统计 * @access public - * @param Model $result 数据对象 - * @param \Closure $closure 闭包 + * @param Model $result 数据对象 + * @param \Closure $closure 闭包 * @return integer */ public function relationCount($result, $closure) @@ -129,7 +132,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(); } @@ -139,33 +142,38 @@ class MorphMany extends Relation /** * 获取关联统计子查询 * @access public - * @param \Closure $closure 闭包 + * @param \Closure $closure 闭包 * @return string */ 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->morphType => $this->type])->fetchSql()->count(); + return $this->query->where([ + $this->morphKey => [ + 'exp', + '=' . $this->parent->getTable() . '.' . $this->parent->getPk() + ], + $this->morphType => $this->type + ])->fetchSql()->count(); } /** * 多态一对多 关联模型预查询 - * @access public - * @param object $model 关联模型对象 - * @param array $where 关联预查询条件 - * @param string $relation 关联名 - * @param string $subRelation 子关联 - * @param \Closure $closure 闭包 + * @access public + * @param array $where 关联预查询条件 + * @param string $relation 关联名 + * @param string $subRelation 子关联 + * @param bool|\Closure $closure 闭包 * @return array */ protected function eagerlyMorphToMany($where, $relation, $subRelation = '', $closure = false) { // 预载入关联查询 支持嵌套预载入 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; @@ -180,7 +188,7 @@ class MorphMany extends Relation /** * 保存(新增)当前关联数据对象 * @access public - * @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键 + * @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键 * @return integer */ public function save($data) @@ -200,7 +208,7 @@ class MorphMany extends Relation /** * 批量保存当前关联数据对象 * @access public - * @param array $dataSet 数据集 + * @param array $dataSet 数据集 * @return integer */ public function saveAll(array $dataSet) diff --git a/library/think/model/relation/MorphTo.php b/library/think/model/relation/MorphTo.php index ffad707b..5e1867f2 100644 --- a/library/think/model/relation/MorphTo.php +++ b/library/think/model/relation/MorphTo.php @@ -27,10 +27,10 @@ class MorphTo extends Relation /** * 架构函数 * @access public - * @param Model $parent 上级模型对象 + * @param Model $parent 上级模型对象 * @param string $morphType 多态字段名 - * @param string $morphKey 外键名 - * @param array $alias 多态别名定义 + * @param string $morphKey 外键名 + * @param array $alias 多态别名定义 */ public function __construct(Model $parent, $morphType, $morphKey, $alias = []) { @@ -42,9 +42,9 @@ class MorphTo extends Relation /** * 延迟获取关联数据 - * @param string $subRelation 子关联名 - * @param \Closure $closure 闭包查询条件 - * @access public + * @param string $subRelation 子关联名 + * @param \Closure $closure 闭包查询条件 + * @return mixed */ public function getRelation($subRelation = '', $closure = null) { @@ -80,7 +80,7 @@ class MorphTo extends Relation /** * 设置多态别名 * @access public - * @param array $alias 别名定义 + * @param array $alias 别名定义 * @return $this */ public function setAlias($alias) @@ -92,11 +92,12 @@ class MorphTo extends Relation /** * 预载入关联查询 * @access public - * @param array $resultSet 数据集 - * @param string $relation 当前关联名 - * @param string $subRelation 子关联名 - * @param \Closure $closure 闭包 + * @param array $resultSet 数据集 + * @param string $relation 当前关联名 + * @param string $subRelation 子关联名 + * @param \Closure $closure 闭包 * @return void + * @throws Exception */ public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure) { @@ -140,10 +141,10 @@ class MorphTo extends Relation /** * 预载入关联查询 * @access public - * @param Model $result 数据对象 - * @param string $relation 当前关联名 - * @param string $subRelation 子关联名 - * @param \Closure $closure 闭包 + * @param Model $result 数据对象 + * @param string $relation 当前关联名 + * @param string $subRelation 子关联名 + * @param \Closure $closure 闭包 * @return void */ public function eagerlyResult(&$result, $relation, $subRelation, $closure) @@ -158,20 +159,21 @@ class MorphTo extends Relation /** * 关联统计 * @access public - * @param Model $result 数据对象 - * @param \Closure $closure 闭包 + * @param Model $result 数据对象 + * @param \Closure $closure 闭包 * @return integer */ public function relationCount($result, $closure) - {} + { + } /** * 多态MorphTo 关联模型预查询 - * @access public - * @param object $model 关联模型对象 - * @param array $where 关联预查询条件 - * @param string $relation 关联名 - * @param string $subRelation 子关联 + * @access public + * @param object $model 关联模型对象 + * @param string $relation 关联名 + * @param $result + * @param string $subRelation 子关联 * @return void */ protected function eagerlyMorphToOne($model, $relation, &$result, $subRelation = '') diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index ad5982d7..d0e1cce9 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -20,8 +20,6 @@ use think\model\Relation; /** * Class OneToOne * @package think\model\relation - * @method void eagerlySet(array $resultSet, string $relation, string $subRelation, \Closure $closure) 预载入关联查询(数据集) - * @method void eagerlyOne(Model $result, string $relation, string $subRelation, \Closure $closure) 预载入关联查询(数据) * */ abstract class OneToOne extends Relation @@ -36,7 +34,7 @@ abstract class OneToOne extends Relation /** * 设置join类型 * @access public - * @param string $type JOIN类型 + * @param string $type JOIN类型 * @return $this */ public function joinType($type) @@ -48,11 +46,11 @@ abstract class OneToOne extends Relation /** * 预载入关联查询(JOIN方式) * @access public - * @param Query $query 查询对象 - * @param string $relation 关联名 - * @param string $subRelation 子关联 - * @param \Closure $closure 闭包条件 - * @param bool $first + * @param Query $query 查询对象 + * @param string $relation 关联名 + * @param string $subRelation 子关联 + * @param \Closure $closure 闭包条件 + * @param bool $first * @return void */ public function eagerly(Query $query, $relation, $subRelation, $closure, $first) @@ -85,7 +83,7 @@ abstract class OneToOne extends Relation if ($closure) { // 执行闭包查询 - call_user_func_array($closure, [ & $query]); + call_user_func_array($closure, [& $query]); // 使用withField指定获取关联的字段,如 // $query->where(['id'=>1])->withField('id,name'); if ($query->getOptions('with_field')) { @@ -100,13 +98,33 @@ abstract class OneToOne extends Relation $query->field($field, false, $joinTable, $joinAlias, $relation . '__'); } + /** + * 预载入关联查询(数据集) + * @param array $resultSet + * @param string $relation + * @param string $subRelation + * @param \Closure $closure + * @return mixed + */ + abstract protected function eagerlySet(&$resultSet, $relation, $subRelation, $closure); + + /** + * 预载入关联查询(数据) + * @param Model $result + * @param string $relation + * @param string $subRelation + * @param \Closure $closure + * @return mixed + */ + abstract protected function eagerlyOne(&$result, $relation, $subRelation, $closure); + /** * 预载入关联查询(数据集) * @access public - * @param array $resultSet 数据集 - * @param string $relation 当前关联名 - * @param string $subRelation 子关联名 - * @param \Closure $closure 闭包 + * @param array $resultSet 数据集 + * @param string $relation 当前关联名 + * @param string $subRelation 子关联名 + * @param \Closure $closure 闭包 * @return void */ public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure) @@ -125,10 +143,10 @@ abstract class OneToOne extends Relation /** * 预载入关联查询(数据) * @access public - * @param Model $result 数据对象 - * @param string $relation 当前关联名 - * @param string $subRelation 子关联名 - * @param \Closure $closure 闭包 + * @param Model $result 数据对象 + * @param string $relation 当前关联名 + * @param string $subRelation 子关联名 + * @param \Closure $closure 闭包 * @return void */ public function eagerlyResult(&$result, $relation, $subRelation, $closure) @@ -145,7 +163,7 @@ abstract class OneToOne extends Relation /** * 保存(新增)当前关联数据对象 * @access public - * @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键 + * @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键 * @return integer */ public function save($data) @@ -162,8 +180,8 @@ abstract class OneToOne extends Relation /** * 设置预载入方式 * @access public - * @param integer $type 预载入方式 0 JOIN查询 1 IN查询 - * @return this + * @param integer $type 预载入方式 0 JOIN查询 1 IN查询 + * @return $this */ public function setEagerlyType($type) { @@ -185,8 +203,8 @@ abstract class OneToOne extends Relation /** * 绑定关联表的属性到父模型属性 * @access public - * @param mixed $attr 要绑定的属性列表 - * @return this + * @param mixed $attr 要绑定的属性列表 + * @return $this */ public function bind($attr) { @@ -200,19 +218,20 @@ abstract class OneToOne extends Relation /** * 关联统计 * @access public - * @param Model $result 数据对象 - * @param \Closure $closure 闭包 + * @param Model $result 数据对象 + * @param \Closure $closure 闭包 * @return integer */ public function relationCount($result, $closure) - {} + { + } /** * 一对一 关联模型预查询拼装 * @access public - * @param string $model 模型名称 - * @param string $relation 关联名 - * @param Model $result 模型对象实例 + * @param string $model 模型名称 + * @param string $relation 关联名 + * @param Model $result 模型对象实例 * @return void */ protected function match($model, $relation, &$result) @@ -239,10 +258,11 @@ abstract class OneToOne extends Relation /** * 绑定关联属性到父模型 * @access protected - * @param Model $model 关联模型对象 - * @param Model $result 父模型对象 - * @param array $bindAttr 绑定属性 + * @param Model $model 关联模型对象 + * @param Model $result 父模型对象 + * @param array $bindAttr 绑定属性 * @return void + * @throws Exception */ protected function bindAttr($model, &$result, $bindAttr) { @@ -259,19 +279,19 @@ abstract class OneToOne extends Relation /** * 一对一 关联模型预查询(IN方式) * @access public - * @param object $model 关联模型对象 - * @param array $where 关联预查询条件 - * @param string $key 关联键名 - * @param string $relation 关联名 - * @param string $subRelation 子关联 - * @param bool $closure + * @param object $model 关联模型对象 + * @param array $where 关联预查询条件 + * @param string $key 关联键名 + * @param string $relation 关联名 + * @param string $subRelation 子关联 + * @param bool|\Closure $closure * @return array */ protected function eagerlyWhere($model, $where, $key, $relation, $subRelation = '', $closure = false) { // 预载入关联查询 支持嵌套预载入 if ($closure) { - call_user_func_array($closure, [ & $model]); + call_user_func_array($closure, [& $model]); if ($field = $model->getOptions('with_field')) { $model->field($field)->removeOption('with_field'); } From c270a32b9f64aa978f5e5123fc099cf5c881e489 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 23 Jan 2017 13:58:30 +0800 Subject: [PATCH 070/125] =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/OneToOne.php | 1 - 1 file changed, 1 deletion(-) diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index ad5982d7..dd04edbc 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -73,7 +73,6 @@ abstract class OneToOne extends Relation // 预载入封装 $joinTable = $this->query->getTable(); - $joinName = Loader::parseName(basename(str_replace('\\', '/', $this->model))); $joinAlias = $relation; $query->via($joinAlias); From 86cc9378a0c46e66dabed6681f8b8de758585ae1 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 23 Jan 2017 13:59:21 +0800 Subject: [PATCH 071/125] =?UTF-8?q?collection=E5=8A=A9=E6=89=8B=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base.php | 2 +- helper.php | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/base.php b/base.php index fdbd5d90..cbe288bf 100644 --- a/base.php +++ b/base.php @@ -9,7 +9,7 @@ // | Author: liu21st // +---------------------------------------------------------------------- -define('THINK_VERSION', '5.0.5beta'); +define('THINK_VERSION', '5.0.5'); define('THINK_START_TIME', microtime(true)); define('THINK_START_MEM', memory_get_usage()); define('EXT', '.php'); diff --git a/helper.php b/helper.php index d5946fb2..543942be 100644 --- a/helper.php +++ b/helper.php @@ -14,7 +14,6 @@ //------------------------- use think\Cache; -use think\Collection; use think\Config; use think\Cookie; use think\Db; @@ -24,6 +23,7 @@ use think\exception\HttpResponseException; use think\Lang; use think\Loader; use think\Log; +use think\Model; use think\Request; use think\Response; use think\Session; @@ -571,10 +571,15 @@ if (!function_exists('collection')) { /** * 数组转换为数据集对象 * @param array $resultSet 数据集数组 - * @return Collection + * @return \think\model\Collection|\think\Collection */ function collection($resultSet) { - return new Collection($resultSet); + $item = current($resultSet); + if ($item instanceof Model) { + return \think\model\Collection::make($resultSet); + } else { + return \think\Collection::make($resultSet); + } } } From d8cc777e1dc768c038164271c18dc11f0980a183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B0=B4=E5=B9=B3=E5=87=A1?= Date: Tue, 24 Jan 2017 11:51:10 +0800 Subject: [PATCH 072/125] =?UTF-8?q?=E4=BF=AE=E6=AD=A3bindParam=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Connection.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index 6198f924..e9cfb19f 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -510,11 +510,13 @@ abstract class Connection protected function bindParam($bind) { foreach ($bind as $key => $val) { - if (is_numeric($key)) { - $key = $key + 1; + $param = is_numeric($key) ? $key + 1 : ':' . $key; + if (is_array($val)) { + array_unshift($val, $param); + $result = call_user_func_array([$this->PDOStatement, 'bindParam'], $val); + } else { + $result = $this->PDOStatement->bindValue($param, $val); } - array_unshift($val, $key); - $result = call_user_func_array([$this->PDOStatement, 'bindParam'], $val); if (!$result) { $param = array_shift($val); throw new BindParamException( From cc77fe99e3650e7a76f24a85f0e8ef93bd1374bf Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 24 Jan 2017 13:09:08 +0800 Subject: [PATCH 073/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BValidate=E7=B1=BB?= =?UTF-8?q?=E7=9A=84ip=E9=AA=8C=E8=AF=81=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Validate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/think/Validate.php b/library/think/Validate.php index ac9e6d55..9a6f58ab 100644 --- a/library/think/Validate.php +++ b/library/think/Validate.php @@ -568,7 +568,7 @@ class Validate break; case 'ip': // 是否为IP地址 - $result = $this->filter($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6); + $result = $this->filter($value, [FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6]); break; case 'url': // 是否为一个URL地址 From 33849140dacc6f42145c6dac48ef9dee9b653eb7 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 24 Jan 2017 16:44:50 +0800 Subject: [PATCH 074/125] =?UTF-8?q?=E4=BC=98=E5=8C=96Connection=E7=B1=BBcl?= =?UTF-8?q?ose=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Connection.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index e9cfb19f..b2d6a305 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -741,7 +741,10 @@ abstract class Connection */ public function close() { - $this->linkID = null; + $this->linkID = null; + $this->linkWrite = null; + $this->linkRead = null; + $this->links = []; } /** From 7d517177bc10d14b6cfdf9ca2192b97b4e552aa5 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 24 Jan 2017 16:49:20 +0800 Subject: [PATCH 075/125] =?UTF-8?q?=E5=88=A0=E9=99=A4Connection=E7=B1=BB?= =?UTF-8?q?=E5=BA=9F=E5=BC=83=E5=B1=9E=E6=80=A7resultSetType?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Connection.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index b2d6a305..f14e010b 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -50,8 +50,6 @@ abstract class Connection protected $linkRead; protected $linkWrite; - // 查询结果类型 - protected $resultSetType = 'array'; // 查询结果类型 protected $fetchType = PDO::FETCH_ASSOC; // 字段属性大小写 @@ -269,10 +267,7 @@ abstract class Connection } // 记录当前字段属性大小写设置 $this->attrCase = $params[PDO::ATTR_CASE]; - // 记录数据集返回类型 - if (isset($config['resultset_type'])) { - $this->resultSetType = $config['resultset_type']; - } + // 数据返回类型 if (isset($config['result_type'])) { $this->fetchType = $config['result_type']; From 2ef5da7b1bc01600575e4c31003125f5ee6826e6 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 26 Jan 2017 09:56:21 +0800 Subject: [PATCH 076/125] =?UTF-8?q?Cache=E7=B1=BBstore=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E5=85=81=E8=AE=B8=E4=B8=BA=E7=A9=BA=20?= =?UTF-8?q?=E8=A1=A8=E7=A4=BA=E8=8E=B7=E5=8F=96=E5=BD=93=E5=89=8D=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E9=A9=B1=E5=8A=A8=E5=8F=A5=E6=9F=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Cache.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/think/Cache.php b/library/think/Cache.php index 12107091..9e4b30ef 100644 --- a/library/think/Cache.php +++ b/library/think/Cache.php @@ -81,9 +81,9 @@ class Cache * @param string $name 缓存标识 * @return Driver */ - public static function store($name) + public static function store($name = '') { - if ('complex' == Config::get('cache.type')) { + if ('' !== $name && 'complex' == Config::get('cache.type')) { self::connect(Config::get('cache.' . $name), strtolower($name)); } return self::$handler; From 55b99355d79263f7a2c2118cf51e90afc733392c Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 26 Jan 2017 13:55:01 +0800 Subject: [PATCH 077/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BQuery=E7=B1=BB?= =?UTF-8?q?=E7=9A=84setBuilder=E6=96=B9=E6=B3=95=20=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E5=9B=A0=E8=87=AA=E5=AE=9A=E4=B9=89=E8=BF=9E=E6=8E=A5=E5=99=A8?= =?UTF-8?q?=E7=B1=BB=E5=90=8E=E6=89=BE=E4=B8=8D=E5=88=B0=E7=94=9F=E6=88=90?= =?UTF-8?q?=E5=99=A8=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Connection.php | 16 ++++++++++++++++ library/think/db/Query.php | 3 +-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index f14e010b..b1f730bf 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -58,6 +58,8 @@ abstract class Connection protected static $event = []; // 查询对象 protected $query = []; + // 使用Builder类 + protected $builder; // 数据库连接参数配置 protected $config = [ // 数据库类型 @@ -148,6 +150,20 @@ abstract class Connection return $this->query[$model]; } + /** + * 获取当前连接器类对应的Builder类 + * @access public + * @return string + */ + public function getBuilder() + { + if (!empty($this->builder)) { + return $this->builder; + } else { + return $this->getConfig('builder') ?: '\\think\\db\\builder\\' . ucfirst($this->getConfig('type')); + } + } + /** * 调用Query类的查询方法 * @access public diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 45cdd432..26a52c85 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -124,8 +124,7 @@ class Query */ protected function setBuilder() { - $builder = $this->connection->getConfig('builder') ?: $this->connection->getConfig('type'); - $class = false !== strpos($builder, '\\') ? $builder : '\\think\\db\\builder\\' . ucfirst($builder); + $class = $this->connection->getBuilder(); $this->builder = new $class($this->connection, $this); } From 13c7eb84524ad76b5261ada3fcbbc95b98300b06 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 26 Jan 2017 15:10:21 +0800 Subject: [PATCH 078/125] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E5=99=A8=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/connector/Mysql.php | 2 ++ library/think/db/connector/Pgsql.php | 1 + library/think/db/connector/Sqlite.php | 2 ++ library/think/db/connector/Sqlsrv.php | 2 +- 4 files changed, 6 insertions(+), 1 deletion(-) diff --git a/library/think/db/connector/Mysql.php b/library/think/db/connector/Mysql.php index 31435694..0a14e48e 100644 --- a/library/think/db/connector/Mysql.php +++ b/library/think/db/connector/Mysql.php @@ -21,6 +21,8 @@ use think\Log; class Mysql extends Connection { + protected $builder = '\\think\\db\\builder\\Mysql'; + /** * 解析pdo连接的dsn信息 * @access protected diff --git a/library/think/db/connector/Pgsql.php b/library/think/db/connector/Pgsql.php index 4f8756ca..04c3e782 100644 --- a/library/think/db/connector/Pgsql.php +++ b/library/think/db/connector/Pgsql.php @@ -19,6 +19,7 @@ use think\db\Connection; */ class Pgsql extends Connection { + protected $builder = '\\think\\db\\builder\\Pgsql'; /** * 解析pdo连接的dsn信息 diff --git a/library/think/db/connector/Sqlite.php b/library/think/db/connector/Sqlite.php index 4a08c740..a0e0873c 100644 --- a/library/think/db/connector/Sqlite.php +++ b/library/think/db/connector/Sqlite.php @@ -20,6 +20,8 @@ use think\db\Connection; class Sqlite extends Connection { + protected $builder = '\\think\\db\\builder\\Sqlite'; + /** * 解析pdo连接的dsn信息 * @access protected diff --git a/library/think/db/connector/Sqlsrv.php b/library/think/db/connector/Sqlsrv.php index 18148051..20d3491d 100644 --- a/library/think/db/connector/Sqlsrv.php +++ b/library/think/db/connector/Sqlsrv.php @@ -25,7 +25,7 @@ class Sqlsrv extends Connection PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_STRINGIFY_FETCHES => false, ]; - + protected $builder = '\\think\\db\\builder\\Sqlsrv'; /** * 解析pdo连接的dsn信息 * @access protected From 79fb22918164e3a197a7a7464e8690155fddfd58 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 26 Jan 2017 20:38:22 +0800 Subject: [PATCH 079/125] =?UTF-8?q?=E5=85=A8=E5=B1=80=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=B7=BB=E5=8A=A0=E6=8E=92=E9=99=A4=E8=A7=84?= =?UTF-8?q?=E5=88=99=20=E6=B7=BB=E5=8A=A0request=5Fcache=5Fexcept=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- convention.php | 2 ++ library/think/App.php | 4 ++-- library/think/Request.php | 8 +++++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/convention.php b/convention.php index c66ef583..8152066b 100644 --- a/convention.php +++ b/convention.php @@ -107,6 +107,8 @@ return [ 'request_cache' => false, // 请求缓存有效期 'request_cache_expire' => null, + // 全局请求缓存排除规则 + 'request_cache_except' => [], // +---------------------------------------------------------------------- // | 模板设置 diff --git a/library/think/App.php b/library/think/App.php index 77721112..963d8b35 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -118,7 +118,7 @@ class App // 监听app_begin Hook::listen('app_begin', $dispatch); // 请求缓存检查 - $request->cache($config['request_cache'], $config['request_cache_expire']); + $request->cache($config['request_cache'], $config['request_cache_expire'], $config['request_cache_except']); switch ($dispatch['type']) { case 'redirect': @@ -336,7 +336,7 @@ class App $request->module($module); $config = self::init($module); // 模块请求缓存检查 - $request->cache($config['request_cache'], $config['request_cache_expire']); + $request->cache($config['request_cache'], $config['request_cache_expire'], $config['request_cache_except']); } else { throw new HttpException(404, 'module not exists:' . $module); } diff --git a/library/think/Request.php b/library/think/Request.php index b72a5aaf..d39153fd 100644 --- a/library/think/Request.php +++ b/library/think/Request.php @@ -1502,9 +1502,10 @@ class Request * @access public * @param string $key 缓存标识,支持变量规则 ,例如 item/:name/:id * @param mixed $expire 缓存有效期 + * @param array $except 缓存排除 * @return void */ - public function cache($key, $expire = null) + public function cache($key, $expire = null, $except = []) { if (false !== $key && $this->isGet() && !$this->isCheckCache) { // 标记请求缓存检查 @@ -1516,6 +1517,11 @@ class Request if ($key instanceof \Closure) { $key = call_user_func_array($key, [$this]); } elseif (true === $key) { + foreach ($except as $rule) { + if (0 === strpos($this->url(), $rule)) { + return; + } + } // 自动缓存功能 $key = '__URL__'; } elseif (strpos($key, '|')) { From e192f97a6d2a4a5a524deaa08e39033e1876d3fc Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 29 Jan 2017 16:40:03 +0800 Subject: [PATCH 080/125] =?UTF-8?q?=E4=BF=AE=E6=AD=A3Query=E7=B1=BB?= =?UTF-8?q?=E7=9A=84update=E5=92=8Cdelete=E6=96=B9=E6=B3=95=E7=9A=84?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E7=BC=93=E5=AD=98=E6=B8=85=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 26a52c85..cd81a08b 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -2157,8 +2157,8 @@ class Query $options = $this->parseExpress(); $data = array_merge($options['data'], $data); $pk = $this->getPk($options); - if (isset($options['cache']) && is_string($options['cache'])) { - $key = $options['cache']; + if (isset($options['cache']) && is_string($options['cache']['key'])) { + $key = $options['cache']['key']; } if (empty($options['where'])) { @@ -2565,8 +2565,8 @@ class Query { // 分析查询表达式 $options = $this->parseExpress(); - if (isset($options['cache']) && is_string($options['cache'])) { - $key = $options['cache']; + if (isset($options['cache']) && is_string($options['cache']['key'])) { + $key = $options['cache']['key']; } if (!is_null($data) && true !== $data) { From 9a308b94e3d1a022c3c072a259f280420b813039 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 29 Jan 2017 17:01:51 +0800 Subject: [PATCH 081/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BQuery=E7=B1=BBdelete?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E7=9A=84=E7=BC=93=E5=AD=98=E6=A0=87=E8=AF=86?= =?UTF-8?q?=E8=AF=BB=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index cd81a08b..b6b8828e 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -2565,6 +2565,7 @@ class Query { // 分析查询表达式 $options = $this->parseExpress(); + $pk = $this->getPk($options); if (isset($options['cache']) && is_string($options['cache']['key'])) { $key = $options['cache']['key']; } @@ -2576,6 +2577,8 @@ class Query } // AR模式分析主键条件 $this->parsePkWhere($data, $options); + } elseif (is_string($pk) && isset($options['where']['AND'][$pk]) && is_scalar($options['where']['AND'][$pk])) { + $key = 'think:' . $options['table'] . '|' . $options['where']['AND'][$pk]; } if (true !== $data && empty($options['where'])) { From 47bfd562fee765c5613f41245945aeaa3445caa9 Mon Sep 17 00:00:00 2001 From: Jim Liu Date: Sun, 29 Jan 2017 17:24:06 +0800 Subject: [PATCH 082/125] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=97=AD=E5=8C=85?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E6=9D=A1=E4=BB=B6=E4=B8=BA=E7=A9=BA=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E7=9A=84=20sql=20=E8=AF=AD=E6=B3=95=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Builder.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/library/think/db/Builder.php b/library/think/db/Builder.php index 380fe239..9034076e 100644 --- a/library/think/db/Builder.php +++ b/library/think/db/Builder.php @@ -250,7 +250,10 @@ abstract class Builder // 使用闭包查询 $query = new Query($this->connection); call_user_func_array($value, [ & $query]); - $str[] = ' ' . $key . ' ( ' . $this->buildWhere($query->getOptions('where'), $options) . ' )'; + $whereClause = $this->buildWhere($query->getOptions('where'), $options); + if (!empty($whereClause)) { + $str[] = ' ' . $key . ' ( ' . $whereClause . ' )'; + } } elseif (strpos($field, '|')) { // 不同字段使用相同查询条件(OR) $array = explode('|', $field); From 8a06e801c8d491915e9464213dd6408273c25ae9 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 29 Jan 2017 18:45:56 +0800 Subject: [PATCH 083/125] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E7=B1=BB=E7=9A=84get=E5=92=8Call=E6=96=B9=E6=B3=95=20=E7=AC=AC?= =?UTF-8?q?=E4=BA=8C=E4=B8=AA=E5=8F=82=E6=95=B0=E4=B8=BAtrue=E6=88=96?= =?UTF-8?q?=E8=80=85=E6=95=B0=E5=AD=97=E8=A1=A8=E7=A4=BA=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 085ca053..86fcb88d 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -189,7 +189,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } // 全局作用域 if ($baseQuery && method_exists($this, 'base')) { - call_user_func_array([$this, 'base'], [& self::$links[$model]]); + call_user_func_array([$this, 'base'], [ & self::$links[$model]]); } // 返回当前模型的数据库查询对象 return self::$links[$model]; @@ -326,10 +326,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess break; } } elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), [ - 'datetime', - 'date', - 'timestamp' - ]) + 'datetime', + 'date', + 'timestamp', + ]) ) { $value = $this->formatDateTime($_SERVER['REQUEST_TIME'], $this->dateFormat); } else { @@ -439,10 +439,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $value = $this->readTransform($value, $this->type[$name]); } elseif (in_array($name, [$this->createTime, $this->updateTime])) { if (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), [ - 'datetime', - 'date', - 'timestamp' - ]) + 'datetime', + 'date', + 'timestamp', + ]) ) { $value = $this->formatDateTime(strtotime($value), $this->dateFormat); } else { @@ -1219,7 +1219,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess if (isset(self::$event[$this->class][$event])) { foreach (self::$event[$this->class][$event] as $callback) { if (is_callable($callback)) { - $result = call_user_func_array($callback, [& $params]); + $result = call_user_func_array($callback, [ & $params]); if (false === $result) { return false; } @@ -1275,6 +1275,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public static function get($data = null, $with = [], $cache = false) { + if (true === $with || is_int($with)) { + $cache = $with; + $with = []; + } $query = static::parseQuery($data, $with, $cache); return $query->find($data); } @@ -1290,6 +1294,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public static function all($data = null, $with = [], $cache = false) { + if (true === $with || is_int($with)) { + $cache = $with; + $with = []; + } $query = static::parseQuery($data, $with, $cache); return $query->select($data); } @@ -1309,7 +1317,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $result = $result->where($data); $data = null; } elseif ($data instanceof \Closure) { - call_user_func_array($data, [& $result]); + call_user_func_array($data, [ & $result]); $data = null; } elseif ($data instanceof Query) { $result = $data->with($with)->cache($cache); @@ -1332,7 +1340,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $query->where($data); $data = null; } elseif ($data instanceof \Closure) { - call_user_func_array($data, [& $query]); + call_user_func_array($data, [ & $query]); $data = null; } elseif (is_null($data)) { return 0; From ba4c5d39b4f80d8cee1588a5fa1d03107422714c Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 30 Jan 2017 08:33:55 +0800 Subject: [PATCH 084/125] =?UTF-8?q?Model=E7=B1=BB=E5=A2=9E=E5=8A=A0readonl?= =?UTF-8?q?y=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/library/think/Model.php b/library/think/Model.php index 86fcb88d..7a488c95 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1018,6 +1018,21 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return $this; } + /** + * 设置只读字段 + * @access public + * @param mixed $field 只读字段 + * @return $this + */ + public function readonly($field) + { + if (is_string($field)) { + $field = explode(',', $field); + } + $this->readonly = $field; + return $this; + } + /** * 是否为更新数据 * @access public From f12adbb94372a45959349a08e59ceb87e165a085 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 30 Jan 2017 13:31:05 +0800 Subject: [PATCH 085/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BQuery=E7=B1=BB?= =?UTF-8?q?=E7=9A=84=E8=87=AA=E5=8A=A8=E7=BC=93=E5=AD=98=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index b6b8828e..a9d1a543 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -2187,8 +2187,8 @@ class Query } else { $options['where']['AND'] = $where; } - } elseif (is_string($pk) && isset($options['where']['AND'][$pk]) && is_scalar($options['where']['AND'][$pk])) { - $key = 'think:' . $options['table'] . '|' . $options['where']['AND'][$pk]; + } elseif (!isset($key) && is_string($pk) && isset($options['where']['AND'][$pk])) { + $key = $this->getCacheKey($options['where']['AND'][$pk], $options); } // 生成UPDATE SQL语句 $sql = $this->builder->update($data, $options); @@ -2339,6 +2339,18 @@ class Query return $resultSet; } + protected function getCacheKey($value, $options) + { + if (is_scalar($value)) { + $data = $value; + } elseif (is_array($value) && 'eq' == strtolower($value[0])) { + $data = $value[1]; + } + if (isset($data)) { + return 'think:' . $options['table'] . '|' . $data; + } + } + /** * 查找单条记录 * @access public @@ -2358,10 +2370,12 @@ class Query } // 分析查询表达式 $options = $this->parseExpress(); - + $pk = $this->getPk($options); if (!is_null($data)) { // AR模式分析主键条件 $this->parsePkWhere($data, $options); + } elseif (!empty($options['cache']) && true === $options['cache']['key'] && is_string($pk) && isset($options['where']['AND'][$pk])) { + $key = $this->getCacheKey($options['where']['AND'][$pk], $options); } $options['limit'] = 1; @@ -2371,7 +2385,7 @@ 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; - } else { + } elseif (!isset($key)) { $key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options)); } $result = Cache::get($key); @@ -2577,8 +2591,8 @@ class Query } // AR模式分析主键条件 $this->parsePkWhere($data, $options); - } elseif (is_string($pk) && isset($options['where']['AND'][$pk]) && is_scalar($options['where']['AND'][$pk])) { - $key = 'think:' . $options['table'] . '|' . $options['where']['AND'][$pk]; + } elseif (!isset($key) && is_string($pk) && isset($options['where']['AND'][$pk])) { + $key = $this->getCacheKey($options['where']['AND'][$pk], $options); } if (true !== $data && empty($options['where'])) { From ff85b8c3cfdafa4428e4934c357c4479e1e41eb9 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 30 Jan 2017 14:29:12 +0800 Subject: [PATCH 086/125] =?UTF-8?q?=E6=94=B9=E8=BF=9Bafter=5Finsert?= =?UTF-8?q?=E5=9B=9E=E8=B0=83=E6=96=B9=E6=B3=95=E7=9A=84=E5=8F=82=E6=95=B0?= =?UTF-8?q?=20=E4=BC=A0=E5=85=A5=E6=9C=80=E7=BB=88=E7=9A=84data=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=20=E5=8C=85=E6=8B=AC=E8=87=AA=E5=A2=9EID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index a9d1a543..3736f85e 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -2070,11 +2070,17 @@ class Query // 执行操作 $result = $this->execute($sql, $bind); if ($result) { - $this->trigger('after_insert', $options); - } - if ($getLastInsID) { - $sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null); - return $this->getLastInsID($sequence); + $sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null); + $lastInsId = $this->getLastInsID($sequence); + if ($lastInsId) { + $pk = $this->getPk($options); + $data[$pk] = $lastInsId; + } + $this->trigger('after_insert', $data); + + if ($getLastInsID) { + return $lastInsId; + } } return $result; } @@ -2190,6 +2196,7 @@ class Query } elseif (!isset($key) && is_string($pk) && isset($options['where']['AND'][$pk])) { $key = $this->getCacheKey($options['where']['AND'][$pk], $options); } + // 生成UPDATE SQL语句 $sql = $this->builder->update($data, $options); // 获取参数绑定 @@ -2728,15 +2735,15 @@ class Query * 触发事件 * @access protected * @param string $event 事件名 - * @param mixed $options 当前查询参数 + * @param mixed $params 额外参数 * @return bool */ - protected function trigger($event, $options = []) + protected function trigger($event, $params = []) { $result = false; if (isset(self::$event[$event])) { $callback = self::$event[$event]; - $result = call_user_func_array($callback, [$options, $this]); + $result = call_user_func_array($callback, [$params, $this]); } return $result; } From 96238de8e3ac653d5c02376e26cee93e4bfa1015 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 30 Jan 2017 14:33:56 +0800 Subject: [PATCH 087/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BQuery=E7=B1=BBafter?= =?UTF-8?q?=5Fupdate=E5=9B=9E=E8=B0=83=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 1 + 1 file changed, 1 insertion(+) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 3736f85e..b5f0eb9a 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -2213,6 +2213,7 @@ class Query // 执行操作 $result = '' == $sql ? 0 : $this->execute($sql, $bind); if ($result) { + $options['data'] = $data; $this->trigger('after_update', $options); } return $result; From 85245fdf355eb1ced2b7b6e34839af2e0f972468 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 30 Jan 2017 14:37:28 +0800 Subject: [PATCH 088/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BQuery=E7=B1=BB?= =?UTF-8?q?=E7=9A=84after=5Finsert=E5=9B=9E=E8=B0=83=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index b5f0eb9a..65e1b07f 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -2076,7 +2076,8 @@ class Query $pk = $this->getPk($options); $data[$pk] = $lastInsId; } - $this->trigger('after_insert', $data); + $options['data'] = $data; + $this->trigger('after_insert', $options); if ($getLastInsID) { return $lastInsId; From e5b42f95c531b898e24a238b03759ac585cec17f Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 30 Jan 2017 14:53:16 +0800 Subject: [PATCH 089/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BQuery=E7=B1=BBafter?= =?UTF-8?q?=5Finsert=20after=5Fupdata=20after=5Fdelete=20before=5Fselect?= =?UTF-8?q?=20before=5Ffind=20=E5=9B=9E=E8=B0=83=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E5=8F=82=E6=95=B0=20=E7=BB=9F=E4=B8=80=E4=BC=A0?= =?UTF-8?q?=E5=85=A5$options['data']?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 65e1b07f..8b222bab 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -2214,6 +2214,14 @@ class Query // 执行操作 $result = '' == $sql ? 0 : $this->execute($sql, $bind); if ($result) { + if (is_string($pk)) { + if (isset($where[$pk])) { + $data[$pk] = $where[$pk]; + } elseif (isset($key) && strpos($key, '|')) { + list($a, $val) = explode('|', $key); + $data[$pk] = $val; + } + } $options['data'] = $data; $this->trigger('after_update', $options); } @@ -2287,6 +2295,8 @@ class Query // 获取实际执行的SQL语句 return $this->connection->getRealSql($sql, $bind); } + + $options['data'] = $data; if ($resultSet = $this->trigger('before_select', $options)) { } else { // 执行查询操作 @@ -2408,7 +2418,7 @@ class Query // 获取实际执行的SQL语句 return $this->connection->getRealSql($sql, $bind); } - + $options['data'] = $data; // 事件回调 if ($result = $this->trigger('before_find', $options)) { } else { @@ -2625,6 +2635,7 @@ class Query // 执行操作 $result = $this->execute($sql, $bind); if ($result) { + $options['data'] = $data; $this->trigger('after_delete', $options); } return $result; From aa7a5e8c324a456193fde66fd2e0ae9c9baf8475 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 30 Jan 2017 15:38:15 +0800 Subject: [PATCH 090/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BQuery=E7=B1=BB?= =?UTF-8?q?=E7=9A=84find=E6=96=B9=E6=B3=95=E6=95=B0=E6=8D=AE=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 54 ++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 8b222bab..ec0cf0cb 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -2214,13 +2214,11 @@ class Query // 执行操作 $result = '' == $sql ? 0 : $this->execute($sql, $bind); if ($result) { - if (is_string($pk)) { - if (isset($where[$pk])) { - $data[$pk] = $where[$pk]; - } elseif (isset($key) && strpos($key, '|')) { - list($a, $val) = explode('|', $key); - $data[$pk] = $val; - } + if (isset($where[$pk])) { + $data[$pk] = $where[$pk]; + } elseif (is_string($pk) && isset($key) && strpos($key, '|')) { + list($a, $val) = explode('|', $key); + $data[$pk] = $val; } $options['data'] = $data; $this->trigger('after_update', $options); @@ -2418,17 +2416,29 @@ class Query // 获取实际执行的SQL语句 return $this->connection->getRealSql($sql, $bind); } + if (is_string($pk)) { + if (!is_array($data)) { + if (isset($key) && strpos($key, '|')) { + list($a, $val) = explode('|', $key); + $item[$pk] = $val; + } else { + $item[$pk] = $data; + } + $data = $item; + } + } $options['data'] = $data; // 事件回调 if ($result = $this->trigger('before_find', $options)) { } else { // 执行查询 - $result = $this->query($sql, $bind, $options['master'], $options['fetch_pdo']); + $resultSet = $this->query($sql, $bind, $options['master'], $options['fetch_pdo']); - if ($result instanceof \PDOStatement) { + if ($resultSet instanceof \PDOStatement) { // 返回PDOStatement对象 - return $result; + return $resultSet; } + $result = isset($resultSet[0]) ? $resultSet[0] : []; } if (isset($cache)) { @@ -2442,32 +2452,31 @@ class Query } // 数据处理 - if (!empty($result[0])) { - $data = $result[0]; + if (!empty($result)) { if (!empty($this->model)) { // 返回模型对象 - $model = $this->model; - $data = new $model($data); - $data->isUpdate(true, isset($options['where']['AND']) ? $options['where']['AND'] : null); + $model = $this->model; + $result = new $model($result); + $result->isUpdate(true, isset($options['where']['AND']) ? $options['where']['AND'] : null); // 关联查询 if (!empty($options['relation'])) { - $data->relationQuery($options['relation']); + $result->relationQuery($options['relation']); } // 预载入查询 if (!empty($options['with'])) { - $data->eagerlyResult($data, $options['with']); + $result->eagerlyResult($result, $options['with']); } // 关联统计 if (!empty($options['with_count'])) { - $data->relationCount($data, $options['with_count']); + $result->relationCount($result, $options['with_count']); } } } elseif (!empty($options['fail'])) { $this->throwNotFound($options); } else { - $data = null; + $result = null; } - return $data; + return $result; } /** @@ -2635,6 +2644,11 @@ class Query // 执行操作 $result = $this->execute($sql, $bind); if ($result) { + if (!is_array($data) && is_string($pk) && isset($key) && strpos($key, '|')) { + list($a, $val) = explode('|', $key); + $item[$pk] = $val; + $data = $item; + } $options['data'] = $data; $this->trigger('after_delete', $options); } From 88dab633149d31b1486b29260d3145c254183360 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 30 Jan 2017 20:06:02 +0800 Subject: [PATCH 091/125] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=96=AD=E7=BA=BF?= =?UTF-8?q?=E9=87=8D=E8=BF=9E=E6=9C=BA=E5=88=B6=E5=92=8C=E5=BC=80=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Connection.php | 23 ++++++++++++++++++++++- library/think/db/connector/Mysql.php | 14 ++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index b1f730bf..0887093d 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -108,6 +108,8 @@ abstract class Connection 'builder' => '', // Query类 'query' => '\\think\\db\\Query', + // 是否需要断线重连 + 'break_reconnect' => false, ]; // PDO连接参数 @@ -391,6 +393,9 @@ abstract class Connection // 返回结果集 return $this->getResult($pdo, $procedure); } catch (\PDOException $e) { + if ($this->config['break_reconnect'] && $this->isBreak($e)) { + return $this->close()->query($sql, $bind, $master, $pdo); + } throw new PDOException($e, $this->config, $this->getLastsql()); } } @@ -446,6 +451,9 @@ abstract class Connection $this->numRows = $this->PDOStatement->rowCount(); return $this->numRows; } catch (\PDOException $e) { + if ($this->config['break_reconnect'] && $this->isBreak($e)) { + return $this->close()->execute($sql, $bind); + } throw new PDOException($e, $this->config, $this->getLastsql()); } } @@ -747,8 +755,9 @@ abstract class Connection } /** - * 关闭数据库 + * 关闭数据库(或者重新连接) * @access public + * @return $this */ public function close() { @@ -756,6 +765,18 @@ abstract class Connection $this->linkWrite = null; $this->linkRead = null; $this->links = []; + return $this; + } + + /** + * 是否断线 + * @access protected + * @param \PDOException $e 异常 + * @return bool + */ + protected function isBreak($e) + { + return false; } /** diff --git a/library/think/db/connector/Mysql.php b/library/think/db/connector/Mysql.php index 0a14e48e..0081fb2e 100644 --- a/library/think/db/connector/Mysql.php +++ b/library/think/db/connector/Mysql.php @@ -129,4 +129,18 @@ class Mysql extends Connection { return true; } + + /** + * 是否断线 + * @access protected + * @param \PDOException $e 异常对象 + * @return bool + */ + protected function isBreak($e) + { + if (false !== stripos($e->getMessage(), 'server has gone away')) { + return true; + } + return false; + } } From 570b5d1ed6a9093293016f2c4c3fce039e94ed62 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 30 Jan 2017 20:28:35 +0800 Subject: [PATCH 092/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BUrl=E7=B1=BB=E7=94=9F?= =?UTF-8?q?=E6=88=90=20root=E4=B8=BA/=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Url.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/think/Url.php b/library/think/Url.php index 51c846d7..d7dfd973 100644 --- a/library/think/Url.php +++ b/library/think/Url.php @@ -153,7 +153,8 @@ class Url // 检测域名 $domain = self::parseDomain($url, $domain); // URL组装 - $url = $domain . (self::$root ?: Request::instance()->root()) . '/' . ltrim($url, '/'); + $url = $domain . rtrim(self::$root ?: Request::instance()->root(), '/') . '/' . ltrim($url, '/'); + self::$bindCheck = false; return $url; } From a3aa8a6de86362a34ea56bb7d00305fe895b8673 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 31 Jan 2017 10:44:10 +0800 Subject: [PATCH 093/125] =?UTF-8?q?find=E6=96=B9=E6=B3=95=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E7=BC=93=E5=AD=98=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index ec0cf0cb..e3e74c38 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -2407,7 +2407,7 @@ class Query } $result = Cache::get($key); } - if (!$result) { + if (false === $result) { // 生成查询SQL $sql = $this->builder->select($options); // 获取参数绑定 @@ -2438,7 +2438,7 @@ class Query // 返回PDOStatement对象 return $resultSet; } - $result = isset($resultSet[0]) ? $resultSet[0] : []; + $result = isset($resultSet[0]) ? $resultSet[0] : null; } if (isset($cache)) { From 1d23f67e11179920cd751fe0299a372edc37a8a2 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 31 Jan 2017 10:47:17 +0800 Subject: [PATCH 094/125] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index e3e74c38..335fe493 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -2473,8 +2473,6 @@ class Query } } elseif (!empty($options['fail'])) { $this->throwNotFound($options); - } else { - $result = null; } return $result; } From 509905bbe4f4d2c96913fa3359cd2cf1ff2122dd Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 31 Jan 2017 16:20:37 +0800 Subject: [PATCH 095/125] =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/thinkphp/library/think/dbTest.php | 52 ++++++++++++------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/tests/thinkphp/library/think/dbTest.php b/tests/thinkphp/library/think/dbTest.php index 4d3d16d5..515f9424 100644 --- a/tests/thinkphp/library/think/dbTest.php +++ b/tests/thinkphp/library/think/dbTest.php @@ -140,7 +140,7 @@ EOF; public function testExecute() { $config = $this->getConfig(); - $sql = $this->getCreateTableSql(); + $sql = $this->getCreateTableSql(); foreach ($sql as $one) { Db::connect($config)->execute($one); } @@ -151,7 +151,7 @@ EOF; public function testQuery() { $config = $this->getConfig(); - $sql = $this->getCreateTableSql(); + $sql = $this->getCreateTableSql(); Db::connect($config)->batchQuery($sql); $tableQueryResult = Db::connect($config)->query("show tables;"); @@ -165,7 +165,7 @@ EOF; public function testBatchQuery() { $config = $this->getConfig(); - $sql = $this->getCreateTableSql(); + $sql = $this->getCreateTableSql(); Db::connect($config)->batchQuery($sql); $tableNum = Db::connect($config)->execute("show tables;"); @@ -174,28 +174,28 @@ EOF; public function testTable() { - $config = $this->getConfig(); + $config = $this->getConfig(); $tableName = 'tp_user'; - $result = Db::connect($config)->table($tableName); + $result = Db::connect($config)->table($tableName); $this->assertEquals($tableName, $result->getOptions()['table']); } public function testName() { - $config = $this->getConfig(); + $config = $this->getConfig(); $tableName = 'user'; - $result = Db::connect($config)->name($tableName); + $result = Db::connect($config)->name($tableName); $this->assertEquals($config['prefix'] . $tableName, $result->getOptions()['table']); } public function testInsert() { $config = $this->getConfig(); - $data = [ + $data = [ 'username' => 'chunice', 'password' => md5('chunice'), 'status' => 1, - 'create_time' => time() + 'create_time' => time(), ]; $result = Db::connect($config)->name('user')->insert($data); $this->assertEquals(1, $result); @@ -204,11 +204,11 @@ EOF; public function testUpdate() { $config = $this->getConfig(); - $data = [ + $data = [ 'username' => 'chunice_update', 'password' => md5('chunice'), 'status' => 1, - 'create_time' => time() + 'create_time' => time(), ]; $result = Db::connect($config)->name('user')->where('username', 'chunice')->update($data); $this->assertEquals(1, $result); @@ -216,7 +216,7 @@ EOF; public function testFind() { - $config = $this->getConfig(); + $config = $this->getConfig(); $mustFind = Db::connect($config)->name('user')->where('username', 'chunice_update')->find(); $this->assertNotEmpty($mustFind); $mustNotFind = Db::connect($config)->name('user')->where('username', 'chunice')->find(); @@ -229,7 +229,7 @@ EOF; $data = [ ['username' => 'foo', 'password' => md5('foo'), 'status' => 1, 'create_time' => time()], - ['username' => 'bar', 'password' => md5('bar'), 'status' => 1, 'create_time' => time()] + ['username' => 'bar', 'password' => md5('bar'), 'status' => 1, 'create_time' => time()], ]; $insertNum = Db::connect($config)->name('user')->insertAll($data); @@ -238,7 +238,7 @@ EOF; public function testSelect() { - $config = $this->getConfig(); + $config = $this->getConfig(); $mustFound = Db::connect($config)->name('user')->where('status', 1)->select(); $this->assertNotEmpty($mustFound); $mustNotFound = Db::connect($config)->name('user')->where('status', 0)->select(); @@ -247,7 +247,7 @@ EOF; public function testValue() { - $config = $this->getConfig(); + $config = $this->getConfig(); $username = Db::connect($config)->name('user')->where('id', 1)->value('username'); $this->assertEquals('chunice_update', $username); $usernameNull = Db::connect($config)->name('user')->where('id', 0)->value('username'); @@ -256,7 +256,7 @@ EOF; public function testColumn() { - $config = $this->getConfig(); + $config = $this->getConfig(); $username = Db::connect($config)->name('user')->where('status', 1)->column('username'); $this->assertNotEmpty($username); $usernameNull = Db::connect($config)->name('user')->where('status', 0)->column('username'); @@ -267,13 +267,13 @@ EOF; public function testInsertGetId() { $config = $this->getConfig(); - $id = Db::connect($config)->name('user')->order('id', 'desc')->value('id'); + $id = Db::connect($config)->name('user')->order('id', 'desc')->value('id'); $data = [ 'username' => uniqid(), 'password' => md5('chunice'), 'status' => 1, - 'create_time' => time() + 'create_time' => time(), ]; $lastId = Db::connect($config)->name('user')->insertGetId($data); $this->assertEquals($id + 1, $lastId); @@ -283,11 +283,11 @@ EOF; public function testGetLastInsId() { $config = $this->getConfig(); - $data = [ + $data = [ 'username' => uniqid(), 'password' => md5('chunice'), 'status' => 1, - 'create_time' => time() + 'create_time' => time(), ]; $lastId = Db::connect($config)->name('user')->insertGetId($data); @@ -308,7 +308,7 @@ EOF; public function testSetInc() { - $config = $this->getConfig(); + $config = $this->getConfig(); $originCreateTime = Db::connect($config)->name('user')->where('id', 1)->value('create_time'); Db::connect($config)->name('user')->where('id', 1)->setInc('create_time'); $newCreateTime = Db::connect($config)->name('user')->where('id', 1)->value('create_time'); @@ -318,7 +318,7 @@ EOF; public function testSetDec() { - $config = $this->getConfig(); + $config = $this->getConfig(); $originCreateTime = Db::connect($config)->name('user')->where('id', 1)->value('create_time'); Db::connect($config)->name('user')->where('id', 1)->setDec('create_time'); $newCreateTime = Db::connect($config)->name('user')->where('id', 1)->value('create_time'); @@ -341,12 +341,12 @@ EOF; public function testCache() { $config = $this->getConfig(); - $result = Db::connect($config)->name('user')->where('id', 1)->cache('key', 60)->select(); - $cache = \think\Cache::get('key'); + $result = Db::connect($config)->name('user')->where('id', 1)->cache('key', 60)->find(); + $cache = \think\Cache::get('key'); $this->assertEquals($result, $cache); - $updateCache = Db::connect($config)->name('user')->cache('key')->find(2); - $this->assertNotEquals($cache, $updateCache); + $updateCache = Db::connect($config)->name('user')->cache('key')->find(1); + $this->assertEquals($cache, $updateCache); } } From ca228203c91910aff3f44409db1b79a013355a5a Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 31 Jan 2017 16:50:38 +0800 Subject: [PATCH 096/125] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E5=A4=9A=E5=AF=B9?= =?UTF-8?q?=E5=A4=9A=E7=9A=84attach=E6=96=B9=E6=B3=95=E7=9A=84=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/BelongsToMany.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/library/think/model/relation/BelongsToMany.php b/library/think/model/relation/BelongsToMany.php index c85a8c0c..128287e0 100644 --- a/library/think/model/relation/BelongsToMany.php +++ b/library/think/model/relation/BelongsToMany.php @@ -54,7 +54,7 @@ class BelongsToMany extends Relation $localKey = $this->localKey; $middle = $this->middle; if ($closure) { - call_user_func_array($closure, [& $this->query]); + call_user_func_array($closure, [ & $this->query]); } // 关联查询 $pk = $this->parent->getPk(); @@ -174,8 +174,8 @@ class BelongsToMany extends Relation return $this->belongsToManyQuery($this->middle, $this->foreignKey, $this->localKey, [ 'pivot.' . $this->localKey => [ 'exp', - '=' . $this->parent->getTable() . '.' . $this->parent->getPk() - ] + '=' . $this->parent->getTable() . '.' . $this->parent->getPk(), + ], ])->fetchSql()->count(); } @@ -271,7 +271,7 @@ class BelongsToMany extends Relation * @access public * @param mixed $data 数据 可以使用数组、关联模型对象 或者 关联对象的主键 * @param array $pivot 中间表额外数据 - * @return int + * @return array|Pivot * @throws Exception */ public function attach($data, $pivot = []) @@ -301,7 +301,12 @@ class BelongsToMany extends Relation $ids = (array) $id; foreach ($ids as $id) { $pivot[$this->foreignKey] = $id; - $result = $this->query->table($this->middle)->insert($pivot, true); + $this->query->table($this->middle)->insert($pivot, true); + $result[] = new Pivot($pivot, $this->middle); + } + if (count($result) == 1) { + // 返回中间表模型对象 + $result = $result[0]; } return $result; } else { From 3f0fa05a7cf7b19e165760309af76a3d5c4c9e7d Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 2 Feb 2017 14:26:06 +0800 Subject: [PATCH 097/125] =?UTF-8?q?redirect=E5=8A=A9=E6=89=8B=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=92=8Ccontroller=E7=B1=BB=E7=9A=84redirect=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=A2=9E=E5=8A=A0with=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper.php | 5 +++-- library/traits/controller/Jump.php | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/helper.php b/helper.php index 543942be..da0dc9c5 100644 --- a/helper.php +++ b/helper.php @@ -495,15 +495,16 @@ if (!function_exists('redirect')) { * @param mixed $url 重定向地址 支持Url::build方法的地址 * @param array|integer $params 额外参数 * @param integer $code 状态码 + * @param array $with 隐式传参 * @return \think\response\Redirect */ - function redirect($url = [], $params = [], $code = 302) + function redirect($url = [], $params = [], $code = 302, $with = []) { if (is_integer($params)) { $code = $params; $params = []; } - return Response::create($url, 'redirect', $code)->params($params); + return Response::create($url, 'redirect', $code)->params($params)->with($with); } } diff --git a/library/traits/controller/Jump.php b/library/traits/controller/Jump.php index 51f4281f..6e6f2dec 100644 --- a/library/traits/controller/Jump.php +++ b/library/traits/controller/Jump.php @@ -131,16 +131,17 @@ trait Jump * @param string $url 跳转的URL表达式 * @param array|integer $params 其它URL参数 * @param integer $code http code + * @param array $with 隐式传参 * @return void */ - protected function redirect($url, $params = [], $code = 302) + protected function redirect($url, $params = [], $code = 302, $with = []) { $response = new Redirect($url); if (is_integer($params)) { $code = $params; $params = []; } - $response->code($code)->params($params); + $response->code($code)->params($params)->with($with); throw new HttpResponseException($response); } From bd3bce86f0cf1481dda824882bdfebdea7aab3f5 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 3 Feb 2017 17:37:37 +0800 Subject: [PATCH 098/125] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E6=83=AF=E4=BE=8B=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=20=E9=81=BF=E5=85=8D=E4=BD=BF=E7=94=A8=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E6=89=A9=E5=B1=95=E7=9A=84=E6=97=B6=E5=80=99=E5=BD=B1?= =?UTF-8?q?=E5=93=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- convention.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/convention.php b/convention.php index 8152066b..b6f23e28 100644 --- a/convention.php +++ b/convention.php @@ -274,10 +274,6 @@ return [ 'datetime_format' => 'Y-m-d H:i:s', // 是否需要进行SQL性能分析 'sql_explain' => false, - // Builder类 - 'builder' => '', - // Query类 - 'query' => '\\think\\db\\Query', ], //分页配置 From fb8415e653ddb83dab28e7d853b6646e082870a2 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 4 Feb 2017 14:13:44 +0800 Subject: [PATCH 099/125] =?UTF-8?q?Query=E7=B1=BB=E5=A2=9E=E5=8A=A0cacheDa?= =?UTF-8?q?ta=E6=96=B9=E6=B3=95=E7=94=A8=E4=BA=8E=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E7=BB=9F=E4=B8=80=E5=86=99=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 46 +++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 335fe493..3d080dcc 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -419,11 +419,7 @@ class Query } if (isset($cache)) { // 缓存数据 - if (isset($cache['tag'])) { - Cache::tag($cache['tag'])->set($key, $result, $cache['expire']); - } else { - Cache::set($key, $result, $cache['expire']); - } + $this->cacheData($key, $result, $cache); } } else { // 清空查询条件 @@ -491,11 +487,7 @@ class Query } if (isset($cache) && isset($guid)) { // 缓存数据 - if (isset($cache['tag'])) { - Cache::tag($cache['tag'])->set($guid, $result, $cache['expire']); - } else { - Cache::set($guid, $result, $cache['expire']); - } + $this->cacheData($guid, $result, $cache); } } else { // 清空查询条件 @@ -2308,11 +2300,7 @@ class Query if (isset($cache)) { // 缓存数据集 - if (isset($cache['tag'])) { - Cache::tag($cache['tag'])->set($key, $resultSet, $cache['expire']); - } else { - Cache::set($key, $resultSet, $cache['expire']); - } + $this->cacheData($key, $resultSet, $cache); } } @@ -2356,6 +2344,28 @@ class Query return $resultSet; } + /** + * 缓存数据 + * @access public + * @param string $key 缓存标识 + * @param mixed $data 缓存数据 + * @param array $config 缓存参数 + */ + protected function cacheData($key, $data, $config = []) + { + if (isset($config['tag'])) { + Cache::tag($config['tag'])->set($key, $data, $config['expire']); + } else { + Cache::set($key, $data, $config['expire']); + } + } + + /** + * 生成缓存标识 + * @access public + * @param mixed $value 缓存数据 + * @param array $options 缓存参数 + */ protected function getCacheKey($value, $options) { if (is_scalar($value)) { @@ -2443,11 +2453,7 @@ class Query if (isset($cache)) { // 缓存数据 - if (isset($cache['tag'])) { - Cache::tag($cache['tag'])->set($key, $result, $cache['expire']); - } else { - Cache::set($key, $result, $cache['expire']); - } + $this->cacheData($key, $result, $cache); } } From fcfe91a97d5e06b41d4763775e0f72b1bb206f22 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 4 Feb 2017 20:08:30 +0800 Subject: [PATCH 100/125] =?UTF-8?q?=E4=BF=AE=E6=AD=A3hasMany=E5=85=B3?= =?UTF-8?q?=E8=81=94=E7=9A=84has=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/HasMany.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index 3094bb55..9bcdcead 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -45,7 +45,7 @@ class HasMany 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(); } @@ -125,7 +125,7 @@ class HasMany extends Relation $count = 0; if (isset($result->$localKey)) { if ($closure) { - call_user_func_array($closure, [& $this->query]); + call_user_func_array($closure, [ & $this->query]); } $count = $this->query->where([$this->foreignKey => $result->$localKey])->count(); } @@ -141,14 +141,14 @@ class HasMany 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->foreignKey => [ 'exp', - '=' . $this->parent->getTable() . '.' . $this->parent->getPk() - ] + '=' . $this->parent->getTable() . '.' . $this->parent->getPk(), + ], ])->fetchSql()->count(); } @@ -167,7 +167,7 @@ class HasMany extends Relation $foreignKey = $this->foreignKey; // 预载入关联查询 支持嵌套预载入 if ($closure) { - call_user_func_array($closure, [& $model]); + call_user_func_array($closure, [ & $model]); } $list = $model->where($where)->with($subRelation)->select(); @@ -217,13 +217,14 @@ class HasMany extends Relation * @param string $operator 比较操作符 * @param integer $count 个数 * @param string $id 关联表的统计字段 + * @param string $joinType JOIN类型 * @return Query */ - public function has($operator = '>=', $count = 1, $id = '*') + public function has($operator = '>=', $count = 1, $id = '*', $joinType = 'INNER') { $table = $this->query->getTable(); return $this->parent->db()->alias('a') - ->join($table . ' b', 'a.' . $this->localKey . '=b.' . $this->foreignKey, $this->joinType) + ->join($table . ' b', 'a.' . $this->localKey . '=b.' . $this->foreignKey, $joinType) ->group('b.' . $this->foreignKey) ->having('count(' . $id . ')' . $operator . $count); } From 113616fa0f46263cf4e5cd44320e86f895941bbd Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 5 Feb 2017 12:30:46 +0800 Subject: [PATCH 101/125] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E7=B1=BB=E7=9A=84ip=E9=AA=8C=E8=AF=81=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Validate.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/think/Validate.php b/library/think/Validate.php index 9a6f58ab..2f728a60 100644 --- a/library/think/Validate.php +++ b/library/think/Validate.php @@ -656,7 +656,7 @@ class Validate if (!in_array($rule, ['ipv4', 'ipv6'])) { $rule = 'ipv4'; } - return $this->filter($value, FILTER_VALIDATE_IP, 'ipv6' == $rule ? FILTER_FLAG_IPV6 : FILTER_FLAG_IPV4); + return $this->filter($value, [FILTER_VALIDATE_IP, 'ipv6' == $rule ? FILTER_FLAG_IPV6 : FILTER_FLAG_IPV4]); } /** @@ -872,6 +872,7 @@ class Validate list($rule, $param) = explode(',', $rule); } elseif (is_array($rule)) { $param = isset($rule[1]) ? $rule[1] : null; + $rule = $rule[0]; } else { $param = null; } From 0b8d05efdbbfa82448400b3c2f6c4c718ed03b0a Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 5 Feb 2017 14:26:15 +0800 Subject: [PATCH 102/125] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=8F=82=E6=95=B0=20datetime=5Fformat=20=E4=B8=BAfals?= =?UTF-8?q?e=E5=88=99=E5=85=B3=E9=97=AD=E6=97=B6=E9=97=B4=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E8=87=AA=E5=8A=A8=E6=A0=BC=E5=BC=8F=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/think/Model.php b/library/think/Model.php index 7a488c95..61baa3a7 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -350,7 +350,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess { if (false !== strpos($format, '\\')) { $time = new $format($time); - } elseif (!$timestamp) { + } elseif (!$timestamp && false !== $format) { $time = date($format, $time); } return $time; From 002cd48d5478127c0a178bda1fe2044a29ac36be Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 6 Feb 2017 14:07:12 +0800 Subject: [PATCH 103/125] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E8=BD=AF=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E7=9A=84=E5=85=BC=E5=AE=B9=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/traits/model/SoftDelete.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/traits/model/SoftDelete.php b/library/traits/model/SoftDelete.php index 8781544f..720b6e88 100644 --- a/library/traits/model/SoftDelete.php +++ b/library/traits/model/SoftDelete.php @@ -42,7 +42,7 @@ trait SoftDelete { $model = new static(); $field = $model->getDeleteTimeField(true); - return $model->db(false)->where($field, 'exp', 'is not null'); + return $model->db(false)->whereNotNull($field); } /** @@ -129,7 +129,7 @@ trait SoftDelete protected function base($query) { $field = $this->getDeleteTimeField(true); - $query->where($field, 'null'); + $query->whereNull($field); } /** From 742e2c3de33469a19f07f54d330c4ee4f172cfd5 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 6 Feb 2017 18:00:35 +0800 Subject: [PATCH 104/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BModel=E7=B1=BB?= =?UTF-8?q?=E7=9A=84has=E5=92=8ChasWhere=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 61baa3a7..29ed3afa 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1422,15 +1422,20 @@ 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(); - if (is_array($operator) || $operator instanceof \Closure) { - return $model->$relation()->hasWhere($operator); + $model = new static(); + $relation = $model->$relation(); + if ($relation instanceof HasMany) { + if (is_array($operator) || $operator instanceof \Closure) { + return $relation->hasWhere($operator); + } + return $relation->has($operator, $count, $id); + } else { + return $relation; } - return $model->$relation()->has($operator, $count, $id); } /** @@ -1438,12 +1443,17 @@ 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); + $model = new static(); + $relation = $model->$relation(); + if ($relation instanceof HasMany) { + return $model->$relation()->hasWhere($where); + } else { + return $relation; + } } /** From 719a1547d0e304cba2fd4d37c15712de6bc8d6f8 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 7 Feb 2017 15:55:12 +0800 Subject: [PATCH 105/125] =?UTF-8?q?=E7=89=88=E6=9C=AC=E6=9B=B4=E6=96=B0=20?= =?UTF-8?q?=E5=8F=AF=E6=97=A0=E7=BC=9D=E5=8D=87=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base.php b/base.php index cbe288bf..63fc16c3 100644 --- a/base.php +++ b/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'); From 7f6102d12e33e2f1beda028fcaecdad665c53622 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 7 Feb 2017 16:03:44 +0800 Subject: [PATCH 106/125] =?UTF-8?q?readme=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d81d68c8..ae209028 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ ThinkPHP5在保持快速开发和大道至简的核心理念不变的同时,PH + 真正惰性加载 + 分布式环境支持 + 支持Composer + + 支持MongoDb > ThinkPHP5的运行环境要求PHP5.4以上。 From a3a37e6cfad132d5b02ffff8f23a4abc565f7928 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 7 Feb 2017 17:39:59 +0800 Subject: [PATCH 107/125] =?UTF-8?q?=E4=BF=AE=E6=AD=A3Model=E7=B1=BBhasWher?= =?UTF-8?q?e=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/think/Model.php b/library/think/Model.php index 29ed3afa..4a73bf39 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1450,7 +1450,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $model = new static(); $relation = $model->$relation(); if ($relation instanceof HasMany) { - return $model->$relation()->hasWhere($where); + return $relation->hasWhere($where); } else { return $relation; } From 550de110fedf52349a82b30c2735815925ed6468 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 7 Feb 2017 22:23:57 +0800 Subject: [PATCH 108/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BModel=E7=B1=BBrelatio?= =?UTF-8?q?nQuery=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/library/think/Model.php b/library/think/Model.php index 4a73bf39..d4db7813 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1492,6 +1492,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 支持闭包查询过滤关联条件 $closure = $relation; $relation = $key; + } elseif (is_array($relation)) { + $subRelation = $relation; + $relation = $key; } if (strpos($relation, '.')) { list($relation, $subRelation) = explode('.', $relation, 2); From 669f70acf0947c6ac99633a3d695e6ad0dc18749 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 7 Feb 2017 22:32:20 +0800 Subject: [PATCH 109/125] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E5=85=B3=E8=81=94?= =?UTF-8?q?=E7=94=A8=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index d4db7813..c9389356 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1492,11 +1492,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 支持闭包查询过滤关联条件 $closure = $relation; $relation = $key; - } elseif (is_array($relation)) { + } + if (is_array($relation)) { $subRelation = $relation; $relation = $key; - } - if (strpos($relation, '.')) { + } elseif (strpos($relation, '.')) { list($relation, $subRelation) = explode('.', $relation, 2); } $method = Loader::parseName($relation, 1, false); @@ -1522,7 +1522,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); @@ -1548,7 +1551,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); From 22b4fff3ddb391bcb5a65a38bc08f02848be55a2 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 7 Feb 2017 22:49:00 +0800 Subject: [PATCH 110/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BwithCount=E7=94=A8?= =?UTF-8?q?=E6=B3=95=E6=94=AF=E6=8C=81=E6=8C=87=E5=AE=9A=E7=BB=9F=E8=AE=A1?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/library/think/Model.php b/library/think/Model.php index c9389356..ed846027 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1578,10 +1578,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); } } From 13924b5a8b887a49445270227547a38c183e845e Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 8 Feb 2017 18:06:12 +0800 Subject: [PATCH 111/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BQuery=E7=B1=BB?= =?UTF-8?q?=E7=9A=84insert=E6=96=B9=E6=B3=95=E4=B8=80=E5=A4=84=E5=8F=AF?= =?UTF-8?q?=E8=83=BD=E5=AD=98=E5=9C=A8=E7=9A=84=E8=AD=A6=E5=91=8A=E9=94=99?= =?UTF-8?q?=E8=AF=AF=20=E6=94=B9=E8=BF=9BModel=E7=B1=BB=E4=B8=80=E5=A4=84C?= =?UTF-8?q?ollection=E7=9A=84use?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 12 ++++++------ library/think/db/Query.php | 6 ++++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index ed846027..8da1ce4a 100644 --- a/library/think/Model.php +++ b/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; @@ -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); diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 3d080dcc..9530013e 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -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); From cc9aa76f311afea7e8c5d5a092505146edb3b0f5 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 8 Feb 2017 18:51:20 +0800 Subject: [PATCH 112/125] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E7=9A=84ext=E5=92=8Cdeny=5Fext=E5=8F=82=E6=95=B0=20=E5=85=81?= =?UTF-8?q?=E8=AE=B8=E8=AE=BE=E7=BD=AE=E4=B8=BA=E7=A9=BA=20=E8=A1=A8?= =?UTF-8?q?=E7=A4=BA=E4=B8=8D=E5=85=81=E8=AE=B8=E4=BB=BB=E4=BD=95=E5=90=8E?= =?UTF-8?q?=E7=BC=80=E6=88=96=E8=80=85=E5=BF=85=E9=A1=BB=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E5=90=8E=E7=BC=80=E8=AE=BF=E9=97=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Route.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/think/Route.php b/library/think/Route.php index 1629d636..4a630adc 100644 --- a/library/think/Route.php +++ b/library/think/Route.php @@ -1126,8 +1126,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检测 From 89ee5379b7f909e6c9f88957f5f7f22596db68d2 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 8 Feb 2017 19:02:06 +0800 Subject: [PATCH 113/125] =?UTF-8?q?=E6=B8=85=E7=A9=BA=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=88=A0=E9=99=A4=E7=A9=BA=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/cache/driver/File.php | 1 + 1 file changed, 1 insertion(+) diff --git a/library/think/cache/driver/File.php b/library/think/cache/driver/File.php index dba02c3e..838025e0 100644 --- a/library/think/cache/driver/File.php +++ b/library/think/cache/driver/File.php @@ -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); } From 1cdb0ba6934e3729ca12f3c8971b9fcb23b5ca21 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 9 Feb 2017 10:29:45 +0800 Subject: [PATCH 114/125] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E8=AF=B7=E6=B1=82=E7=BC=93=E5=AD=98=E5=AF=B9?= =?UTF-8?q?=E5=AD=90=E5=9F=9F=E5=90=8D=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Request.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/think/Request.php b/library/think/Request.php index d39153fd..9c8ae276 100644 --- a/library/think/Request.php +++ b/library/think/Request.php @@ -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, ':')) { From 4824c43e903e10460e34ad909e5f236102372036 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 9 Feb 2017 11:39:56 +0800 Subject: [PATCH 115/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BURl=E7=94=9F=E6=88=90?= =?UTF-8?q?=E5=AF=B9ext=E5=8F=82=E6=95=B0=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Route.php | 8 +++++--- library/think/Url.php | 11 +++++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/library/think/Route.php b/library/think/Route.php index 4a630adc..679a1372 100644 --- a/library/think/Route.php +++ b/library/think/Route.php @@ -304,8 +304,9 @@ 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 ($group) { if ('*' != $type) { @@ -447,7 +448,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]; } diff --git a/library/think/Url.php b/library/think/Url.php index d7dfd973..520e664a 100644 --- a/library/think/Url.php +++ b/library/think/Url.php @@ -80,6 +80,9 @@ class Url if (!empty($match[1])) { $domain = $match[1]; } + if (!empty($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; } From f2b4a0680efe08dd7a2834bea074bacb14f091e0 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 9 Feb 2017 11:44:05 +0800 Subject: [PATCH 116/125] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/thinkphp/library/think/urlTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/thinkphp/library/think/urlTest.php b/tests/thinkphp/library/think/urlTest.php index acb43365..1cb64084 100644 --- a/tests/thinkphp/library/think/urlTest.php +++ b/tests/thinkphp/library/think/urlTest.php @@ -88,7 +88,7 @@ class urlTest extends \PHPUnit_Framework_TestCase public function testBuildNameRoute() { Route::get(['name', 'blog/:id'], 'index/blog'); - $this->assertEquals([['blog/:id', ['id' => 1], null]], Route::name('name')); + $this->assertEquals([['blog/:id', ['id' => 1], null, null]], Route::name('name')); Config::set('url_html_suffix', 'shtml'); $this->assertEquals('/blog/10.shtml', Url::build('name?id=10')); } From a8a903bfd4df2c5229f1e501efd65612f5bf8215 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 9 Feb 2017 11:59:34 +0800 Subject: [PATCH 117/125] =?UTF-8?q?=E6=94=B9=E8=BF=9Burl=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Url.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/think/Url.php b/library/think/Url.php index 520e664a..de3cd912 100644 --- a/library/think/Url.php +++ b/library/think/Url.php @@ -80,7 +80,7 @@ class Url if (!empty($match[1])) { $domain = $match[1]; } - if (!empty($match[2])) { + if (!is_null($match[2])) { $suffix = $match[2]; } } elseif (!empty($rule) && isset($name)) { From 2639db4f9e6d53a1045d36b6d5d811ddd05bd2f7 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 9 Feb 2017 12:21:50 +0800 Subject: [PATCH 118/125] =?UTF-8?q?=E4=BF=AE=E6=AD=A3BelongsTo=E5=85=B3?= =?UTF-8?q?=E8=81=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/BelongsTo.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library/think/model/relation/BelongsTo.php b/library/think/model/relation/BelongsTo.php index d1c4b9bd..1997badc 100644 --- a/library/think/model/relation/BelongsTo.php +++ b/library/think/model/relation/BelongsTo.php @@ -85,10 +85,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 +116,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)) { // 绑定关联属性 From a01c43472d6714dc61396dc20fb9bbd030256cf6 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 9 Feb 2017 14:26:36 +0800 Subject: [PATCH 119/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BModel=E7=B1=BB?= =?UTF-8?q?=E7=9A=84has=E5=92=8ChasWhere=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lang/zh-cn.php | 1 + library/think/Model.php | 21 +++------- library/think/model/relation/BelongsTo.php | 39 ++++++++++++++++++ .../think/model/relation/BelongsToMany.php | 25 ++++++++++++ .../think/model/relation/HasManyThrough.php | 28 ++++++++++++- library/think/model/relation/HasOne.php | 18 ++++++++- library/think/model/relation/MorphMany.php | 40 +++++++++++++++---- library/think/model/relation/MorphTo.php | 25 ++++++++++++ 8 files changed, 172 insertions(+), 25 deletions(-) diff --git a/lang/zh-cn.php b/lang/zh-cn.php index b837bc41..c6c5a50f 100644 --- a/lang/zh-cn.php +++ b/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/library/think/Model.php b/library/think/Model.php index 8da1ce4a..1222b548 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1426,16 +1426,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public static function has($relation, $operator = '>=', $count = 1, $id = '*') { - $model = new static(); - $relation = $model->$relation(); - if ($relation instanceof HasMany) { - if (is_array($operator) || $operator instanceof \Closure) { - return $relation->hasWhere($operator); - } - return $relation->has($operator, $count, $id); - } else { - return $relation; + $relation = (new static())->$relation(); + if (is_array($operator) || $operator instanceof \Closure) { + return $relation->hasWhere($operator); } + return $relation->has($operator, $count, $id); } /** @@ -1447,13 +1442,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public static function hasWhere($relation, $where = []) { - $model = new static(); - $relation = $model->$relation(); - if ($relation instanceof HasMany) { - return $relation->hasWhere($where); - } else { - return $relation; - } + return (new static())->$relation()->hasWhere($where); } /** diff --git a/library/think/model/relation/BelongsTo.php b/library/think/model/relation/BelongsTo.php index 1997badc..5cee16d8 100644 --- a/library/think/model/relation/BelongsTo.php +++ b/library/think/model/relation/BelongsTo.php @@ -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 diff --git a/library/think/model/relation/BelongsToMany.php b/library/think/model/relation/BelongsToMany.php index 128287e0..691ad846 100644 --- a/library/think/model/relation/BelongsToMany.php +++ b/library/think/model/relation/BelongsToMany.php @@ -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/library/think/model/relation/HasManyThrough.php b/library/think/model/relation/HasManyThrough.php index e1518972..04b71e04 100644 --- a/library/think/model/relation/HasManyThrough.php +++ b/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; @@ -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/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index 2c0cf350..a2ff72be 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -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'])->whereExp('a.' . $localKey, '=b.' . $foreignKey); + }); + } + /** * 根据关联条件查询当前模型 * @access public diff --git a/library/think/model/relation/MorphMany.php b/library/think/model/relation/MorphMany.php index 3fc8179c..4dbd2ef5 100644 --- a/library/think/model/relation/MorphMany.php +++ b/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; @@ -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/library/think/model/relation/MorphTo.php b/library/think/model/relation/MorphTo.php index 5e1867f2..04df0e79 100644 --- a/library/think/model/relation/MorphTo.php +++ b/library/think/model/relation/MorphTo.php @@ -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 From 532173e0105976c742d6b15ca6c4feb920cb9928 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 9 Feb 2017 14:42:39 +0800 Subject: [PATCH 120/125] =?UTF-8?q?=E4=BC=98=E5=8C=96HasOne=E7=9A=84has?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/HasOne.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index a2ff72be..9d4f5f79 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -65,7 +65,7 @@ class HasOne extends OneToOne $foreignKey = $this->foreignKey; return $this->parent->db()->alias('a') ->whereExists(function ($query) use ($table, $localKey, $foreignKey) { - $query->table([$table => 'b'])->whereExp('a.' . $localKey, '=b.' . $foreignKey); + $query->table([$table => 'b'])->field('b.' . $foreignKey)->whereExp('a.' . $localKey, '=b.' . $foreignKey); }); } From 76d78048a2c17b554b5b95b845e7f996270a7592 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 9 Feb 2017 16:03:52 +0800 Subject: [PATCH 121/125] =?UTF-8?q?readme=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ae209028..4b0eb838 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ ThinkPHP5在保持快速开发和大道至简的核心理念不变的同时,PH > ThinkPHP5的运行环境要求PHP5.4以上。 -详细开发文档参考 [ThinkPHP5完全开发手册](http://www.kancloud.cn/manual/thinkphp5) +详细开发文档参考 [ThinkPHP5完全开发手册](http://www.kancloud.cn/manual/thinkphp5) 以及[ThinkPHP5入门系列教程](http://www.kancloud.cn/special/thinkphp5_quickstart) ## 目录结构 From 0d48f4a80c6fd87b10d871ab1647628ebebf68fb Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 9 Feb 2017 16:28:10 +0800 Subject: [PATCH 122/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BQuery=E7=B1=BB?= =?UTF-8?q?=E7=9A=84find=E6=96=B9=E6=B3=95=E7=9A=84=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 9530013e..5473b688 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -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'); } @@ -2414,8 +2414,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); } From 1e1f36eb40f950de86330bbf3d2420e34ad81bfc Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 9 Feb 2017 17:49:47 +0800 Subject: [PATCH 123/125] =?UTF-8?q?=E6=94=B9=E8=BF=9BQuery=E7=B1=BB?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=9B=B4=E6=96=B0=20=E4=BF=AE=E6=AD=A3Connec?= =?UTF-8?q?tion=E7=B1=BB=E4=B8=80=E5=A4=84=E5=8F=AF=E8=83=BD=E7=9A=84?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Connection.php | 4 ++-- library/think/db/Query.php | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index 0887093d..f166e916 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -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/library/think/db/Query.php b/library/think/db/Query.php index 5473b688..5e258d71 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -2204,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); @@ -2377,6 +2379,8 @@ class Query } if (isset($data)) { return 'think:' . $options['table'] . '|' . $data; + } else { + return md5(serialize($options)); } } @@ -2648,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); From 2c0ee2f616ea714e8e5e9f55daecfc3a9515c4ae Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 9 Feb 2017 19:12:45 +0800 Subject: [PATCH 124/125] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E8=BD=AF=E5=88=A0?= =?UTF-8?q?=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/builder/Mysql.php | 2 ++ library/think/db/builder/Pgsql.php | 2 ++ library/think/db/builder/Sqlite.php | 2 ++ library/think/db/builder/Sqlsrv.php | 2 ++ library/traits/model/SoftDelete.php | 2 +- 5 files changed, 9 insertions(+), 1 deletion(-) diff --git a/library/think/db/builder/Mysql.php b/library/think/db/builder/Mysql.php index de38fac5..e432e396 100644 --- a/library/think/db/builder/Mysql.php +++ b/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/library/think/db/builder/Pgsql.php b/library/think/db/builder/Pgsql.php index 8b853a2f..67b98b44 100644 --- a/library/think/db/builder/Pgsql.php +++ b/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/library/think/db/builder/Sqlite.php b/library/think/db/builder/Sqlite.php index 02d1bf2e..680b4965 100644 --- a/library/think/db/builder/Sqlite.php +++ b/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/library/think/db/builder/Sqlsrv.php b/library/think/db/builder/Sqlsrv.php index d2f418f3..99f68409 100644 --- a/library/think/db/builder/Sqlsrv.php +++ b/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/library/traits/model/SoftDelete.php b/library/traits/model/SoftDelete.php index 720b6e88..2b97ff72 100644 --- a/library/traits/model/SoftDelete.php +++ b/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); From ea9fe74bc38ea1f6a9dd04de9d54564235d1cc31 Mon Sep 17 00:00:00 2001 From: mineyang Date: Mon, 13 Feb 2017 13:09:45 +0800 Subject: [PATCH 125/125] =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=85=B3=E9=97=ADses?= =?UTF-8?q?sion=E7=9A=84=E5=AE=89=E5=85=A8=E4=BC=A0=E8=BE=93=EF=BC=8C?= =?UTF-8?q?=E6=AD=A4=E9=80=89=E9=A1=B9=E4=BB=85=E8=83=BD=E5=9C=A8HTTPS?= =?UTF-8?q?=E4=B8=8B=E8=AE=BE=E7=BD=AE=E5=BC=80=E5=90=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- convention.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/convention.php b/convention.php index b6f23e28..887b076d 100644 --- a/convention.php +++ b/convention.php @@ -206,7 +206,7 @@ return [ // 是否自动开启 SESSION 'auto_start' => true, 'httponly' => true, - 'secure' => true, + 'secure' => false, ], // +----------------------------------------------------------------------