From bfa6c185f3b476602486ebb2fbf74e1ed78fc220 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 12 Apr 2016 17:38:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3Model=E7=B1=BB=E5=85=B3?= =?UTF-8?q?=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.php | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 4debeb8e..a7fde996 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -65,6 +65,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected $isUpdate = false; // 当前执行的关联类型 private $relation; + // 是否预载入 + protected $eagerly = false; /** * 初始化过的模型. @@ -787,10 +789,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public function eagerlyResultSet($resultSet, $relation) { - $relations = is_string($relation) ? explode(',', $relation) : $relation; + $this->eagerly = true; + $relations = is_string($relation) ? explode(',', $relation) : $relation; foreach ($relations as $relation) { - $subRelation = null; if (strpos($relation, '.')) { list($relation, $subRelation) = explode('.', $relation); } @@ -862,6 +864,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } $this->relation = []; } + $this->eagerly = false; return $resultSet; } @@ -872,9 +875,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @param string $relation 关联名 * @return Model */ - public function eagerlyResult($result, $with) + public function eagerlyResult($result, $relation) { - $relations = is_string($relation) ? explode(',', $relation) : $relation; + $this->eagerly = true; + $relations = is_string($relation) ? explode(',', $relation) : $relation; foreach ($relations as $relation) { if (strpos($relation, '.')) { @@ -942,6 +946,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } $this->relation = []; } + $this->eagerly = false; return $result; } @@ -959,7 +964,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $localKey = $localKey ?: $this->pk; $foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; $this->relation = [self::HAS_ONE, $foreignKey, $localKey]; - if (isset($this->data[$localKey])) { + + if (!$this->eagerly && isset($this->data[$localKey])) { // 关联查询封装 return $model::where($foreignKey, $this->data[$localKey]); } else { @@ -982,7 +988,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $foreignKey = $foreignKey ?: $this->pk; $localKey = $localKey ?: Loader::parseName(basename(str_replace('\\', '/', $model))) . '_id'; $this->relation = [self::BELONGS_TO, $foreignKey, $localKey]; - if (isset($this->data[$localKey])) { + + if (!$this->eagerly && isset($this->data[$localKey])) { // 关联查询封装 return $model::where($foreignKey, $this->data[$localKey]); } else { @@ -1005,7 +1012,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; $this->relation = [self::HAS_MANY, $foreignKey, $localKey]; - if (isset($this->data[$localKey])) { + if (!$this->eagerly && isset($this->data[$localKey])) { // 关联查询封装 return $model::where($foreignKey, $this->data[$localKey]); } else { @@ -1028,7 +1035,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $localKey = $localKey ?: Loader::parseName(basename(str_replace('\\', '/', $model))) . '_id'; $this->relation = [self::BELONGS_TO_MANY, $foreignKey, $localKey]; - if (isset($this->data[$localKey])) { + if (!$this->eagerly && isset($this->data[$localKey])) { // 关联查询封装 return $model::where($foreignKey, $this->data[$localKey]); } else {