From 513701788ef223b8342537546b29bceaaf0cf1f0 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 16 Apr 2018 12:46:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E5=85=B3=E8=81=94withCount?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/BelongsToMany.php | 3 ++- library/think/model/relation/HasMany.php | 9 ++------- library/think/model/relation/MorphMany.php | 3 ++- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/library/think/model/relation/BelongsToMany.php b/library/think/model/relation/BelongsToMany.php index 2f451a72..c9845a9e 100644 --- a/library/think/model/relation/BelongsToMany.php +++ b/library/think/model/relation/BelongsToMany.php @@ -12,6 +12,7 @@ namespace think\model\relation; use think\Collection; +use think\Db; use think\db\Query; use think\Exception; use think\Loader; @@ -338,7 +339,7 @@ class BelongsToMany extends Relation return $this->belongsToManyQuery($this->foreignKey, $this->localKey, [ 'pivot.' . $this->localKey => [ 'exp', - '=' . $this->parent->getTable() . '.' . $this->parent->getPk(), + Db::raw('=' . $this->parent->getTable() . '.' . $this->parent->getPk()), ], ])->fetchSql()->count(); } diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index 3a9b111d..cfcf2a98 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -143,7 +143,7 @@ class HasMany extends Relation if ($closure) { call_user_func_array($closure, [ & $this->query]); } - $count = $this->query->where([$this->foreignKey => $result->$localKey])->count(); + $count = $this->query->where($this->foreignKey, $result->$localKey)->count(); } return $count; } @@ -160,12 +160,7 @@ class HasMany extends Relation call_user_func_array($closure, [ & $this->query]); } $localKey = $this->localKey ?: $this->parent->getPk(); - return $this->query->where([ - $this->foreignKey => [ - 'exp', - '=' . $this->parent->getTable() . '.' . $localKey, - ], - ])->fetchSql()->count(); + return $this->query->whereExp($this->foreignKey, '=' . $this->parent->getTable() . '.' . $localKey)->fetchSql()->count(); } /** diff --git a/library/think/model/relation/MorphMany.php b/library/think/model/relation/MorphMany.php index 23f7c52a..8945042f 100644 --- a/library/think/model/relation/MorphMany.php +++ b/library/think/model/relation/MorphMany.php @@ -11,6 +11,7 @@ namespace think\model\relation; +use think\Db; use think\db\Query; use think\Exception; use think\Loader; @@ -201,7 +202,7 @@ class MorphMany extends Relation return $this->query->where([ $this->morphKey => [ 'exp', - '=' . $this->parent->getTable() . '.' . $this->parent->getPk(), + Db::raw('=' . $this->parent->getTable() . '.' . $this->parent->getPk()), ], $this->morphType => $this->type, ])->fetchSql()->count();