From ccfe9ad612a4a2df041cb361ecb49d5a11442417 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 7 Feb 2018 12:10:28 +0800 Subject: [PATCH 1/9] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E4=B8=80=E5=AF=B9?= =?UTF-8?q?=E4=B8=80=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/BelongsTo.php | 17 +++++++++++++++++ library/think/model/relation/HasOne.php | 16 ++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/library/think/model/relation/BelongsTo.php b/library/think/model/relation/BelongsTo.php index dfd61147..4765811e 100644 --- a/library/think/model/relation/BelongsTo.php +++ b/library/think/model/relation/BelongsTo.php @@ -220,4 +220,21 @@ class BelongsTo extends OneToOne return $this->parent->setRelation($this->relation, null); } + + /** + * 执行基础查询(仅执行一次) + * @access protected + * @return void + */ + protected function baseQuery() + { + if (empty($this->baseQuery)) { + if (isset($this->parent->{$this->foreignKey})) { + // 关联查询带入关联条件 + $this->query->where($this->localKey, '=', $this->parent->{$this->foreignKey}); + } + + $this->baseQuery = true; + } + } } diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index f64f133f..46fedb87 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -190,4 +190,20 @@ class HasOne extends OneToOne } } + /** + * 执行基础查询(仅执行一次) + * @access protected + * @return void + */ + protected function baseQuery() + { + if (empty($this->baseQuery)) { + if (isset($this->parent->{$this->localKey})) { + // 关联查询带入关联条件 + $this->query->where($this->foreignKey, '=', $this->parent->{$this->localKey}); + } + + $this->baseQuery = true; + } + } } From 4d513f3c62cdce050d3d57e19cb950e72ec8202c Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 8 Feb 2018 15:28:40 +0800 Subject: [PATCH 2/9] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E7=BC=93=E5=AD=98?= 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 d425d78a..e53cdacb 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -2424,7 +2424,7 @@ class Query $data = $value[1]; } if (isset($data)) { - return 'think:' . (is_array($options['table']) ? key($options['table']) : $options['table']) . '|' . $data; + return 'think:' . $this->connection->getConfig('database') . '.' . (is_array($options['table']) ? key($options['table']) : $options['table']) . '|' . $data; } else { return md5(serialize($options) . serialize($bind)); } From b8d4bb3c985761e48b0e92ab7014dcabc7a93bdd Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 8 Feb 2018 15:41:38 +0800 Subject: [PATCH 3/9] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index e53cdacb..6c64a074 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -403,7 +403,7 @@ class Query if (empty($this->options['table'])) { $this->options['table'] = $this->getTable(); } - $key = is_string($cache['key']) ? $cache['key'] : md5($field . serialize($this->options) . serialize($this->bind)); + $key = is_string($cache['key']) ? $cache['key'] : md5($this->connection->getConfig('database') . '.' . $field . serialize($this->options) . serialize($this->bind)); $result = Cache::get($key); } if (false === $result) { @@ -447,7 +447,7 @@ class Query if (empty($this->options['table'])) { $this->options['table'] = $this->getTable(); } - $guid = is_string($cache['key']) ? $cache['key'] : md5($field . serialize($this->options) . serialize($this->bind)); + $guid = is_string($cache['key']) ? $cache['key'] : md5($this->connection->getConfig('database') . '.' . $field . serialize($this->options) . serialize($this->bind)); $result = Cache::get($guid); } if (false === $result) { @@ -2322,7 +2322,7 @@ class Query // 判断查询缓存 $cache = $options['cache']; unset($options['cache']); - $key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options) . serialize($this->bind)); + $key = is_string($cache['key']) ? $cache['key'] : md5($this->connection->getConfig('database') . '.' . serialize($options) . serialize($this->bind)); $resultSet = Cache::get($key); } if (false === $resultSet) { @@ -2463,11 +2463,11 @@ 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; + $key = 'think:' . $this->connection->getConfig('database') . '.' . (is_array($options['table']) ? key($options['table']) : $options['table']) . '|' . $data; } elseif (is_string($cache['key'])) { $key = $cache['key']; } elseif (!isset($key)) { - $key = md5(serialize($options) . serialize($this->bind)); + $key = md5($this->connection->getConfig('database') . '.' . serialize($options) . serialize($this->bind)); } $result = Cache::get($key); } From b052eccb269cbc2f36946ffab9e069ae7f264629 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 8 Feb 2018 15:48:14 +0800 Subject: [PATCH 4/9] =?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 | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 6c64a074..0396faca 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -2423,10 +2423,12 @@ class Query } elseif (is_array($value) && is_string($value[0]) && 'eq' == strtolower($value[0])) { $data = $value[1]; } + $prefix = $this->connection->getConfig('database') . '.'; + if (isset($data)) { - return 'think:' . $this->connection->getConfig('database') . '.' . (is_array($options['table']) ? key($options['table']) : $options['table']) . '|' . $data; + return 'think:' . $prefix . (is_array($options['table']) ? key($options['table']) : $options['table']) . '|' . $data; } else { - return md5(serialize($options) . serialize($bind)); + return md5($prefix . serialize($options) . serialize($bind)); } } From f80073f9f925712d0405f1ba30d4fd0a8bafce3b Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 8 Feb 2018 17:27:26 +0800 Subject: [PATCH 5/9] =?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=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2?= 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, 2 insertions(+) diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index 02d7d563..dd8595c9 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -304,6 +304,8 @@ abstract class OneToOne extends Relation */ protected function eagerlyWhere($model, $where, $key, $relation, $subRelation = '', $closure = false) { + $this->baseQuery = true; + // 预载入关联查询 支持嵌套预载入 if ($closure) { call_user_func_array($closure, [ & $model]); From 09402cee9be14333e580d954cd956fba17474dc9 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 14 Feb 2018 12:11:25 +0800 Subject: [PATCH 6/9] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=B8=80=E5=AF=B9?= =?UTF-8?q?=E4=B8=80=E5=85=B3=E8=81=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/BelongsTo.php | 1 + library/think/model/relation/HasOne.php | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/library/think/model/relation/BelongsTo.php b/library/think/model/relation/BelongsTo.php index 4765811e..ebef780c 100644 --- a/library/think/model/relation/BelongsTo.php +++ b/library/think/model/relation/BelongsTo.php @@ -128,6 +128,7 @@ class BelongsTo extends OneToOne } if (!empty($range)) { + $this->query->removeWhereField($localKey); $data = $this->eagerlyWhere($this->query, [ $localKey => [ 'in', diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index 46fedb87..dad8a7f2 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -130,7 +130,8 @@ class HasOne extends OneToOne } if (!empty($range)) { - $data = $this->eagerlyWhere($this, [ + $this->query->removeWhereField($foreignKey); + $data = $this->eagerlyWhere($this->query, [ $foreignKey => [ 'in', $range, From 9606f235f2b718fd393c1be81a8167d42676190f Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 14 Feb 2018 12:14:06 +0800 Subject: [PATCH 7/9] =?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/BelongsTo.php | 3 ++- library/think/model/relation/HasOne.php | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/library/think/model/relation/BelongsTo.php b/library/think/model/relation/BelongsTo.php index ebef780c..e609a876 100644 --- a/library/think/model/relation/BelongsTo.php +++ b/library/think/model/relation/BelongsTo.php @@ -172,7 +172,8 @@ class BelongsTo extends OneToOne { $localKey = $this->localKey; $foreignKey = $this->foreignKey; - $data = $this->eagerlyWhere($this->query, [$localKey => $result->$foreignKey], $localKey, $relation, $subRelation, $closure); + $this->query->removeWhereField($localKey); + $data = $this->eagerlyWhere($this->query, [$localKey => $result->$foreignKey], $localKey, $relation, $subRelation, $closure); // 关联模型 if (!isset($data[$result->$foreignKey])) { $relationModel = null; diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index dad8a7f2..3c036c01 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -173,7 +173,8 @@ class HasOne extends OneToOne { $localKey = $this->localKey; $foreignKey = $this->foreignKey; - $data = $this->eagerlyWhere($this, [$foreignKey => $result->$localKey], $foreignKey, $relation, $subRelation, $closure); + $this->query->removeWhereField($foreignKey); + $data = $this->eagerlyWhere($this->query, [$foreignKey => $result->$localKey], $foreignKey, $relation, $subRelation, $closure); // 关联模型 if (!isset($data[$result->$localKey])) { From 680be85343d09febcd9f2038da3c751379bd7734 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 14 Feb 2018 22:31:41 +0800 Subject: [PATCH 8/9] =?UTF-8?q?=E6=94=B9=E8=BF=9Bfield=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 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 0396faca..53aa71b5 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -793,7 +793,7 @@ class Query } if (isset($this->options['field'])) { - $field = array_merge($this->options['field'], $field); + $field = array_merge((array) $this->options['field'], $field); } $this->options['field'] = array_unique($field); return $this; From 8fffb9c5ccec20fff317be2bcd702b3bbcece78a Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 23 Feb 2018 21:17:40 +0800 Subject: [PATCH 9/9] =?UTF-8?q?=E4=BC=98=E5=8C=96Template=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Template.php | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/library/think/Template.php b/library/think/Template.php index 02cd504b..9ba0ff35 100644 --- a/library/think/Template.php +++ b/library/think/Template.php @@ -64,14 +64,16 @@ class Template */ public function __construct(array $config = []) { - $this->config['cache_path'] = TEMP_PATH; - $this->config = array_merge($this->config, $config); + $this->config['cache_path'] = TEMP_PATH; + $this->config = array_merge($this->config, $config); + $this->config['taglib_begin_origin'] = $this->config['taglib_begin']; $this->config['taglib_end_origin'] = $this->config['taglib_end']; - $this->config['taglib_begin'] = $this->stripPreg($this->config['taglib_begin']); - $this->config['taglib_end'] = $this->stripPreg($this->config['taglib_end']); - $this->config['tpl_begin'] = $this->stripPreg($this->config['tpl_begin']); - $this->config['tpl_end'] = $this->stripPreg($this->config['tpl_end']); + + $this->config['taglib_begin'] = preg_quote($this->config['taglib_begin'], '/'); + $this->config['taglib_end'] = preg_quote($this->config['taglib_end'], '/'); + $this->config['tpl_begin'] = preg_quote($this->config['tpl_begin'], '/'); + $this->config['tpl_end'] = preg_quote($this->config['tpl_end'], '/'); // 初始化模板编译存储器 $type = $this->config['compile_type'] ? $this->config['compile_type'] : 'File'; @@ -79,20 +81,6 @@ class Template $this->storage = new $class(); } - /** - * 字符串替换 避免正则混淆 - * @access private - * @param string $str - * @return string - */ - private function stripPreg($str) - { - return str_replace( - ['{', '}', '(', ')', '|', '[', ']', '-', '+', '*', '.', '^', '?'], - ['\{', '\}', '\(', '\)', '\|', '\[', '\]', '\-', '\+', '\*', '\.', '\^', '\?'], - $str); - } - /** * 模板变量赋值 * @access public