Skip to content

Commit

Permalink
getTable方法支持返回数据表别名
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Oct 29, 2024
1 parent 2255649 commit 88509f0
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 25 deletions.
17 changes: 9 additions & 8 deletions src/db/BaseQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
9 changes: 3 additions & 6 deletions src/db/concern/JoinAndViewQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace think\db\concern;

use think\db\Raw;
use think\helper\Str;

/**
* JOIN和VIEW查询.
Expand Down Expand Up @@ -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, ' ')) {
// 使用别名
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/model/relation/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/model/relation/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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()];
}
Expand Down
4 changes: 2 additions & 2 deletions src/model/relation/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/model/relation/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/model/relation/MorphMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/model/relation/MorphToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 88509f0

Please sign in to comment.