From 742e2c3de33469a19f07f54d330c4ee4f172cfd5 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 6 Feb 2017 18:00:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BModel=E7=B1=BB=E7=9A=84has?= =?UTF-8?q?=E5=92=8ChasWhere=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 61baa3a7..29ed3afa 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1422,15 +1422,20 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @param mixed $operator 比较操作符 * @param integer $count 个数 * @param string $id 关联表的统计字段 - * @return Model + * @return Relation|Query */ public static function has($relation, $operator = '>=', $count = 1, $id = '*') { - $model = new static(); - if (is_array($operator) || $operator instanceof \Closure) { - return $model->$relation()->hasWhere($operator); + $model = new static(); + $relation = $model->$relation(); + if ($relation instanceof HasMany) { + if (is_array($operator) || $operator instanceof \Closure) { + return $relation->hasWhere($operator); + } + return $relation->has($operator, $count, $id); + } else { + return $relation; } - return $model->$relation()->has($operator, $count, $id); } /** @@ -1438,12 +1443,17 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @access public * @param string $relation 关联方法名 * @param mixed $where 查询条件(数组或者闭包) - * @return Model + * @return Relation|Query */ public static function hasWhere($relation, $where = []) { - $model = new static(); - return $model->$relation()->hasWhere($where); + $model = new static(); + $relation = $model->$relation(); + if ($relation instanceof HasMany) { + return $model->$relation()->hasWhere($where); + } else { + return $relation; + } } /**