0 follower

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

InheritanceYiisoft\Db\Migration\Service\Generate\CreateService

Method Details

Hide inherited methods

__construct() public method

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 tablePrefix setting of the DB connection. For example, if the table name is post, the generator will return {{%post}}.

                public function __construct(
    private readonly ConnectionInterface $db,
    private readonly bool $useTablePrefix = true,
) {
    $this->phpRenderer = new PhpRenderer();
}

            
getTemplate() public method

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

            
run() public method

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

            
setIo() public method

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

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