From fcfe91a97d5e06b41d4763775e0f72b1bb206f22 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 4 Feb 2017 20:08:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3hasMany=E5=85=B3=E8=81=94?= =?UTF-8?q?=E7=9A=84has=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/HasMany.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index 3094bb55..9bcdcead 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -45,7 +45,7 @@ class HasMany extends Relation public function getRelation($subRelation = '', $closure = null) { if ($closure) { - call_user_func_array($closure, [& $this->query]); + call_user_func_array($closure, [ & $this->query]); } return $this->relation($subRelation)->select(); } @@ -125,7 +125,7 @@ class HasMany extends Relation $count = 0; if (isset($result->$localKey)) { if ($closure) { - call_user_func_array($closure, [& $this->query]); + call_user_func_array($closure, [ & $this->query]); } $count = $this->query->where([$this->foreignKey => $result->$localKey])->count(); } @@ -141,14 +141,14 @@ class HasMany extends Relation public function getRelationCountQuery($closure) { if ($closure) { - call_user_func_array($closure, [& $this->query]); + call_user_func_array($closure, [ & $this->query]); } return $this->query->where([ $this->foreignKey => [ 'exp', - '=' . $this->parent->getTable() . '.' . $this->parent->getPk() - ] + '=' . $this->parent->getTable() . '.' . $this->parent->getPk(), + ], ])->fetchSql()->count(); } @@ -167,7 +167,7 @@ class HasMany extends Relation $foreignKey = $this->foreignKey; // 预载入关联查询 支持嵌套预载入 if ($closure) { - call_user_func_array($closure, [& $model]); + call_user_func_array($closure, [ & $model]); } $list = $model->where($where)->with($subRelation)->select(); @@ -217,13 +217,14 @@ class HasMany extends Relation * @param string $operator 比较操作符 * @param integer $count 个数 * @param string $id 关联表的统计字段 + * @param string $joinType JOIN类型 * @return Query */ - public function has($operator = '>=', $count = 1, $id = '*') + public function has($operator = '>=', $count = 1, $id = '*', $joinType = 'INNER') { $table = $this->query->getTable(); return $this->parent->db()->alias('a') - ->join($table . ' b', 'a.' . $this->localKey . '=b.' . $this->foreignKey, $this->joinType) + ->join($table . ' b', 'a.' . $this->localKey . '=b.' . $this->foreignKey, $joinType) ->group('b.' . $this->foreignKey) ->having('count(' . $id . ')' . $operator . $count); }