Skip to content
PHP Ecosystem
Explore

BaseMigrateController

API reference › yii › console › controllers › BaseMigrateController

1 subclass

BaseMigrateController is the base class for migrate controllers.

PropertyTypeDescriptionDefined by
$actionyii\base\Action<static>|nullThe action that is currently being executed. Default: null.yii\base\Controller
$behaviors read-onlyarray<yii\base\Behavior<static>>List of behaviors attached to this component.yii\base\Component
$colorbool|nullWhether to enable ANSI color in the output. Default: null.yii\console\Controller
$compactboolIndicates whether the console output should be compacted. Default: false. Available since 2.0.13.BaseMigrateController
$defaultActionstringThe default command action. Default: 'up'.BaseMigrateController
$helpboolWhether to display help information about current command. Default: false. Available since 2.0.10.yii\console\Controller
$helpSummary read-onlystringThe one-line short summary describing this controller.yii\console\Controller
$idstringThe ID of this controller. Default: null.yii\base\Controller
$interactiveboolWhether to run the command interactively. Default: true.yii\console\Controller
$layoutstring|null|falseThe name of the layout to be applied to this controller’s views. Default: null.yii\base\Controller
$migrationNamespacesarrayList of namespaces containing the migration classes. Default: []. Available since 2.0.10.BaseMigrateController
$migrationPathstring|array|nullThe directory containing the migration classes. Default: ['@app/migrations'].BaseMigrateController
$moduleyii\base\TThe module that this controller belongs to. Default: null.yii\base\Controller
$modules read-onlyyii\base\Module[]All ancestor modules that this controller is located within.yii\base\Controller
$newFileModeint|nullThe permission to be set for newly generated migration files. Default: null. Available since 2.0.43.BaseMigrateController
$newFileOwnershipstring|int|nullThe user and/or group ownership to be set for newly generated migration files. Default: null. Available since 2.0.43.BaseMigrateController
$passedOptions read-onlyarrayThe names of the options passed during execution.yii\console\Controller
$passedOptionValues read-onlyarrayThe properties corresponding to the passed options.yii\console\Controller
$requestyii\console\RequestThe request object.yii\console\Controller
$responseyii\console\ResponseThe response object.yii\console\Controller
$route read-onlystringThe route (module ID, controller ID and action ID) of the current request.yii\base\Controller
$silentExitOnExceptionbool|nullIf true - script finish with ExitCode::OK in case of exception. false - ExitCode::UNSPECIFIED_ERROR. Default: null. Available since 2.0.36.yii\console\Controller
$templateFilestringThe template file for generating new migrations. Default: null.BaseMigrateController
$uniqueId read-onlystringThe controller ID that is prefixed with the module ID (if any).yii\base\Controller
$viewyii\base\View|yii\web\ViewThe view object that can be used to render views or view files.yii\base\Controller
$viewPathstringThe directory containing the view files for this controller.yii\base\Controller
MethodDescriptionDefined by
__construct()yii\base\Controller
__call()Calls the named method which is not a class method.yii\base\Component
__clone()This method is called after the object is created by cloning an existing one.yii\base\Component
__get()Returns the value of a component property.yii\base\Component
__isset()Checks if a property is set, i.e. defined and not null.yii\base\Component
__set()Sets the value of a component property.yii\base\Component
__unset()Sets a component property to be null.yii\base\Component
actionCreate()Creates a new migration.BaseMigrateController
actionDown()Downgrades the application by reverting old migrations.BaseMigrateController
actionFresh()Drops all tables and related constraints. Starts the migration from the beginning.BaseMigrateController
actionHistory()Displays the migration history.BaseMigrateController
actionMark()Modifies the migration history to the specified version.BaseMigrateController
actionNew()Displays the un-applied new migrations.BaseMigrateController
actionRedo()Redoes the last few migrations.BaseMigrateController
actions()Declares external actions for the controller.yii\base\Controller
actionTo()Upgrades or downgrades till the specified version.BaseMigrateController
actionUp()Upgrades the application by applying new migrations.BaseMigrateController
afterAction()This method is invoked right after an action is executed.yii\base\Controller
ansiFormat()Formats a string with ANSI codes.yii\console\Controller
attachBehavior()Attaches a behavior to this component.yii\base\Component
attachBehaviors()Attaches a list of behaviors to the component.yii\base\Component
beforeAction()This method is invoked right before an action is to be executed (after all possible filters.) It checks the existence of the $migrationPath.BaseMigrateController
behaviors()Returns a list of behaviors that this component should behave as.yii\base\Component
bindActionParams()Binds the parameters to the action.yii\console\Controller
canGetProperty()Returns a value indicating whether a property can be read.yii\base\Component
canSetProperty()Returns a value indicating whether a property can be set.yii\base\Component
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
confirm()Asks user to confirm by typing y or n.yii\console\Controller
createAction()Creates an action based on the given action ID.yii\base\Controller
detachBehavior()Detaches a behavior from the component.yii\base\Component
detachBehaviors()Detaches all behaviors from the component.yii\base\Component
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.yii\base\Component
findLayoutFile()Finds the applicable layout file.yii\base\Controller
getActionArgsHelp()Returns the help information for the anonymous arguments for the action.yii\console\Controller
getActionHelp()Returns the detailed help information for the specified action.yii\console\Controller
getActionHelpSummary()Returns a one-line short summary describing the specified action.yii\console\Controller
getActionOptionsHelp()Returns the help information for the options for the action.yii\console\Controller
getBehavior()Returns the named behavior object.yii\base\Component
getBehaviors()Returns all behaviors attached to this component.yii\base\Component
getHelp()Returns help information for this controller.yii\console\Controller
getHelpSummary()Returns one-line short summary describing this controller.yii\console\Controller
getModules()Returns all ancestor modules of this controller.yii\base\Controller
getOptionValues()Returns properties corresponding to the options for the action id Child classes may override this method to specify possible properties.yii\console\Controller
getPassedOptions()Returns the names of valid options passed during execution.yii\console\Controller
getPassedOptionValues()Returns the properties corresponding to the passed options.yii\console\Controller
getRoute()Returns the route of the current request.yii\base\Controller
getUniqueId()Returns the unique ID of the controller.yii\base\Controller
getView()Returns the view object that can be used to render views or view files.yii\base\Controller
getViewPath()Returns the directory containing view files for this controller.yii\base\Controller
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.yii\base\Component
hasMethod()Returns a value indicating whether a method is defined.yii\base\Component
hasProperty()Returns a value indicating whether a property is defined for this component.yii\base\Component
init()Initializes the object.yii\base\Controller
isColorEnabled()Returns a value indicating whether ANSI color is enabled.yii\console\Controller
off()Detaches an existing event handler from this component.yii\base\Component
on()Attaches an event handler to an event.yii\base\Component
optionAliases()Returns option alias names.yii\console\Controller
options()Returns the names of valid options for the action (id) An option requires the existence of a public member variable whose name is the option name.BaseMigrateController
prompt()Prompts the user for input and validates it.yii\console\Controller
render()Renders a view and applies layout if available.yii\base\Controller
renderContent()Renders a static string by applying a layout.yii\base\Controller
renderFile()Renders a view file.yii\base\Controller
renderPartial()Renders a view without applying layout.yii\base\Controller
run()Runs a request specified in terms of a route.yii\base\Controller
runAction()Runs an action with the specified action ID and parameters.yii\console\Controller
select()Gives the user an option to choose from. Giving ’?’ as an input will show a list of options to choose from and their explanations.yii\console\Controller
setView()Sets the view object to be used by this controller.yii\base\Controller
setViewPath()Sets the directory that contains the view files.yii\base\Controller
stderr()Prints a string to STDERR.yii\console\Controller
stdout()Prints a string to STDOUT.yii\console\Controller
trigger()Triggers an event.yii\base\Component
addMigrationHistory() protected abstractAdds new migration entry to the history.BaseMigrateController
bindInjectedParams() protected finalFills parameters based on types and names in action method signature.yii\base\Controller
createMigration() protectedCreates a new migration instance.BaseMigrateController
generateMigrationSourceCode() protectedGenerates new migration source PHP code.BaseMigrateController
getActionMethodReflection() protectedyii\console\Controller
getMigrationHistory() protected abstractReturns the migration history.BaseMigrateController
getMigrationNameLimit() protectedReturn the maximum name length for a migration.BaseMigrateController
getNewMigrations() protectedReturns the migrations that are not applied.BaseMigrateController
includeMigrationFile() protectedIncludes the migration file for a given migration class name.BaseMigrateController
migrateDown() protectedDowngrades with the specified migration class.BaseMigrateController
migrateToTime() protectedMigrates to the specified apply time in the past.BaseMigrateController
migrateToVersion() protectedMigrates to the certain version.BaseMigrateController
migrateUp() protectedUpgrades with the specified migration class.BaseMigrateController
parseDocCommentDetail() protectedReturns full description from the docblock.yii\console\Controller
parseDocCommentSummary() protectedReturns the first line of docblock.yii\console\Controller
parseDocCommentTags() protectedParses the comment block into tags.yii\console\Controller
removeMigrationHistory() protected abstractRemoves existing migration from the history.BaseMigrateController
truncateDatabase() protectedTruncates the database.BaseMigrateController
EventTypeDescriptionDefined by
EVENT_BEFORE_ACTIONyii\base\ActionEventAn event raised right before executing a controller action. Event name: 'beforeAction'.yii\base\Controller
EVENT_AFTER_ACTIONyii\base\ActionEventAn event raised right after executing a controller action. Event name: 'afterAction'.yii\base\Controller
ConstantValueDescriptionDefined by
BASE_MIGRATION'm000000_000000_base'The name of the dummy migration that marks the beginning of the whole migration history.BaseMigrateController
public bool $compact = false

Indicates whether the console output should be compacted.

If this is set to true, the individual commands ran within the migration will not be output to the console. Default is false, in other words the output is fully verbose by default.

Since

2.0.13

public array $migrationNamespaces = []

List of namespaces containing the migration classes.

Migration namespaces should be resolvable as a path alias if prefixed with @, e.g. if you specify the namespace app\migrations, the code Yii::getAlias('@app/migrations') should be able to return the file path to the directory this namespace refers to. This corresponds with the autoloading conventions of Yii.

For example:

[
'app\migrations',
'some\extension\migrations',
]

Since

2.0.10

public string|array|null $migrationPath = ['@app/migrations']

The directory containing the migration classes.

This can be either a path alias or a directory path.

Migration classes located at this path should be declared without a namespace. Use $migrationNamespaces property in case you are using namespaced migrations.

If you have set up $migrationNamespaces, you may set this field to null in order to disable usage of migrations that are not namespaced.

Since version 2.0.12 you may also specify an array of migration paths that should be searched for migrations to load. This is mainly useful to support old extensions that provide migrations without namespace and to adopt the new feature of namespaced migrations while keeping existing migrations.

In general, to load migrations from different locations, $migrationNamespaces is the preferable solution as the migration name contains the origin of the migration in the history, which is not the case when using multiple migration paths.

public int|null $newFileMode = null

The permission to be set for newly generated migration files.

This value will be used by PHP chmod() function. No umask will be applied. If not set, the permission will be determined by the current environment.

Since

2.0.43

public string|int|null $newFileOwnership = null

The user and/or group ownership to be set for newly generated migration files.

If not set, the ownership will be determined by the current environment.

Since

2.0.43

public string $templateFile = null

The template file for generating new migrations.

This can be either a path alias (e.g. “@app/migrations/template.php”) or a file path.

public function actionCreate(string $name): mixed

Creates a new migration.

This command creates a new migration using the available migration template. After using this command, developers should modify the created migration skeleton by filling up the actual migration logic.

yii migrate/create create_user_table

In order to generate a namespaced migration, you should specify a namespace before the migration’s name. Note that backslash (\) is usually considered a special character in the shell, so you need to escape it properly to avoid shell errors or incorrect behavior. For example:

yii migrate/create app\\migrations\\createUserTable

In case $migrationPath is not set and no namespace is provided, the first entry of $migrationNamespaces will be used.

Parameters

ParameterTypeDescription
$namestringThe name of the new migration. This should only contain letters, digits, underscores and/or backslashes. Note: If the migration name is of a special form, for example create_xxx or drop_xxx, then the generated migration file will contain extra code, in this case for creating/dropping tables.

Returns

mixed

Throws

public function actionDown(int|string $limit = 1): int

Downgrades the application by reverting old migrations.

For example,

yii migrate/down # revert the last migration
yii migrate/down 3 # revert the last 3 migrations
yii migrate/down all # revert all migrations

Parameters

ParameterTypeDescription
$limitint|stringThe number of migrations to be reverted. Defaults to 1, meaning the last applied migration will be reverted. When value is “all”, all migrations will be reverted.

Returns

int: The status of the action execution. 0 means normal, other values mean abnormal.

Throws

public function actionFresh(): mixed

Drops all tables and related constraints. Starts the migration from the beginning.

yii migrate/fresh

Returns

mixed

Since

2.0.13

public function actionHistory(int|string $limit = 10): mixed

Displays the migration history.

This command will show the list of migrations that have been applied so far. For example,

yii migrate/history # showing the last 10 migrations
yii migrate/history 5 # showing the last 5 migrations
yii migrate/history all # showing the whole history

Parameters

ParameterTypeDescription
$limitint|stringThe maximum number of migrations to be displayed. If it is “all”, the whole migration history will be displayed.

Returns

mixed

Throws

public function actionMark(string $version): int

Modifies the migration history to the specified version.

No actual migration will be performed.

yii migrate/mark 101129_185401 # using timestamp
yii migrate/mark m101129_185401_create_user_table # using full name
yii migrate/mark app\migrations\M101129185401CreateUser # using full namespace name
yii migrate/mark m000000_000000_base # reset the complete migration history

Parameters

ParameterTypeDescription
$versionstringThe version at which the migration history should be marked. This can be either the timestamp or the full name of the migration. You may specify the name m000000_000000_base to set the migration history to a state where no migration has been applied.

Returns

int: CLI exit code

Throws

public function actionNew(int|string $limit = 10): mixed

Displays the un-applied new migrations.

This command will show the new migrations that have not been applied. For example,

yii migrate/new # showing the first 10 new migrations
yii migrate/new 5 # showing the first 5 new migrations
yii migrate/new all # showing all new migrations

Parameters

ParameterTypeDescription
$limitint|stringThe maximum number of new migrations to be displayed. If it is all, all available new migrations will be displayed.

Returns

mixed

Throws

public function actionRedo(int|string $limit = 1): int

Redoes the last few migrations.

This command will first revert the specified migrations, and then apply them again. For example,

yii migrate/redo # redo the last applied migration
yii migrate/redo 3 # redo the last 3 applied migrations
yii migrate/redo all # redo all migrations

Parameters

ParameterTypeDescription
$limitint|stringThe number of migrations to be redone. Defaults to 1, meaning the last applied migration will be redone. When equals “all”, all migrations will be redone.

Returns

int: The status of the action execution. 0 means normal, other values mean abnormal.

Throws

public function actionTo(string $version): mixed

Upgrades or downgrades till the specified version.

Can also downgrade versions to the certain apply time in the past by providing a UNIX timestamp or a string parseable by the strtotime() function. This means that all the versions applied after the specified certain time would be reverted.

This command will first revert the specified migrations, and then apply them again. For example,

yii migrate/to 101129_185401 # using timestamp
yii migrate/to m101129_185401_create_user_table # using full name
yii migrate/to 1392853618 # using UNIX timestamp
yii migrate/to "2014-02-15 13:00:50" # using strtotime() parseable string
yii migrate/to app\migrations\M101129185401CreateUser # using full namespace name

Parameters

ParameterTypeDescription
$versionstringEither the version name or the certain time value in the past that the application should be migrated to. This can be either the timestamp, the full name of the migration, the UNIX timestamp, or the parseable datetime string.

Returns

mixed

Throws

public function actionUp(int $limit = 0): int

Upgrades the application by applying new migrations.

For example,

yii migrate # apply all new migrations
yii migrate 3 # apply the first 3 new migrations

Parameters

ParameterTypeDescription
$limitintThe number of new migrations to be applied. If 0, it means applying all available new migrations.

Returns

int: The status of the action execution. 0 means normal, other values mean abnormal.

public function beforeAction(yii\base\Action<static> $action): bool

This method is invoked right before an action is to be executed (after all possible filters.) It checks the existence of the $migrationPath.

Parameters

ParameterTypeDescription
$actionyii\base\Action<static>The action to be executed.

Returns

bool: Whether the action should continue to be executed.

Throws

public function options(mixed $actionID): string[]

Returns the names of valid options for the action (id) An option requires the existence of a public member variable whose name is the option name.

Child classes may override this method to specify possible options.

Note that the values setting via options are not available until beforeAction() is being called.

Parameters

ParameterTypeDescription
$actionIDmixedThe action id of the current request

Returns

string[]: The names of the options valid for the action

abstract protected function addMigrationHistory(string $version): mixed

Adds new migration entry to the history.

Parameters

ParameterTypeDescription
$versionstringMigration version name.

Returns

mixed

protected function createMigration(string $class): yii\db\MigrationInterface

Creates a new migration instance.

Parameters

ParameterTypeDescription
$classstringThe migration class name

Returns

yii\db\MigrationInterface: The migration instance

protected function generateMigrationSourceCode(array $params): string

Generates new migration source PHP code.

Child class may override this method, adding extra logic or variation to the process.

Parameters

ParameterTypeDescription
$paramsarrayGeneration parameters, usually following parameters are present: - name: string migration base name - className: string migration class name

Returns

string: Generated PHP code.

Since

2.0.8

abstract protected function getMigrationHistory(int|null $limit): array

Returns the migration history.

Parameters

ParameterTypeDescription
$limitint|nullThe maximum number of records in the history to be returned. null for “no limit”.

Returns

array: The migration history

protected function getMigrationNameLimit(): int|null

Return the maximum name length for a migration.

Subclasses may override this method to define a limit.

Returns

int|null: The maximum name length for a migration or null if no limit applies.

Since

2.0.13

protected function getNewMigrations(): array

Returns the migrations that are not applied.

Returns

array: List of new migrations

protected function includeMigrationFile(string $class): mixed

Includes the migration file for a given migration class name.

This function will do nothing on namespaced migrations, which are loaded by autoloading automatically. It will include the migration file, by searching $migrationPath for classes without namespace.

Parameters

ParameterTypeDescription
$classstringThe migration class name.

Returns

mixed

Since

2.0.12

protected function migrateDown(string $class): bool

Downgrades with the specified migration class.

Parameters

ParameterTypeDescription
$classstringThe migration class name

Returns

bool: Whether the migration is successful

protected function migrateToTime(int $time): mixed

Migrates to the specified apply time in the past.

Parameters

ParameterTypeDescription
$timeintUNIX timestamp value.

Returns

mixed

protected function migrateToVersion(string $version): int

Migrates to the certain version.

Parameters

ParameterTypeDescription
$versionstringName in the full format.

Returns

int: CLI exit code

Throws

protected function migrateUp(string $class): bool

Upgrades with the specified migration class.

Parameters

ParameterTypeDescription
$classstringThe migration class name

Returns

bool: Whether the migration is successful

abstract protected function removeMigrationHistory(string $version): mixed

Removes existing migration from the history.

Parameters

ParameterTypeDescription
$versionstringMigration version name.

Returns

mixed

protected function truncateDatabase(): mixed

Truncates the database.

This method should be overwritten in subclasses to implement the task of clearing the database.

Returns

mixed

Throws

Since

2.0.13