mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
Optimized the argument table
like database.table
for gen:model
which can be used to generate another database models. (#7044)
Co-authored-by: hexiangyu <hexiangyu@addcn.com>
This commit is contained in:
parent
196386bc7e
commit
0f69aa2320
@ -9,6 +9,7 @@
|
||||
|
||||
- [#7033](https://github.com/hyperf/hyperf/pull/7033) Improved `ConsoleLogger` to support running in watcher.
|
||||
- [#7040](https://github.com/hyperf/hyperf/pull/7040) Improved packaging speed for command `phar:build`.
|
||||
- [#7044](https://github.com/hyperf/hyperf/pull/7044) Optimized the argument `table` like `database.table` for `gen:model` which can be used to generate another database models.
|
||||
|
||||
## Added
|
||||
|
||||
|
@ -216,11 +216,8 @@ class PostgresBuilder extends Builder
|
||||
|
||||
/**
|
||||
* Get the column type listing for a given table.
|
||||
*
|
||||
* @param string $table
|
||||
* @return array
|
||||
*/
|
||||
public function getColumnTypeListing($table)
|
||||
public function getColumnTypeListing(string $table, ?string $database = null): array
|
||||
{
|
||||
[$schema, $table] = $this->parseSchemaAndTable($table);
|
||||
|
||||
@ -228,7 +225,7 @@ class PostgresBuilder extends Builder
|
||||
|
||||
$results = $this->connection->select(
|
||||
$this->grammar->compileColumnListing(),
|
||||
[$this->connection->getDatabaseName(), $schema, $table]
|
||||
[$database ?? $this->connection->getDatabaseName(), $schema, $table]
|
||||
);
|
||||
|
||||
/** @var PostgresProcessor $processor */
|
||||
|
@ -152,11 +152,13 @@ class ModelCommand extends Command
|
||||
return $table === $this->config->get('databases.migrations', 'migrations');
|
||||
}
|
||||
|
||||
protected function createModel(string $table, ModelOption $option)
|
||||
protected function createModel(string $table, ModelOption $option): void
|
||||
{
|
||||
$builder = $this->getSchemaBuilder($option->getPool());
|
||||
$table = Str::replaceFirst($option->getPrefix(), '', $table);
|
||||
$columns = $this->formatColumns($builder->getColumnTypeListing($table));
|
||||
$pureTable = Str::after($table, '.');
|
||||
$databaseName = Str::contains($table, '.') ? Str::before($table, '.') : null;
|
||||
$columns = $this->formatColumns($builder->getColumnTypeListing($pureTable, $databaseName));
|
||||
if (empty($columns)) {
|
||||
$this->output?->error(
|
||||
sprintf('Query columns empty, maybe is table `%s` does not exist.You can check it in database.', $table)
|
||||
@ -164,7 +166,7 @@ class ModelCommand extends Command
|
||||
}
|
||||
|
||||
$project = new Project();
|
||||
$class = $option->getTableMapping()[$table] ?? Str::studly(Str::singular($table));
|
||||
$class = $option->getTableMapping()[$table] ?? Str::studly(Str::singular($pureTable));
|
||||
$class = $project->namespace($option->getPath()) . $class;
|
||||
$path = BASE_PATH . '/' . $project->path($class);
|
||||
|
||||
|
@ -85,17 +85,14 @@ class MySqlBuilder extends Builder
|
||||
|
||||
/**
|
||||
* Get the column type listing for a given table.
|
||||
*
|
||||
* @param string $table
|
||||
* @return array
|
||||
*/
|
||||
public function getColumnTypeListing($table)
|
||||
public function getColumnTypeListing(string $table, ?string $database = null): array
|
||||
{
|
||||
$table = $this->connection->getTablePrefix() . $table;
|
||||
|
||||
$results = $this->connection->select(
|
||||
$this->grammar->compileColumnListing(),
|
||||
[$this->connection->getDatabaseName(), $table]
|
||||
[$database ?? $this->connection->getDatabaseName(), $table]
|
||||
);
|
||||
|
||||
/** @var MySqlProcessor $processor */
|
||||
|
Loading…
Reference in New Issue
Block a user