Skip to content

Commit 161755c

Browse files
devin515hexiangyu
andauthored
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>
1 parent 1c2d1e0 commit 161755c

2 files changed

Lines changed: 7 additions & 8 deletions

File tree

src/Commands/ModelCommand.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,19 +152,21 @@ protected function isIgnoreTable(string $table, ModelOption $option): bool
152152
return $table === $this->config->get('databases.migrations', 'migrations');
153153
}
154154

155-
protected function createModel(string $table, ModelOption $option)
155+
protected function createModel(string $table, ModelOption $option): void
156156
{
157157
$builder = $this->getSchemaBuilder($option->getPool());
158158
$table = Str::replaceFirst($option->getPrefix(), '', $table);
159-
$columns = $this->formatColumns($builder->getColumnTypeListing($table));
159+
$pureTable = Str::after($table, '.');
160+
$databaseName = Str::contains($table, '.') ? Str::before($table, '.') : null;
161+
$columns = $this->formatColumns($builder->getColumnTypeListing($pureTable, $databaseName));
160162
if (empty($columns)) {
161163
$this->output?->error(
162164
sprintf('Query columns empty, maybe is table `%s` does not exist.You can check it in database.', $table)
163165
);
164166
}
165167

166168
$project = new Project();
167-
$class = $option->getTableMapping()[$table] ?? Str::studly(Str::singular($table));
169+
$class = $option->getTableMapping()[$table] ?? Str::studly(Str::singular($pureTable));
168170
$class = $project->namespace($option->getPath()) . $class;
169171
$path = BASE_PATH . '/' . $project->path($class);
170172

src/Schema/MySqlBuilder.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,14 @@ public function getColumns(): array
8585

8686
/**
8787
* Get the column type listing for a given table.
88-
*
89-
* @param string $table
90-
* @return array
9188
*/
92-
public function getColumnTypeListing($table)
89+
public function getColumnTypeListing(string $table, ?string $database = null): array
9390
{
9491
$table = $this->connection->getTablePrefix() . $table;
9592

9693
$results = $this->connection->select(
9794
$this->grammar->compileColumnListing(),
98-
[$this->connection->getDatabaseName(), $table]
95+
[$database ?? $this->connection->getDatabaseName(), $table]
9996
);
10097

10198
/** @var MySqlProcessor $processor */

0 commit comments

Comments
 (0)