0

Final Class Yiisoft\Db\Migration\Service\Generate\CreateService

InheritanceYiisoft\Db\Migration\Service\Generate\CreateService

Constants

Hide inherited constants

Constant Value Description Defined By
DEFAULT_USE_TABLE_PREFIX true Default value of whether the generated table names should consider the tablePrefix setting. Yiisoft\Db\Migration\Service\Generate\CreateService

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Yiisoft\Db\Connection\ConnectionInterface $db, boolean $useTablePrefix = self::DEFAULT_USE_TABLE_PREFIX, boolean|null $transactional = null )
$db \Yiisoft\Db\Connection\ConnectionInterface
$useTablePrefix boolean

Indicates whether the table names generated should consider the tablePrefix setting of the DB connection. For example, if the table name is post, the generator will return {{%post}}.

$transactional boolean|null

Whether generated migrations use transactions. null keeps template defaults.

                public function __construct(
    private readonly ConnectionInterface $db,
    private readonly bool $useTablePrefix = self::DEFAULT_USE_TABLE_PREFIX,
    private readonly ?bool $transactional = null,
) {
    $this->phpRenderer = new PhpRenderer();
}

            
getTemplate() public method

public string getTemplate ( ?string $key )
$key ?string

                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];
}

            
run() public method

public string run ( string $command, string $table, string $className, ?string $namespace = null, ?string $fields = null, ?string $and = null, ?string $tableComment = null, boolean|null $transactional = null )
$command string
$table string
$className string
$namespace ?string
$fields ?string
$and ?string
$tableComment ?string
$transactional boolean|null

Overrides the configured transaction mode for this migration. null uses the configured mode or the template default.

                public function run(
    string $command,
    string $table,
    string $className,
    ?string $namespace = null,
    ?string $fields = null,
    ?string $and = null,
    ?string $tableComment = null,
    ?bool $transactional = 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,
            'transactional' => $transactional ?? $this->transactional,
        ],
    );
}

            
setIo() public method

public void setIo ( ?\Symfony\Component\Console\Style\SymfonyStyle $io )
$io ?\Symfony\Component\Console\Style\SymfonyStyle

                public function setIo(?SymfonyStyle $io): void
{
    $this->io = $io;
}

            
setTemplate() public method

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;
}

            
setTemplates() public method

public void setTemplates ( array $value = [] )
$value array

                public function setTemplates(array $value = []): void
{
    $this->templates = $value;
}