From 15de3e328a94d8c7f1c27073d7db4531e4862951 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 25 May 2016 18:17:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E8=BF=9C=E7=A8=8B=E4=B8=80?= =?UTF-8?q?=E5=AF=B9=E5=A4=9A=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 41 ++++++++++++++++++++------------ library/think/model/Relation.php | 6 ++--- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index fcb94814..5c98a858 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -740,10 +740,16 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $model = new static(); $info = $model->$relation()->getRelationInfo(); $table = $info['model']::getTable(); - return $model->db()->alias('a') - ->join($table . ' b', 'a.' . $info['localKey'] . '=b.' . $info['foreignKey']) - ->group('b.' . $info['foreignKey']) - ->having('count(' . $id . ')' . $operator . $count); + switch($info['type']){ + case Relation::HAS_MANY: + return $model->db()->alias('a') + ->join($table . ' b', 'a.' . $info['localKey'] . '=b.' . $info['foreignKey']) + ->group('b.' . $info['foreignKey']) + ->having('count(' . $id . ')' . $operator . $count); + case Relation::HAS_MANY_THROUGH: + // TODO + } + } /** @@ -757,19 +763,24 @@ abstract class Model implements \JsonSerializable, \ArrayAccess { $model = new static(); $info = $model->$relation()->getRelationInfo(); - $table = $info['model']::getTable(); - if (is_array($where)) { - foreach ($where as $key => $val) { - if (false === strpos($key, '.')) { - $where['b.' . $key] = $val; - unset($where[$key]); + switch($info['type']){ + case Relation::HAS_MANY: + $table = $info['model']::getTable(); + if (is_array($where)) { + foreach ($where as $key => $val) { + if (false === strpos($key, '.')) { + $where['b.' . $key] = $val; + unset($where[$key]); + } + } } - } + return $model->db()->alias('a') + ->field('a.*') + ->join($table . ' b', 'a.' . $info['localKey'] . '=b.' . $info['foreignKey']) + ->where($where); + case Relation::HAS_MANY_THROUGH: + // TODO } - return $model->db()->alias('a') - ->field('a.*') - ->join($table . ' b', 'a.' . $info['localKey'] . '=b.' . $info['foreignKey']) - ->where($where); } /** diff --git a/library/think/model/Relation.php b/library/think/model/Relation.php index 52d2d556..cc9c1cf6 100644 --- a/library/think/model/Relation.php +++ b/library/think/model/Relation.php @@ -664,13 +664,13 @@ class Relation case self::HAS_MANY_THROUGH: $through = $this->middle; $model = $this->model; - $table = $model::getTable(); + $alias = Loader::parseName(basename(str_replace('\\', '/', $model))); $throughTable = $through::getTable(); $pk = (new $this->model)->getPk(); $throughKey = $this->throughKey; $modelTable = $this->parent->getTable(); - $result = $db->field($table.'.*') - ->join($throughTable,$throughTable.'.'.$pk.'='.$table.'.'.$throughKey) + $result = $db->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;