From 88509f0826b32e82347bf6904d170a775b5c760e Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 29 Oct 2024 13:54:10 +0800 Subject: [PATCH] =?UTF-8?q?getTable=E6=96=B9=E6=B3=95=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E6=95=B0=E6=8D=AE=E8=A1=A8=E5=88=AB=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/BaseQuery.php | 17 +++++++++-------- src/db/concern/JoinAndViewQuery.php | 9 +++------ src/model/relation/BelongsTo.php | 4 ++-- src/model/relation/BelongsToMany.php | 6 +++--- src/model/relation/HasMany.php | 4 ++-- src/model/relation/HasOne.php | 4 ++-- src/model/relation/MorphMany.php | 2 +- src/model/relation/MorphToMany.php | 2 +- 8 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/db/BaseQuery.php b/src/db/BaseQuery.php index 4b353d66..b653e7b3 100644 --- a/src/db/BaseQuery.php +++ b/src/db/BaseQuery.php @@ -277,20 +277,21 @@ public function getConfig(string $name = '') /** * 得到当前或者指定名称的数据表. - * - * @param string $name 不含前缀的数据表名字 + * @param bool $alias 是否返回数据表别名 * * @return string|array|Raw */ - public function getTable(string $name = '') + public function getTable(bool $alias = false) { - if (empty($name) && isset($this->options['table'])) { - return $this->options['table']; + if (isset($this->options['table'])) { + $table = $this->options['table']; + if ($alias && is_string($table) && !empty($this->options['alias'][$table])) { + return $this->options['alias'][$table]; + } + return $table; } - $name = $name ?: $this->name; - - return $this->prefix . Str::snake($name) . $this->suffix; + return $this->prefix . Str::snake($this->name) . $this->suffix; } /** diff --git a/src/db/concern/JoinAndViewQuery.php b/src/db/concern/JoinAndViewQuery.php index c1a8e487..51b57d5b 100644 --- a/src/db/concern/JoinAndViewQuery.php +++ b/src/db/concern/JoinAndViewQuery.php @@ -14,6 +14,7 @@ namespace think\db\concern; use think\db\Raw; +use think\helper\Str; /** * JOIN和VIEW查询. @@ -103,16 +104,12 @@ protected function getJoinTable(array | string | Raw $join, ?string &$alias = nu return $table; } - if ($join instanceof Raw) { + if ($join instanceof Raw || str_contains($join, '(')) { return $join; } $join = trim($join); - if (str_contains($join, '(')) { - // 使用子查询 - return $join; - } // 使用别名 if (str_contains($join, ' ')) { // 使用别名 @@ -125,7 +122,7 @@ protected function getJoinTable(array | string | Raw $join, ?string &$alias = nu } if ($this->prefix && !str_contains($table, '.') && !str_starts_with($table, $this->prefix)) { - $table = $this->getTable($table); + $table = $this->prefix . Str::snake($table) . $this->suffix; } if (!empty($alias) && $table != $alias) { diff --git a/src/model/relation/BelongsTo.php b/src/model/relation/BelongsTo.php index 5f751416..78509b5f 100644 --- a/src/model/relation/BelongsTo.php +++ b/src/model/relation/BelongsTo.php @@ -98,9 +98,9 @@ public function getRelationCountQuery(?Closure $closure = null, string $aggregat } return $this->query - ->whereExp($this->localKey, '=' . $this->parent->getTable() . '.' . $this->foreignKey) + ->whereExp($this->localKey, '=' . $this->parent->getTable(true) . '.' . $this->foreignKey) ->fetchSql() - ->$aggregate($field); + ->$aggregate($this->localKey); } /** diff --git a/src/model/relation/BelongsToMany.php b/src/model/relation/BelongsToMany.php index aff47987..140b5a53 100644 --- a/src/model/relation/BelongsToMany.php +++ b/src/model/relation/BelongsToMany.php @@ -384,7 +384,7 @@ public function getRelationCountQuery(?Closure $closure = null, string $aggregat return $this->belongsToManyQuery($this->foreignKey, $this->localKey, [ [ - 'pivot.' . $this->localKey, 'exp', new Raw('=' . $this->parent->db(false)->getTable() . '.' . $this->parent->getPk()), + 'pivot.' . $this->localKey, 'exp', new Raw('=' . $this->parent->db(false)->getTable(true) . '.' . $this->parent->getPk()), ], ])->fetchSql()->$aggregate($field); } @@ -445,7 +445,7 @@ protected function belongsToManyQuery(string $foreignKey, string $localKey, arra { // 关联查询封装 if (empty($this->baseQuery)) { - $tableName = $this->query->getTable(); + $tableName = $this->query->getTable(true); $table = $this->pivot->db()->getTable(); $fields = $this->getQueryFields($tableName); @@ -679,7 +679,7 @@ protected function baseQuery(): void // 关联查询 if (null === $this->parent->getKey()) { - $condition = ['pivot.' . $localKey, 'exp', new Raw('=' . $this->parent->getTable() . '.' . $this->parent->getPk())]; + $condition = ['pivot.' . $localKey, 'exp', new Raw('=' . $this->parent->getTable(true) . '.' . $this->parent->getPk())]; } else { $condition = ['pivot.' . $localKey, '=', $this->parent->getKey()]; } diff --git a/src/model/relation/HasMany.php b/src/model/relation/HasMany.php index afd1235c..4e2150cd 100644 --- a/src/model/relation/HasMany.php +++ b/src/model/relation/HasMany.php @@ -181,9 +181,9 @@ public function getRelationCountQuery(?Closure $closure = null, string $aggregat } return $this->query->alias($aggregate . '_table') - ->whereExp($aggregate . '_table.' . $this->foreignKey, '=' . $this->parent->getTable() . '.' . $this->localKey) + ->whereExp($aggregate . '_table.' . $this->foreignKey, '=' . $this->parent->getTable(true) . '.' . $this->localKey) ->fetchSql() - ->$aggregate($field); + ->$aggregate($this->localKey); } /** diff --git a/src/model/relation/HasOne.php b/src/model/relation/HasOne.php index de9e2ad3..ce983f29 100644 --- a/src/model/relation/HasOne.php +++ b/src/model/relation/HasOne.php @@ -98,9 +98,9 @@ public function getRelationCountQuery(?Closure $closure = null, string $aggregat } return $this->query - ->whereExp($this->foreignKey, '=' . $this->parent->getTable() . '.' . $this->localKey) + ->whereExp($this->foreignKey, '=' . $this->parent->getTable(true) . '.' . $this->localKey) ->fetchSql() - ->$aggregate($field); + ->$aggregate($this->localKey); } /** diff --git a/src/model/relation/MorphMany.php b/src/model/relation/MorphMany.php index 10d8af43..c2e36479 100644 --- a/src/model/relation/MorphMany.php +++ b/src/model/relation/MorphMany.php @@ -238,7 +238,7 @@ public function getRelationCountQuery(?Closure $closure = null, string $aggregat } return $this->query - ->whereExp($this->morphKey, '=' . $this->parent->getTable() . '.' . $this->parent->getPk()) + ->whereExp($this->morphKey, '=' . $this->parent->getTable(true) . '.' . $this->parent->getPk()) ->where($this->morphType, '=', $this->type) ->fetchSql() ->$aggregate($field); diff --git a/src/model/relation/MorphToMany.php b/src/model/relation/MorphToMany.php index 30bee0d8..616c264f 100644 --- a/src/model/relation/MorphToMany.php +++ b/src/model/relation/MorphToMany.php @@ -198,7 +198,7 @@ public function getRelationCountQuery(?Closure $closure = null, string $aggregat } return $this->belongsToManyQuery($this->foreignKey, $this->localKey, [ - ['pivot.' . $this->localKey, 'exp', new Raw('=' . $this->parent->db(false)->getTable() . '.' . $this->parent->getPk())], + ['pivot.' . $this->localKey, 'exp', new Raw('=' . $this->parent->db(false)->getTable(true) . '.' . $this->parent->getPk())], ['pivot.' . $this->morphType, '=', $this->morphClass], ])->fetchSql()->$aggregate($field); }