From a489f99ef44d458c3beb3f29f4205384822505dc Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 31 Mar 2017 16:04:38 +0800 Subject: [PATCH 01/11] =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E8=8C=83=E5=9B=B4?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=9D=99=E6=80=81=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 976823aa..bae4902f 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1812,16 +1812,27 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } } - public static function __callStatic($method, $params) + public static function __callStatic($method, $args) { + $model = new static(); + if (isset(static::$db)) { $query = static::$db; static::$db = null; } else { - $query = (new static())->db(); + $query = $model->db(); } - return call_user_func_array([$query, $method], $params); + if (method_exists($model, 'scope' . $method)) { + // 动态调用命名范围 + $method = 'scope' . $method; + array_unshift($args, $query); + + call_user_func_array([$model, $method], $args); + return $query; + } else { + return call_user_func_array([$query, $method], $args); + } } /** From 164b7521c57bffb4a7a2496285a00bb5be88a8e1 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 1 Apr 2017 10:59:40 +0800 Subject: [PATCH 02/11] =?UTF-8?q?=E6=94=B9=E8=BF=9BsetAttr=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/Model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/think/Model.php b/library/think/Model.php index 976823aa..d3e8e99d 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -283,7 +283,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 检测修改器 $method = 'set' . Loader::parseName($name, 1) . 'Attr'; if (method_exists($this, $method)) { - $value = $this->$method($value, $data); + $value = $this->$method($value, array_merge($this->data, $data)); } elseif (isset($this->type[$name])) { // 类型转换 $value = $this->writeTransform($value, $this->type[$name]); From ae501f402805c898f89834d670d649d228622c56 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 1 Apr 2017 12:50:32 +0800 Subject: [PATCH 03/11] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=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 | 48 ++++++++++++++++++++-- library/think/model/relation/BelongsTo.php | 4 ++ library/think/model/relation/HasMany.php | 10 +++++ library/think/model/relation/HasOne.php | 20 +++++---- library/think/model/relation/MorphMany.php | 10 +++++ library/think/model/relation/MorphOne.php | 23 +++++++++-- library/think/model/relation/MorphTo.php | 7 +++- library/think/model/relation/OneToOne.php | 8 +++- 8 files changed, 113 insertions(+), 17 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 1e6e32a8..e6d7661f 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -36,6 +36,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected static $links = []; // 数据库配置 protected $connection = []; + // 父关联模型对象 + protected $parent; // 数据库查询对象 protected $query; // 当前模型名称 @@ -219,6 +221,29 @@ abstract class Model implements \JsonSerializable, \ArrayAccess { } + /** + * 设置父关联对象 + * @access public + * @param Model $model 模型对象 + * @return $this + */ + public function setParent($model) + { + $this->parent = $model; + + return $this; + } + + /** + * 获取父关联对象 + * @access public + * @return Model + */ + public function getParent() + { + return $this->parent; + } + /** * 设置数据对象值 * @access public @@ -458,10 +483,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } elseif ($notFound) { $method = Loader::parseName($name, 1, false); if (method_exists($this, $method) && $this->$method() instanceof Relation) { - // 清空之前的查询参数 - $this->$method()->removeOption(); + $modelRelation = $this->$method(); // 不存在该字段 获取关联数据 - $value = $this->$method()->getRelation(); + $value = $this->getRelationData($modelRelation); // 保存关联对象值 $this->data[$name] = $value; } else { @@ -471,6 +495,24 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return $value; } + /** + * 获取关联模型数据 + * @access public + * @param mixed $value 值 + * @param string|array $type 要转换的类型 + * @return mixed + */ + protected function getRelationData($modelRelation) + { + if ($this->parent && get_class($this->parent) == $modelRelation->getModel()) { + $value = $this->parent; + } else { + // 首先获取关联数据 + $value = $modelRelation->removeOption()->getRelation(); + } + return $value; + } + /** * 数据读取 类型转换 * @access public diff --git a/library/think/model/relation/BelongsTo.php b/library/think/model/relation/BelongsTo.php index f693ef6d..d0973614 100644 --- a/library/think/model/relation/BelongsTo.php +++ b/library/think/model/relation/BelongsTo.php @@ -130,6 +130,8 @@ class BelongsTo extends OneToOne $relationModel = null; } else { $relationModel = $data[$result->$foreignKey]; + $relationModel->setParent($result); + $relationModel->isUpdate(true); } if ($relationModel && !empty($this->bindAttr)) { @@ -161,6 +163,8 @@ class BelongsTo extends OneToOne $relationModel = null; } else { $relationModel = $data[$result->$foreignKey]; + $relationModel->setParent($result); + $relationModel->isUpdate(true); } if ($relationModel && !empty($this->bindAttr)) { // 绑定关联属性 diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index 4c045494..3fc9f746 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -84,6 +84,11 @@ class HasMany extends Relation if (!isset($data[$result->$localKey])) { $data[$result->$localKey] = []; } + + foreach ($data[$result->$localKey] as &$relationModel) { + $relationModel->setParent($result); + } + $result->setAttr($attr, $this->resultSetBuild($data[$result->$localKey])); } } @@ -108,6 +113,11 @@ class HasMany extends Relation if (!isset($data[$result->$localKey])) { $data[$result->$localKey] = []; } + + foreach ($data[$result->$localKey] as &$relationModel) { + $relationModel->setParent($result); + } + $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$localKey])); } } diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index 95a401bf..a19d9a4b 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -132,10 +132,12 @@ class HasOne extends OneToOne $relationModel = null; } else { $relationModel = $data[$result->$localKey]; - } - if ($relationModel && !empty($this->bindAttr)) { - // 绑定关联属性 - $this->bindAttr($relationModel, $result, $this->bindAttr); + $relationModel->setParent($result); + $relationModel->isUpdate(true); + if (!empty($this->bindAttr)) { + // 绑定关联属性 + $this->bindAttr($relationModel, $result, $this->bindAttr); + } } // 设置关联属性 $result->setAttr($attr, $relationModel); @@ -163,12 +165,14 @@ class HasOne extends OneToOne $relationModel = null; } else { $relationModel = $data[$result->$localKey]; + $relationModel->setParent($result); + $relationModel->isUpdate(true); + if (!empty($this->bindAttr)) { + // 绑定关联属性 + $this->bindAttr($relationModel, $result, $this->bindAttr); + } } - if ($relationModel && !empty($this->bindAttr)) { - // 绑定关联属性 - $this->bindAttr($relationModel, $result, $this->bindAttr); - } $result->setAttr(Loader::parseName($relation), $relationModel); } diff --git a/library/think/model/relation/MorphMany.php b/library/think/model/relation/MorphMany.php index 964d3d3a..7142314b 100644 --- a/library/think/model/relation/MorphMany.php +++ b/library/think/model/relation/MorphMany.php @@ -119,6 +119,10 @@ class MorphMany extends Relation if (!isset($data[$result->$pk])) { $data[$result->$pk] = []; } + foreach ($data[$result->$pk] as &$relationModel) { + $relationModel->setParent($result); + $relationModel->isUpdate(true); + } $result->setAttr($attr, $this->resultSetBuild($data[$result->$pk])); } } @@ -141,6 +145,12 @@ class MorphMany extends Relation $this->morphKey => $result->$pk, $this->morphType => $this->type, ], $relation, $subRelation, $closure); + + foreach ($data[$result->$pk] as &$relationModel) { + $relationModel->setParent($result); + $relationModel->isUpdate(true); + } + $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$pk])); } } diff --git a/library/think/model/relation/MorphOne.php b/library/think/model/relation/MorphOne.php index 1bcc7710..c2547c0a 100644 --- a/library/think/model/relation/MorphOne.php +++ b/library/think/model/relation/MorphOne.php @@ -117,9 +117,14 @@ class MorphOne extends Relation // 关联数据封装 foreach ($resultSet as $result) { if (!isset($data[$result->$pk])) { - $data[$result->$pk] = []; + $relationModel = null; + } else { + $relationModel = $data[$result->$pk]; + $relationModel->setParent($result); + $relationModel->isUpdate(true); } - $result->setAttr($attr, $this->resultSetBuild($data[$result->$pk])); + + $result->setAttr($attr, $relationModel); } } } @@ -137,11 +142,21 @@ class MorphOne extends Relation { $pk = $result->getPk(); if (isset($result->$pk)) { + $pk = $result->$pk; $data = $this->eagerlyMorphToOne([ - $this->morphKey => $result->$pk, + $this->morphKey => $pk, $this->morphType => $this->type, ], $relation, $subRelation, $closure); - $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$pk])); + + if (isset($data[$pk])) { + $relationModel = $data[$pk]; + $data[$pk]->setParent($result); + $relationModel->isUpdate(true); + } else { + $relationModel = null; + } + + $result->setAttr(Loader::parseName($relation), $relationModel); } } diff --git a/library/think/model/relation/MorphTo.php b/library/think/model/relation/MorphTo.php index 9c020b85..304186dc 100644 --- a/library/think/model/relation/MorphTo.php +++ b/library/think/model/relation/MorphTo.php @@ -168,7 +168,11 @@ class MorphTo extends Relation if (!isset($data[$result->$morphKey])) { throw new Exception('relation data not exists :' . $this->model); } else { - $result->setAttr($attr, $data[$result->$morphKey]); + $relationModel = $data[$result->$morphKey]; + $relationModel->setParent($result); + $relationModel->isUpdate(true); + + $result->setAttr($attr, $relationModel); } } } @@ -220,6 +224,7 @@ class MorphTo extends Relation $pk = $this->parent->{$this->morphKey}; $data = (new $model)->with($subRelation)->find($pk); if ($data) { + $data->setParent($result); $data->isUpdate(true); } $result->setAttr(Loader::parseName($relation), $data ?: null); diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index b0cf931d..7dc5e47e 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -245,13 +245,19 @@ abstract class OneToOne extends Relation } } } + if (isset($list[$relation])) { $relationModel = new $model($list[$relation]); + $relationModel->setParent($result); + $relationModel->isUpdate(true); + if (!empty($this->bindAttr)) { $this->bindAttr($relationModel, $result, $this->bindAttr); } + } else { + $relationModel = null; } - $result->setAttr(Loader::parseName($relation), !isset($relationModel) ? null : $relationModel->isUpdate(true)); + $result->setAttr(Loader::parseName($relation), $relationModel); } /** From 68a1882e1e30d702013a7284008861e548ac4123 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 1 Apr 2017 14:07:27 +0800 Subject: [PATCH 04/11] =?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 | 4 ++-- library/think/model/relation/HasMany.php | 4 ++-- library/think/model/relation/HasOne.php | 4 ++-- library/think/model/relation/MorphMany.php | 4 ++-- library/think/model/relation/MorphOne.php | 4 ++-- library/think/model/relation/MorphTo.php | 4 ++-- library/think/model/relation/OneToOne.php | 2 +- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/library/think/model/relation/BelongsTo.php b/library/think/model/relation/BelongsTo.php index d0973614..cd5c04c0 100644 --- a/library/think/model/relation/BelongsTo.php +++ b/library/think/model/relation/BelongsTo.php @@ -130,7 +130,7 @@ class BelongsTo extends OneToOne $relationModel = null; } else { $relationModel = $data[$result->$foreignKey]; - $relationModel->setParent($result); + $relationModel->setParent(clone $result); $relationModel->isUpdate(true); } @@ -163,7 +163,7 @@ class BelongsTo extends OneToOne $relationModel = null; } else { $relationModel = $data[$result->$foreignKey]; - $relationModel->setParent($result); + $relationModel->setParent(clone $result); $relationModel->isUpdate(true); } if ($relationModel && !empty($this->bindAttr)) { diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index 3fc9f746..2f0d30b8 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -86,7 +86,7 @@ class HasMany extends Relation } foreach ($data[$result->$localKey] as &$relationModel) { - $relationModel->setParent($result); + $relationModel->setParent(clone $result); } $result->setAttr($attr, $this->resultSetBuild($data[$result->$localKey])); @@ -115,7 +115,7 @@ class HasMany extends Relation } foreach ($data[$result->$localKey] as &$relationModel) { - $relationModel->setParent($result); + $relationModel->setParent(clone $result); } $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$localKey])); diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index a19d9a4b..27627627 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -132,7 +132,7 @@ class HasOne extends OneToOne $relationModel = null; } else { $relationModel = $data[$result->$localKey]; - $relationModel->setParent($result); + $relationModel->setParent(clone $result); $relationModel->isUpdate(true); if (!empty($this->bindAttr)) { // 绑定关联属性 @@ -165,7 +165,7 @@ class HasOne extends OneToOne $relationModel = null; } else { $relationModel = $data[$result->$localKey]; - $relationModel->setParent($result); + $relationModel->setParent(clone $result); $relationModel->isUpdate(true); if (!empty($this->bindAttr)) { // 绑定关联属性 diff --git a/library/think/model/relation/MorphMany.php b/library/think/model/relation/MorphMany.php index 7142314b..6b1ca7b3 100644 --- a/library/think/model/relation/MorphMany.php +++ b/library/think/model/relation/MorphMany.php @@ -120,7 +120,7 @@ class MorphMany extends Relation $data[$result->$pk] = []; } foreach ($data[$result->$pk] as &$relationModel) { - $relationModel->setParent($result); + $relationModel->setParent(clone $result); $relationModel->isUpdate(true); } $result->setAttr($attr, $this->resultSetBuild($data[$result->$pk])); @@ -147,7 +147,7 @@ class MorphMany extends Relation ], $relation, $subRelation, $closure); foreach ($data[$result->$pk] as &$relationModel) { - $relationModel->setParent($result); + $relationModel->setParent(clone $result); $relationModel->isUpdate(true); } diff --git a/library/think/model/relation/MorphOne.php b/library/think/model/relation/MorphOne.php index c2547c0a..b1e4c3d1 100644 --- a/library/think/model/relation/MorphOne.php +++ b/library/think/model/relation/MorphOne.php @@ -120,7 +120,7 @@ class MorphOne extends Relation $relationModel = null; } else { $relationModel = $data[$result->$pk]; - $relationModel->setParent($result); + $relationModel->setParent(clone $result); $relationModel->isUpdate(true); } @@ -150,7 +150,7 @@ class MorphOne extends Relation if (isset($data[$pk])) { $relationModel = $data[$pk]; - $data[$pk]->setParent($result); + $data[$pk]->setParent(clone $result); $relationModel->isUpdate(true); } else { $relationModel = null; diff --git a/library/think/model/relation/MorphTo.php b/library/think/model/relation/MorphTo.php index 304186dc..c6a2211b 100644 --- a/library/think/model/relation/MorphTo.php +++ b/library/think/model/relation/MorphTo.php @@ -169,7 +169,7 @@ class MorphTo extends Relation throw new Exception('relation data not exists :' . $this->model); } else { $relationModel = $data[$result->$morphKey]; - $relationModel->setParent($result); + $relationModel->setParent(clone $result); $relationModel->isUpdate(true); $result->setAttr($attr, $relationModel); @@ -224,7 +224,7 @@ class MorphTo extends Relation $pk = $this->parent->{$this->morphKey}; $data = (new $model)->with($subRelation)->find($pk); if ($data) { - $data->setParent($result); + $data->setParent(clone $result); $data->isUpdate(true); } $result->setAttr(Loader::parseName($relation), $data ?: null); diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index 7dc5e47e..4368618e 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -248,7 +248,7 @@ abstract class OneToOne extends Relation if (isset($list[$relation])) { $relationModel = new $model($list[$relation]); - $relationModel->setParent($result); + $relationModel->setParent(clone $result); $relationModel->isUpdate(true); if (!empty($this->bindAttr)) { From 2d15c762a2f23bd165d37c70f2ff125503edcfd9 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 1 Apr 2017 14:17:24 +0800 Subject: [PATCH 05/11] =?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 e6d7661f..9024c30c 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -498,8 +498,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 获取关联模型数据 * @access public - * @param mixed $value 值 - * @param string|array $type 要转换的类型 + * @param Relation $modelRelation 模型关联对象 * @return mixed */ protected function getRelationData($modelRelation) From 2ebf07ba166b037f8a59b461f6c8f73afe770a32 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 1 Apr 2017 15:46:26 +0800 Subject: [PATCH 06/11] =?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/MorphOne.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/think/model/relation/MorphOne.php b/library/think/model/relation/MorphOne.php index b1e4c3d1..9e231d68 100644 --- a/library/think/model/relation/MorphOne.php +++ b/library/think/model/relation/MorphOne.php @@ -150,7 +150,7 @@ class MorphOne extends Relation if (isset($data[$pk])) { $relationModel = $data[$pk]; - $data[$pk]->setParent(clone $result); + $relationModel->setParent(clone $result); $relationModel->isUpdate(true); } else { $relationModel = null; From d3be47e269be080c0f30ac74d322e46fabef653b Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 1 Apr 2017 16:13:44 +0800 Subject: [PATCH 07/11] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E5=85=B3=E8=81=94?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/BelongsTo.php | 8 +++++++- library/think/model/relation/HasMany.php | 9 ++++++++- library/think/model/relation/HasManyThrough.php | 10 ++++------ library/think/model/relation/HasOne.php | 5 ++++- library/think/model/relation/MorphMany.php | 9 ++++++++- library/think/model/relation/MorphOne.php | 5 ++++- library/think/model/relation/MorphTo.php | 6 ++++-- 7 files changed, 39 insertions(+), 13 deletions(-) diff --git a/library/think/model/relation/BelongsTo.php b/library/think/model/relation/BelongsTo.php index cd5c04c0..9e443d54 100644 --- a/library/think/model/relation/BelongsTo.php +++ b/library/think/model/relation/BelongsTo.php @@ -50,7 +50,13 @@ class BelongsTo extends OneToOne if ($closure) { call_user_func_array($closure, [ & $this->query]); } - return $this->query->where($this->localKey, $this->parent->$foreignKey)->relation($subRelation)->find(); + $relationModel = $this->query + ->where($this->localKey, $this->parent->$foreignKey) + ->relation($subRelation) + ->find(); + $relationModel->setParent(clone $this->parent); + + return $relationModel; } /** diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index 2f0d30b8..2c574869 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -47,7 +47,14 @@ class HasMany extends Relation if ($closure) { call_user_func_array($closure, [ & $this->query]); } - return $this->relation($subRelation)->select(); + $list = $this->relation($subRelation)->select(); + $parent = clone $this->parent; + + foreach ($list as &$model) { + $model->setParent($parent); + } + + return $list; } /** diff --git a/library/think/model/relation/HasManyThrough.php b/library/think/model/relation/HasManyThrough.php index dd6f6a55..395e7f55 100644 --- a/library/think/model/relation/HasManyThrough.php +++ b/library/think/model/relation/HasManyThrough.php @@ -57,6 +57,7 @@ class HasManyThrough extends Relation if ($closure) { call_user_func_array($closure, [ & $this->query]); } + return $this->relation($subRelation)->select(); } @@ -96,8 +97,7 @@ class HasManyThrough extends Relation * @return void */ public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class) - { - } + {} /** * 预载入关联查询 返回模型对象 @@ -110,8 +110,7 @@ class HasManyThrough extends Relation * @return void */ public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class) - { - } + {} /** * 关联统计 @@ -121,8 +120,7 @@ class HasManyThrough extends Relation * @return integer */ public function relationCount($result, $closure) - { - } + {} /** * 执行基础查询(进执行一次) diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index 27627627..991a594f 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -50,7 +50,10 @@ class HasOne extends OneToOne call_user_func_array($closure, [ & $this->query]); } // 判断关联类型执行查询 - return $this->query->where($this->foreignKey, $this->parent->$localKey)->relation($subRelation)->find(); + $relationModel = $this->query->where($this->foreignKey, $this->parent->$localKey)->relation($subRelation)->find(); + $relationModel->setParent(clone $this->parent); + + return $relationModel; } /** diff --git a/library/think/model/relation/MorphMany.php b/library/think/model/relation/MorphMany.php index 6b1ca7b3..0e4bf6f4 100644 --- a/library/think/model/relation/MorphMany.php +++ b/library/think/model/relation/MorphMany.php @@ -56,7 +56,14 @@ class MorphMany extends Relation if ($closure) { call_user_func_array($closure, [ & $this->query]); } - return $this->relation($subRelation)->select(); + $list = $this->relation($subRelation)->select(); + $parent = clone $this->parent; + + foreach ($list as &$model) { + $model->setParent($parent); + } + + return $list; } /** diff --git a/library/think/model/relation/MorphOne.php b/library/think/model/relation/MorphOne.php index 9e231d68..b564fbc8 100644 --- a/library/think/model/relation/MorphOne.php +++ b/library/think/model/relation/MorphOne.php @@ -56,7 +56,10 @@ class MorphOne extends Relation if ($closure) { call_user_func_array($closure, [ & $this->query]); } - return $this->relation($subRelation)->find(); + $relationModel = $this->relation($subRelation)->find(); + $relationModel->setParent(clone $this->parent); + + return $relationModel; } /** diff --git a/library/think/model/relation/MorphTo.php b/library/think/model/relation/MorphTo.php index c6a2211b..70f1dc03 100644 --- a/library/think/model/relation/MorphTo.php +++ b/library/think/model/relation/MorphTo.php @@ -56,8 +56,10 @@ class MorphTo extends Relation // 多态模型 $model = $this->parseModel($this->parent->$morphType); // 主键数据 - $pk = $this->parent->$morphKey; - return (new $model)->relation($subRelation)->find($pk); + $pk = $this->parent->$morphKey; + $relationModel = (new $model)->relation($subRelation)->find($pk); + $relationModel->setParent(clone $this->parent); + return $relationModel; } /** From 5d8944349617c3285ca3c6c308bce2671c15809d Mon Sep 17 00:00:00 2001 From: ThinkPHP Date: Sat, 1 Apr 2017 07:47:28 +0000 Subject: [PATCH 08/11] Apply fixes from StyleCI --- library/think/model/relation/HasMany.php | 1 - library/think/model/relation/HasManyThrough.php | 1 - library/think/model/relation/MorphMany.php | 1 - library/think/model/relation/MorphOne.php | 1 - tests/thinkphp/library/think/requestTest.php | 1 - 5 files changed, 5 deletions(-) diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index 2f0d30b8..f883a57c 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -11,7 +11,6 @@ namespace think\model\relation; -use think\Db; use think\db\Query; use think\Loader; use think\Model; diff --git a/library/think/model/relation/HasManyThrough.php b/library/think/model/relation/HasManyThrough.php index dd6f6a55..ab153693 100644 --- a/library/think/model/relation/HasManyThrough.php +++ b/library/think/model/relation/HasManyThrough.php @@ -11,7 +11,6 @@ namespace think\model\relation; -use think\Db; use think\db\Query; use think\Exception; use think\Loader; diff --git a/library/think/model/relation/MorphMany.php b/library/think/model/relation/MorphMany.php index 6b1ca7b3..2307645c 100644 --- a/library/think/model/relation/MorphMany.php +++ b/library/think/model/relation/MorphMany.php @@ -11,7 +11,6 @@ namespace think\model\relation; -use think\Db; use think\db\Query; use think\Exception; use think\Loader; diff --git a/library/think/model/relation/MorphOne.php b/library/think/model/relation/MorphOne.php index 9e231d68..e873aa04 100644 --- a/library/think/model/relation/MorphOne.php +++ b/library/think/model/relation/MorphOne.php @@ -11,7 +11,6 @@ namespace think\model\relation; -use think\Db; use think\db\Query; use think\Exception; use think\Loader; diff --git a/tests/thinkphp/library/think/requestTest.php b/tests/thinkphp/library/think/requestTest.php index 7d89fa36..f86546e6 100644 --- a/tests/thinkphp/library/think/requestTest.php +++ b/tests/thinkphp/library/think/requestTest.php @@ -17,7 +17,6 @@ namespace tests\thinkphp\library\think; use think\Config; use think\Request; -use think\Route; class requestTest extends \PHPUnit_Framework_TestCase { From 6e5d6fa7b28ebb67434d0844325797b6eb020e0f Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 3 Apr 2017 09:37:31 +0800 Subject: [PATCH 09/11] =?UTF-8?q?=E6=94=B9=E8=BF=9B=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/relation/BelongsTo.php | 5 ++++- library/think/model/relation/HasOne.php | 5 ++++- library/think/model/relation/MorphOne.php | 5 ++++- library/think/model/relation/MorphTo.php | 5 ++++- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/library/think/model/relation/BelongsTo.php b/library/think/model/relation/BelongsTo.php index 9e443d54..630719c0 100644 --- a/library/think/model/relation/BelongsTo.php +++ b/library/think/model/relation/BelongsTo.php @@ -54,7 +54,10 @@ class BelongsTo extends OneToOne ->where($this->localKey, $this->parent->$foreignKey) ->relation($subRelation) ->find(); - $relationModel->setParent(clone $this->parent); + + if ($relationModel) { + $relationModel->setParent(clone $this->parent); + } return $relationModel; } diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index 991a594f..e5a557a0 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -51,7 +51,10 @@ class HasOne extends OneToOne } // 判断关联类型执行查询 $relationModel = $this->query->where($this->foreignKey, $this->parent->$localKey)->relation($subRelation)->find(); - $relationModel->setParent(clone $this->parent); + + if ($relationModel) { + $relationModel->setParent(clone $this->parent); + } return $relationModel; } diff --git a/library/think/model/relation/MorphOne.php b/library/think/model/relation/MorphOne.php index b58b0419..e22198bb 100644 --- a/library/think/model/relation/MorphOne.php +++ b/library/think/model/relation/MorphOne.php @@ -56,7 +56,10 @@ class MorphOne extends Relation call_user_func_array($closure, [ & $this->query]); } $relationModel = $this->relation($subRelation)->find(); - $relationModel->setParent(clone $this->parent); + + if ($relationModel) { + $relationModel->setParent(clone $this->parent); + } return $relationModel; } diff --git a/library/think/model/relation/MorphTo.php b/library/think/model/relation/MorphTo.php index 70f1dc03..d67989c4 100644 --- a/library/think/model/relation/MorphTo.php +++ b/library/think/model/relation/MorphTo.php @@ -58,7 +58,10 @@ class MorphTo extends Relation // 主键数据 $pk = $this->parent->$morphKey; $relationModel = (new $model)->relation($subRelation)->find($pk); - $relationModel->setParent(clone $this->parent); + + if ($relationModel) { + $relationModel->setParent(clone $this->parent); + } return $relationModel; } From 2b4ddaa35c5a5cc97e579758d9ec06240e7e22c2 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 3 Apr 2017 22:34:10 +0800 Subject: [PATCH 10/11] =?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 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/think/Model.php b/library/think/Model.php index 9024c30c..2e5531e5 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1258,7 +1258,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 返回模型的错误信息 * @access public - * @return string + * @return string|array */ public function getError() { From 3370da9a034c8e58b36990eb040d6c70c57f6762 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 6 Apr 2017 15:01:34 +0800 Subject: [PATCH 11/11] =?UTF-8?q?=E4=BF=AE=E6=AD=A3cache=E5=8A=A9=E6=89=8B?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E5=AF=B9option=E4=BC=A0=E5=8F=82=E7=9A=84?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/helper.php b/helper.php index da0dc9c5..677b47f5 100644 --- a/helper.php +++ b/helper.php @@ -354,22 +354,25 @@ if (!function_exists('cache')) { { if (is_array($options)) { // 缓存操作的同时初始化 - Cache::connect($options); + $cache = Cache::connect($options); } elseif (is_array($name)) { // 缓存初始化 return Cache::connect($name); + } else { + $cache = Cache::init(); } + if (is_null($name)) { - return Cache::clear($value); + return $cache->clear($value); } elseif ('' === $value) { // 获取缓存 - return 0 === strpos($name, '?') ? Cache::has(substr($name, 1)) : Cache::get($name); + return 0 === strpos($name, '?') ? $cache->has(substr($name, 1)) : $cache->get($name); } elseif (is_null($value)) { // 删除缓存 - return Cache::rm($name); + return $cache->rm($name); } elseif (0 === strpos($name, '?') && '' !== $value) { $expire = is_numeric($options) ? $options : null; - return Cache::remember(substr($name, 1), $value, $expire); + return $cache->remember(substr($name, 1), $value, $expire); } else { // 缓存数据 if (is_array($options)) { @@ -378,9 +381,9 @@ if (!function_exists('cache')) { $expire = is_numeric($options) ? $options : null; //默认快捷缓存设置过期时间 } if (is_null($tag)) { - return Cache::set($name, $value, $expire); + return $cache->set($name, $value, $expire); } else { - return Cache::tag($tag)->set($name, $value, $expire); + return $cache->tag($tag)->set($name, $value, $expire); } } }