From 0bffec6aaa612a3e59d7b4e03f7a44c9aa433c3b Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 25 May 2016 21:55:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 17 ++++++------- library/think/db/Connection.php | 13 +++++----- library/think/model/Relation.php | 41 ++++++++++++++++---------------- 3 files changed, 36 insertions(+), 35 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 5c98a858..787f19ff 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -740,14 +740,14 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $model = new static(); $info = $model->$relation()->getRelationInfo(); $table = $info['model']::getTable(); - switch($info['type']){ + 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); + ->having('count(' . $id . ')' . $operator . $count); case Relation::HAS_MANY_THROUGH: - // TODO + // TODO } } @@ -763,7 +763,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess { $model = new static(); $info = $model->$relation()->getRelationInfo(); - switch($info['type']){ + switch ($info['type']) { case Relation::HAS_MANY: $table = $info['model']::getTable(); if (is_array($where)) { @@ -777,9 +777,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return $model->db()->alias('a') ->field('a.*') ->join($table . ' b', 'a.' . $info['localKey'] . '=b.' . $info['foreignKey']) - ->where($where); + ->where($where); case Relation::HAS_MANY_THROUGH: - // TODO + // TODO } } @@ -907,7 +907,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @param array $alias 别名定义 * @return \think\db\Query|string */ - public function hasManyThrough($model,$through,$foreignKey='',$throughKey='',$localKey='',$alias=[]){ + public function hasManyThrough($model, $through, $foreignKey = '', $throughKey = '', $localKey = '', $alias = []) + { // 记录当前关联信息 $model = $this->parseModel($model); $through = $this->parseModel($through); @@ -915,7 +916,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; $name = Loader::parseName(basename(str_replace('\\', '/', $through))); $throughKey = $throughKey ?: $name . '_id'; - return $this->relation()->hasManyThrough($model, $through,$foreignKey,$throughKey, $localKey, $alias); + return $this->relation()->hasManyThrough($model, $through, $foreignKey, $throughKey, $localKey, $alias); } /** diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index 45f90eab..dba76bdc 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -356,8 +356,8 @@ abstract class Connection $result = $this->PDOStatement->execute(); // 调试结束 $this->debug(false); - $procedure = 0 === strpos(strtolower(substr(trim($sql),0,4)),'call'); - return $this->getResult($class,$procedure); + $procedure = 0 === strpos(strtolower(substr(trim($sql), 0, 4)), 'call'); + return $this->getResult($class, $procedure); } catch (\PDOException $e) { throw new PDOException($e, $this->config, $this->queryStr); } @@ -476,13 +476,13 @@ abstract class Connection * @param bool $procedure 是否存储过程 * @return mixed */ - protected function getResult($class = '',$procedure=false) + protected function getResult($class = '', $procedure = false) { if (true === $class) { // 返回PDOStatement对象处理 return $this->PDOStatement; } - if($procedure){ + if ($procedure) { return $this->procedure($class); } $result = $this->PDOStatement->fetchAll($this->fetchType); @@ -512,11 +512,12 @@ abstract class Connection * @param bool|string $class true 返回PDOStatement 字符串用于指定返回的类名 * @return array */ - protected function procedure($class){ + protected function procedure($class) + { $item = []; do { $result = $this->getResult($class); - if($result){ + if ($result) { $item[] = $result; } } while ($this->PDOStatement->nextRowset()); diff --git a/library/think/model/Relation.php b/library/think/model/Relation.php index cc9c1cf6..fa97e260 100644 --- a/library/think/model/Relation.php +++ b/library/think/model/Relation.php @@ -19,12 +19,11 @@ use think\model\Pivot; class Relation { - const HAS_ONE = 1; - const HAS_MANY = 2; - const HAS_MANY_THROUGH= 5; - const BELONGS_TO = 3; - const BELONGS_TO_MANY = 4; - + const HAS_ONE = 1; + const HAS_MANY = 2; + const HAS_MANY_THROUGH = 5; + const BELONGS_TO = 3; + const BELONGS_TO_MANY = 4; // 父模型对象 protected $parent; @@ -37,7 +36,7 @@ class Relation // 关联表外键 protected $foreignKey; // 中间关联表外键 - protected $throughKey; + protected $throughKey; // 关联表主键 protected $localKey; // 数据表别名 @@ -469,7 +468,7 @@ class Relation * @param array $alias 别名定义 * @return $this */ - public function hasManyThrough($model, $through,$foreignKey,$throughKey, $localKey, $alias) + public function hasManyThrough($model, $through, $foreignKey, $throughKey, $localKey, $alias) { // 记录当前关联信息 $this->type = self::HAS_MANY_THROUGH; @@ -654,25 +653,25 @@ class Relation if ($this->model) { $model = new $this->model; $db = $model->db(); - switch($this->type){ + switch ($this->type) { case self::HAS_MANY: if (isset($this->parent->{$this->localKey})) { // 关联查询带入关联条件 $db->where($this->foreignKey, $this->parent->{$this->localKey}); - } + } break; case self::HAS_MANY_THROUGH: - $through = $this->middle; - $model = $this->model; - $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($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}); + $through = $this->middle; + $model = $this->model; + $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($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);