Skip to content

Commit

Permalink
Optimized the argument table like database.table for gen:model
Browse files Browse the repository at this point in the history
…which can be used to generate another database models. (#7044)

Co-authored-by: hexiangyu <[email protected]>
  • Loading branch information
devin515 and hexiangyu authored Sep 5, 2024
1 parent 1c2d1e0 commit 161755c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
8 changes: 5 additions & 3 deletions src/Commands/ModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,21 @@ protected function isIgnoreTable(string $table, ModelOption $option): bool
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)
);
}

$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);

Expand Down
7 changes: 2 additions & 5 deletions src/Schema/MySqlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,14 @@ public function getColumns(): array

/**
* 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 */
Expand Down

0 comments on commit 161755c

Please sign in to comment.