From b4823a60f35f0011117197c6e785d51f11939142 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 14 Jul 2016 16:34:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BRelation=E7=B1=BB=E7=9A=84?= =?UTF-8?q?=E5=85=B3=E8=81=94=E5=AE=9A=E4=B9=89=E6=96=B9=E6=B3=95=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E5=AF=B9=E8=B1=A1=20=E7=BB=9F=E4=B8=80=E8=BF=94?= =?UTF-8?q?=E5=9B=9ERelation=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/Relation.php | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/library/think/model/Relation.php b/library/think/model/Relation.php index d762afd5..515bf362 100644 --- a/library/think/model/Relation.php +++ b/library/think/model/Relation.php @@ -43,6 +43,8 @@ class Relation protected $alias; // 当前关联的JOIN类型 protected $joinType; + // 关联模型查询对象 + protected $query; /** * 架构函数 @@ -409,7 +411,7 @@ class Relation $this->localKey = $localKey; $this->alias = $alias; $this->joinType = $joinType; - + $this->query = (new $model)->db(); // 返回关联的模型对象 return $this; } @@ -433,7 +435,7 @@ class Relation $this->localKey = $otherKey; $this->alias = $alias; $this->joinType = $joinType; - + $this->query = (new $model)->db(); // 返回关联的模型对象 return $this; } @@ -455,7 +457,7 @@ class Relation $this->foreignKey = $foreignKey; $this->localKey = $localKey; $this->alias = $alias; - + $this->query = (new $model)->db(); // 返回关联的模型对象 return $this; } @@ -481,7 +483,7 @@ class Relation $this->throughKey = $throughKey; $this->localKey = $localKey; $this->alias = $alias; - + $this->query = (new $model)->db(); // 返回关联的模型对象 return $this; } @@ -505,7 +507,7 @@ class Relation $this->localKey = $localKey; $this->middle = $table; $this->alias = $alias; - + $this->query = (new $model)->db(); // 返回关联的模型对象 return $this; } @@ -653,14 +655,12 @@ class Relation public function __call($method, $args) { - if ($this->model) { - $model = new $this->model; - $db = $model->db(); + if ($this->query) { switch ($this->type) { case self::HAS_MANY: if (isset($this->parent->{$this->localKey})) { // 关联查询带入关联条件 - $db->where($this->foreignKey, $this->parent->{$this->localKey}); + $this->query->where($this->foreignKey, $this->parent->{$this->localKey}); } break; case self::HAS_MANY_THROUGH: @@ -671,13 +671,18 @@ class Relation $pk = (new $this->model)->getPk(); $throughKey = $this->throughKey; $modelTable = $this->parent->getTable(); - $result = $db->field($alias . '.*')->alias($alias) + $result = $this->query->field($alias . '.*')->alias($alias) ->join($throughTable, $throughTable . '.' . $pk . '=' . $alias . '.' . $throughKey) ->join($modelTable, $modelTable . '.' . $this->localKey . '=' . $throughTable . '.' . $this->foreignKey) ->where($throughTable . '.' . $this->foreignKey, $this->parent->{$this->localKey}); break; } - return call_user_func_array([$db, $method], $args); + $result = call_user_func_array([$this->query, $method], $args); + if ($result instanceof \think\db\Query) { + return $this; + } else { + return $result; + } } else { throw new Exception('method not exists:' . __CLASS__ . '->' . $method); }