Final Class Yiisoft\Db\Migration\Service\Generate\CreateService
| Inheritance | Yiisoft\Db\Migration\Service\Generate\CreateService |
|---|
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Db\Migration\Service\Generate\CreateService | |
| getTemplate() | Yiisoft\Db\Migration\Service\Generate\CreateService | |
| run() | Yiisoft\Db\Migration\Service\Generate\CreateService | |
| setIo() | Yiisoft\Db\Migration\Service\Generate\CreateService | |
| setTemplate() | Set of template paths for generating migration code automatically. | Yiisoft\Db\Migration\Service\Generate\CreateService |
| setTemplates() | Yiisoft\Db\Migration\Service\Generate\CreateService |
Method Details
| public mixed __construct ( \Yiisoft\Db\Connection\ConnectionInterface $db, boolean $useTablePrefix = true ) | ||
| $db | \Yiisoft\Db\Connection\ConnectionInterface | |
| $useTablePrefix | boolean |
Indicates whether the table names generated should consider the |
public function __construct(
private readonly ConnectionInterface $db,
private readonly bool $useTablePrefix = true,
) {
$this->phpRenderer = new PhpRenderer();
}
| public string getTemplate ( string|null $key ) | ||
| $key | string|null | |
public function getTemplate(?string $key): string
{
$key = (string) $key;
if ($this->templates === null) {
$this->setDefaultTemplates();
}
if (!isset($this->templates[$key])) {
throw new InvalidConfigException('You must define a template to generate the migration.');
}
return $this->templates[$key];
}
| public string run ( string $command, string $table, string $className, string|null $namespace = null, string|null $fields = null, string|null $and = null, string|null $tableComment = null ) | ||
| $command | string | |
| $table | string | |
| $className | string | |
| $namespace | string|null | |
| $fields | string|null | |
| $and | string|null | |
| $tableComment | string|null | |
public function run(
string $command,
string $table,
string $className,
?string $namespace = null,
?string $fields = null,
?string $and = null,
?string $tableComment = null,
): string {
$templateFile = $this->getTemplate($command);
$foreignKeyFactory = new ForeignKeyFactory(
$this->db,
$this->io,
$this->useTablePrefix,
);
[$columns, $foreignKeys] = (new FieldsParser($foreignKeyFactory))->parse(
$table,
$fields,
in_array($command, ['table', 'dropTable'], true),
);
if ($command === 'junction') {
$and = (string) $and;
$columns = array_merge(
[
new Column($table . '_id', ['integer()']),
new Column($and . '_id', ['integer()']),
],
$columns,
[
new Column('PRIMARY KEY(' . $table . '_id, ' . $and . '_id)'),
],
);
$foreignKeys[] = $foreignKeyFactory->create($table . '_' . $and, $table . '_id', $table, null);
$foreignKeys[] = $foreignKeyFactory->create($table . '_' . $and, $and . '_id', $and, null);
$table .= '_' . $and;
}
return $this->phpRenderer->render(
$templateFile,
[
'table' => $table,
'className' => $className,
'namespace' => $namespace,
'columns' => $columns,
'foreignKeys' => $foreignKeys,
'tableComment' => $tableComment,
],
);
}
| public void setIo ( \Symfony\Component\Console\Style\SymfonyStyle|null $io ) | ||
| $io | \Symfony\Component\Console\Style\SymfonyStyle|null | |
public function setIo(?SymfonyStyle $io): void
{
$this->io = $io;
}
Set of template paths for generating migration code automatically.
The key is the template type, the value is a path.
Supported types are:
'create' => 'vendor/yiisoft/db-migration/resources/views/migration.php',
'table' => vendor/yiisoft/db-migration/resources/views/createTableMigration.php',
'dropTable' => 'vendor/yiisoft/db-migration/resources/views/dropTableMigration.php',
'addColumn' => 'vendor/yiisoft/db-migration/resources/views/addColumnMigration.php',
'dropColumn' => 'vendor/yiisoft/db-migration/resources/views/dropColumnMigration.php',
'junction' => 'vendor/yiisoft/db-migration/resources/views/createTableMigration.php'
| public void setTemplate ( string $key, string $value ) | ||
| $key | string | |
| $value | string | |
public function setTemplate(string $key, string $value): void
{
$this->templates[$key] = $value;
}
| public void setTemplates ( array $value = [] ) | ||
| $value | array | |
public function setTemplates(array $value = []): void
{
$this->templates = $value;
}
Signup or Login in order to comment.