From c9846a51e131482b16a861ab028b3ff620893333 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 29 May 2016 12:09:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BRelation=E7=B1=BB=E7=9A=84get?= =?UTF-8?q?Relation=E6=96=B9=E6=B3=95=20=E6=94=AF=E6=8C=81=E5=9C=A8?= =?UTF-8?q?=E5=85=B3=E8=81=94=E5=AE=9A=E4=B9=89=E6=96=B9=E6=B3=95=E4=B8=AD?= =?UTF-8?q?=E4=BD=BF=E7=94=A8where=E6=9D=A1=E4=BB=B6=E6=88=96=E8=80=85?= =?UTF-8?q?=E5=85=B6=E4=BB=96=E7=9A=84=E6=93=8D=E4=BD=9CQuery=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=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.php | 73 +++++++++++++++----------------- 1 file changed, 35 insertions(+), 38 deletions(-) diff --git a/library/think/model/Relation.php b/library/think/model/Relation.php index fa97e260..ac88e6a0 100644 --- a/library/think/model/Relation.php +++ b/library/think/model/Relation.php @@ -78,48 +78,45 @@ class Relation $relation = $this->parent->$name(); $foreignKey = $this->foreignKey; $localKey = $this->localKey; - if ($relation instanceof Relation) { - // 判断关联类型执行查询 - switch ($this->type) { - case self::HAS_ONE: - $result = $relation->where($foreignKey, $this->parent->$localKey)->find(); - break; - case self::BELONGS_TO: - $result = $relation->where($localKey, $this->parent->$foreignKey)->find(); - break; - case self::HAS_MANY: - $result = $relation->select(); - break; - case self::HAS_MANY_THROUGH: - $result = $relation->select(); - break; - case self::BELONGS_TO_MANY: - // 关联查询 - $pk = $this->parent->getPk(); - $condition['pivot.' . $localKey] = $this->parent->$pk; - $result = $this->belongsToManyQuery($relation, $this->middle, $foreignKey, $localKey, $condition)->select(); - foreach ($result as $set) { - $pivot = []; - foreach ($set->toArray() as $key => $val) { - if (strpos($key, '__')) { - list($name, $attr) = explode('__', $key, 2); - if ('pivot' == $name) { - $pivot[$attr] = $val; - unset($set->$key); - } + + // 判断关联类型执行查询 + switch ($this->type) { + case self::HAS_ONE: + $result = $relation->where($foreignKey, $this->parent->$localKey)->find(); + break; + case self::BELONGS_TO: + $result = $relation->where($localKey, $this->parent->$foreignKey)->find(); + break; + case self::HAS_MANY: + $result = $relation->select(); + break; + case self::HAS_MANY_THROUGH: + $result = $relation->select(); + break; + case self::BELONGS_TO_MANY: + // 关联查询 + $pk = $this->parent->getPk(); + $condition['pivot.' . $localKey] = $this->parent->$pk; + $result = $this->belongsToManyQuery($relation, $this->middle, $foreignKey, $localKey, $condition)->select(); + foreach ($result as $set) { + $pivot = []; + foreach ($set->toArray() as $key => $val) { + if (strpos($key, '__')) { + list($name, $attr) = explode('__', $key, 2); + if ('pivot' == $name) { + $pivot[$attr] = $val; + unset($set->$key); } } - $set->pivot = new Pivot($pivot, $this->middle); } - break; - default: - // 直接返回 - $result = $relation; - } - return $result; - } else { - return $relation; + $set->pivot = new Pivot($pivot, $this->middle); + } + break; + default: + // 直接返回 + $result = $relation; } + return $result; } /**