From cdfe0a5e967f3674127d7c36354f8b68da4e556d Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 8 May 2016 22:20:17 +0800 Subject: [PATCH] =?UTF-8?q?Model=E7=B1=BB=E7=9A=84get=E5=92=8Call=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E6=94=AF=E6=8C=81=E7=9B=B4=E6=8E=A5=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E6=9D=A1=E4=BB=B6=E6=9F=A5=E8=AF=A2=20?= =?UTF-8?q?=E6=94=B9=E8=BF=9Brelation=E7=9A=84=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 14 ++++++++------ library/think/model/Relation.php | 22 +++++++++++----------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index d140d839..9636c8b1 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -598,7 +598,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected static function parseQuery(&$data, $with, $cache) { $result = self::with($with)->cache($cache); - if ($data instanceof \Closure) { + if (is_array($data)) { + $result = self::db()->where($data); + } elseif ($data instanceof \Closure) { call_user_func_array($data, [ & $result]); $data = []; } elseif ($data instanceof Query) { @@ -832,19 +834,19 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @access public * @param string $model 模型名 * @param string $table 中间表名 + * @param string $foreignKey 关联外键 * @param string $localKey 当前模型关联键 - * @param string $foreignKey 关联模型关联键 * @return \think\db\Query|string */ - public function belongsToMany($model, $table = '', $localKey = '', $foreignKey = '') + public function belongsToMany($model, $table = '', $foreignKey = '', $localKey = '') { // 记录当前关联信息 $model = $this->parseModel($model); $name = Loader::parseName(basename(str_replace('\\', '/', $model))); $table = $table ?: Db::name(Loader::parseName($this->name) . '_' . $name)->getTable(); - $localKey = $localKey ?: $name . '_id'; - $foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; - return $this->relation->belongsToMany($model, $table, $localKey, $foreignKey); + $foreignKey = $foreignKey ?: $name . '_id'; + $localKey = $localKey ?: Loader::parseName($this->name) . '_id'; + return $this->relation->belongsToMany($model, $table, $foreignKey, $localKey); } /** diff --git a/library/think/model/Relation.php b/library/think/model/Relation.php index 5065500b..22223dd2 100644 --- a/library/think/model/Relation.php +++ b/library/think/model/Relation.php @@ -88,7 +88,7 @@ class Relation // 关联查询 $pk = $this->parent->getPk(); $condition['pivot.' . $foreignKey] = $this->parent->$pk; - $result = $this->belongsToManyQuery($relation, $this->middle, $localKey, $foreignKey, $condition)->select(); + $result = $this->belongsToManyQuery($relation, $this->middle, $foreignKey, $localKey, $condition)->select(); foreach ($result as $set) { $pivot = []; foreach ($set->toArray() as $key => $val) { @@ -333,7 +333,7 @@ class Relation $foreignKey = $this->foreignKey; $localKey = $this->localKey; // 预载入关联查询 支持嵌套预载入 - $list = $this->belongsToManyQuery($model, $this->middle, $localKey, $foreignKey, $where)->with($subRelation)->select(); + $list = $this->belongsToManyQuery($model, $this->middle, $foreignKey, $localKey, $where)->with($subRelation)->select(); // 组装模型数据 $data = []; @@ -360,7 +360,7 @@ class Relation * @param string $model 模型名 * @param string $foreignKey 关联外键 * @param string $localKey 关联主键 - * @return \think\db\Query|string + * @return $this */ public function hasOne($model, $foreignKey, $localKey) { @@ -379,7 +379,7 @@ class Relation * @param string $model 模型名 * @param string $foreignKey 关联外键 * @param string $otherKey 关联主键 - * @return \think\db\Query|string + * @return $this */ public function belongsTo($model, $foreignKey, $otherKey) { @@ -399,7 +399,7 @@ class Relation * @param string $model 模型名 * @param string $foreignKey 关联外键 * @param string $localKey 关联主键 - * @return \think\db\Query|string + * @return $this */ public function hasMany($model, $foreignKey, $localKey) { @@ -418,11 +418,11 @@ class Relation * @access public * @param string $model 模型名 * @param string $table 中间表名 + * @param string $foreignKey 关联模型外键 * @param string $localKey 当前模型关联键 - * @param string $foreignKey 关联模型关联键 - * @return \think\db\Query|string + * @return $this */ - public function belongsToMany($model, $table, $localKey, $foreignKey) + public function belongsToMany($model, $table, $foreignKey, $localKey) { // 记录当前关联信息 $this->type = self::BELONGS_TO_MANY; @@ -440,19 +440,19 @@ class Relation * @access public * @param object $model 关联模型对象 * @param string $table 中间表名 - * @param string $localKey 当前模型关联键 * @param string $foreignKey 关联模型关联键 + * @param string $localKey 当前模型关联键 * @param array $condition 关联查询条件 * @return \think\db\Query|string */ - protected function belongsToManyQuery($model, $table, $localKey, $foreignKey, $condition = []) + protected function belongsToManyQuery($model, $table, $foreignKey, $localKey, $condition = []) { // 关联查询封装 $tableName = $model->getTable(); $relationFk = $model->getPk(); return $model::field($tableName . '.*') ->field(true, false, $table, 'pivot', 'pivot__') - ->join($table . ' pivot', 'pivot.' . $localKey . '=' . $tableName . '.' . $relationFk) + ->join($table . ' pivot', 'pivot.' . $foreignKey . '=' . $tableName . '.' . $relationFk) ->where($condition); }