From 88599fe33d8537fe71939c2de03c04928ae21b20 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 1 Dec 2017 11:43:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E7=9A=84hasWhere=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=A2=9E=E5=8A=A0fields=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 | 9 +++++---- library/think/model/relation/BelongsTo.php | 14 +++++++++----- library/think/model/relation/BelongsToMany.php | 5 +++-- library/think/model/relation/HasMany.php | 11 ++++++++--- library/think/model/relation/HasManyThrough.php | 5 +++-- library/think/model/relation/HasOne.php | 10 +++++++--- library/think/model/relation/MorphMany.php | 5 +++-- library/think/model/relation/MorphOne.php | 5 +++-- library/think/model/relation/MorphTo.php | 5 +++-- 9 files changed, 44 insertions(+), 25 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 63e1622f..31ef5e4c 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1780,13 +1780,14 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 根据关联条件查询当前模型 * @access public - * @param string $relation 关联方法名 - * @param mixed $where 查询条件(数组或者闭包) + * @param string $relation 关联方法名 + * @param mixed $where 查询条件(数组或者闭包) + * @param mixed $fields 字段 * @return Relation|Query */ - public static function hasWhere($relation, $where = []) + public static function hasWhere($relation, $where = [], $fields = null) { - return (new static())->$relation()->hasWhere($where); + return (new static())->$relation()->hasWhere($where, $fields); } /** diff --git a/library/think/model/relation/BelongsTo.php b/library/think/model/relation/BelongsTo.php index 45118fc9..415c0c36 100644 --- a/library/think/model/relation/BelongsTo.php +++ b/library/think/model/relation/BelongsTo.php @@ -79,14 +79,16 @@ class BelongsTo extends OneToOne /** * 根据关联条件查询当前模型 * @access public - * @param mixed $where 查询条件(数组或者闭包) + * @param mixed $where 查询条件(数组或者闭包) + * @param mixed $fields 字段 * @return Query */ - public function hasWhere($where = []) + public function hasWhere($where = [], $fields = null) { $table = $this->query->getTable(); $model = basename(str_replace('\\', '/', get_class($this->parent))); $relation = basename(str_replace('\\', '/', $this->model)); + if (is_array($where)) { foreach ($where as $key => $val) { if (false === strpos($key, '.')) { @@ -95,8 +97,10 @@ class BelongsTo extends OneToOne } } } + $fields = $this->getRelationQueryFields($fields, $model); + return $this->parent->db()->alias($model) - ->field($model . '.*') + ->field($fields) ->group($model . '.' . $this->foreignKey) ->join($table . ' ' . $relation, $model . '.' . $this->foreignKey . '=' . $relation . '.' . $this->localKey, $this->joinType) ->where($where); @@ -125,7 +129,7 @@ class BelongsTo extends OneToOne } if (!empty($range)) { - $data = $this->eagerlyWhere($this, [ + $data = $this->eagerlyWhere($this->query, [ $localKey => [ 'in', $range, @@ -168,7 +172,7 @@ class BelongsTo extends OneToOne { $localKey = $this->localKey; $foreignKey = $this->foreignKey; - $data = $this->eagerlyWhere($this, [$localKey => $result->$foreignKey], $localKey, $relation, $subRelation, $closure); + $data = $this->eagerlyWhere($this->query, [$localKey => $result->$foreignKey], $localKey, $relation, $subRelation, $closure); // 关联模型 if (!isset($data[$result->$foreignKey])) { $relationModel = null; diff --git a/library/think/model/relation/BelongsToMany.php b/library/think/model/relation/BelongsToMany.php index 1a2a7ebd..2fc00ae8 100644 --- a/library/think/model/relation/BelongsToMany.php +++ b/library/think/model/relation/BelongsToMany.php @@ -212,11 +212,12 @@ class BelongsToMany extends Relation /** * 根据关联条件查询当前模型 * @access public - * @param mixed $where 查询条件(数组或者闭包) + * @param mixed $where 查询条件(数组或者闭包) + * @param mixed $fields 字段 * @return Query * @throws Exception */ - public function hasWhere($where = []) + public function hasWhere($where = [], $fields = null) { throw new Exception('relation not support: hasWhere'); } diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index a27ec2ea..b132ea83 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -253,14 +253,16 @@ class HasMany extends Relation /** * 根据关联条件查询当前模型 * @access public - * @param mixed $where 查询条件(数组或者闭包) + * @param mixed $where 查询条件(数组或者闭包) + * @param mixed $fields 字段 * @return Query */ - public function hasWhere($where = []) + public function hasWhere($where = [], $fields = null) { $table = $this->query->getTable(); $model = basename(str_replace('\\', '/', get_class($this->parent))); $relation = basename(str_replace('\\', '/', $this->model)); + if (is_array($where)) { foreach ($where as $key => $val) { if (false === strpos($key, '.')) { @@ -269,8 +271,11 @@ class HasMany extends Relation } } } + + $fields = $this->getRelationQueryFields($fields, $model); + return $this->parent->db()->alias($model) - ->field($model . '.*') + ->field($fields) ->group($model . '.' . $this->localKey) ->join($table . ' ' . $relation, $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey) ->where($where); diff --git a/library/think/model/relation/HasManyThrough.php b/library/think/model/relation/HasManyThrough.php index 7c268b40..dc0985c0 100644 --- a/library/think/model/relation/HasManyThrough.php +++ b/library/think/model/relation/HasManyThrough.php @@ -77,10 +77,11 @@ class HasManyThrough extends Relation /** * 根据关联条件查询当前模型 * @access public - * @param mixed $where 查询条件(数组或者闭包) + * @param mixed $where 查询条件(数组或者闭包) + * @param mixed $fields 字段 * @return Query */ - public function hasWhere($where = []) + public function hasWhere($where = [], $fields = null) { throw new Exception('relation not support: hasWhere'); } diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index 7e8fa8d6..ec815969 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -81,14 +81,16 @@ class HasOne extends OneToOne /** * 根据关联条件查询当前模型 * @access public - * @param mixed $where 查询条件(数组或者闭包) + * @param mixed $where 查询条件(数组或者闭包) + * @param mixed $fields 字段 * @return Query */ - public function hasWhere($where = []) + public function hasWhere($where = [], $fields = null) { $table = $this->query->getTable(); $model = basename(str_replace('\\', '/', get_class($this->parent))); $relation = basename(str_replace('\\', '/', $this->model)); + if (is_array($where)) { foreach ($where as $key => $val) { if (false === strpos($key, '.')) { @@ -97,8 +99,10 @@ class HasOne extends OneToOne } } } + $fields = $this->getRelationQueryFields($fields, $model); + return $this->parent->db()->alias($model) - ->field($model . '.*') + ->field($fields) ->join($table . ' ' . $relation, $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey, $this->joinType) ->where($where); } diff --git a/library/think/model/relation/MorphMany.php b/library/think/model/relation/MorphMany.php index 87c2d66c..2d305354 100644 --- a/library/think/model/relation/MorphMany.php +++ b/library/think/model/relation/MorphMany.php @@ -82,10 +82,11 @@ class MorphMany extends Relation /** * 根据关联条件查询当前模型 * @access public - * @param mixed $where 查询条件(数组或者闭包) + * @param mixed $where 查询条件(数组或者闭包) + * @param mixed $fields 字段 * @return Query */ - public function hasWhere($where = []) + public function hasWhere($where = [], $fields = null) { throw new Exception('relation not support: hasWhere'); } diff --git a/library/think/model/relation/MorphOne.php b/library/think/model/relation/MorphOne.php index 4dfd1c0f..a47ff2e7 100644 --- a/library/think/model/relation/MorphOne.php +++ b/library/think/model/relation/MorphOne.php @@ -81,10 +81,11 @@ class MorphOne extends Relation /** * 根据关联条件查询当前模型 * @access public - * @param mixed $where 查询条件(数组或者闭包) + * @param mixed $where 查询条件(数组或者闭包) + * @param mixed $fields 字段 * @return Query */ - public function hasWhere($where = []) + public function hasWhere($where = [], $fields = null) { throw new Exception('relation not support: hasWhere'); } diff --git a/library/think/model/relation/MorphTo.php b/library/think/model/relation/MorphTo.php index e1a9db5b..b92a1f07 100644 --- a/library/think/model/relation/MorphTo.php +++ b/library/think/model/relation/MorphTo.php @@ -82,10 +82,11 @@ class MorphTo extends Relation /** * 根据关联条件查询当前模型 * @access public - * @param mixed $where 查询条件(数组或者闭包) + * @param mixed $where 查询条件(数组或者闭包) + * @param mixed $fields 字段 * @return Query */ - public function hasWhere($where = []) + public function hasWhere($where = [], $fields = null) { throw new Exception('relation not support: hasWhere'); }