Abstract Class yii\console\controllers\BaseMigrateController

继承yii\console\controllers\BaseMigrateController » yii\console\Controller » yii\base\Controller » yii\base\Component » yii\base\BaseObject
实现yii\base\Configurable, yii\base\ViewContextInterface
子类yii\console\controllers\MigrateController
可用自版本2.0
源代码 https://github.com/yiisoft/yii2/blob/master/framework/console/controllers/BaseMigrateController.php

BaseMigrateController is the base class for migrate controllers.

公共属性

隐藏继承的属性

属性类型描述定义在
$action yii\base\Action|null 当前正在执行的动作. yii\base\Controller
$behaviors yii\base\Behavior[] 附加到此组件的行为列表. 该属性是只读的. yii\base\Component
$color boolean|null 是否在输出中启用 ANSI 颜色. yii\console\Controller
$compact boolean Indicates whether the console output should be compacted. yii\console\controllers\BaseMigrateController
$defaultAction string The default command action. yii\console\controllers\BaseMigrateController
$help boolean 是否显示有关当前命令的帮助信息. yii\console\Controller
$helpSummary string 该属性是只读的. yii\console\Controller
$id string 该控制器的ID. yii\base\Controller
$interactive boolean 是否以交互方式运行命令. yii\console\Controller
$layout null|string|false 要应用于此控制器的视图的布局的名称. yii\base\Controller
$migrationNamespaces array List of namespaces containing the migration classes. yii\console\controllers\BaseMigrateController
$migrationPath string|array The directory containing the migration classes. yii\console\controllers\BaseMigrateController
$module yii\base\Module 该控制器所属的模块. yii\base\Controller
$modules yii\base\Module[] 该控制器所在的所有祖先模块. 该属性是只读的. yii\base\Controller
$passedOptionValues array 与传递的选项相对应的属性. 该属性是只读的. yii\console\Controller
$passedOptions array 执行期间传递的选项名称. 该属性是只读的. yii\console\Controller
$request yii\console\Request yii\console\Controller
$response yii\console\Response yii\console\Controller
$route string 当前请求的路由(模块ID, 控制器ID和动作ID). 该属性是只读的. yii\base\Controller
$silentExitOnException boolean|null 如果为true, 脚本在出现异常的情况下以ExitCode::OK结尾; 如果为false, ExitCode::UNSPECIFIED_ERROR. yii\console\Controller
$templateFile string The template file for generating new migrations. yii\console\controllers\BaseMigrateController
$uniqueId string 以模块ID(如果有)为前缀的控制器ID. 该属性是只读的. yii\base\Controller
$view yii\base\View|yii\web\View 可用于渲染视图或视图文件的视图对象. yii\base\Controller
$viewPath string 包含此控制器的视图文件的目录. yii\base\Controller

公共方法

隐藏继承的方法

方法描述定义在
__call() 调用不是类方法的指定的方法. yii\base\Component
__clone() 在通过克隆现有对象创建对象之后调用此方法. yii\base\Component
__construct() 构造函数. yii\base\Controller
__get() 返回组件属性的值. yii\base\Component
__isset() 检查属性是否已设置, 即已定义且不为null. yii\base\Component
__set() 设置组件属性的值. yii\base\Component
__unset() 将组件属性设置为null. yii\base\Component
actionCreate() Creates a new migration. yii\console\controllers\BaseMigrateController
actionDown() Downgrades the application by reverting old migrations. yii\console\controllers\BaseMigrateController
actionFresh() Drops all tables and related constraints. Starts the migration from the beginning. yii\console\controllers\BaseMigrateController
actionHistory() Displays the migration history. yii\console\controllers\BaseMigrateController
actionMark() Modifies the migration history to the specified version. yii\console\controllers\BaseMigrateController
actionNew() Displays the un-applied new migrations. yii\console\controllers\BaseMigrateController
actionRedo() Redoes the last few migrations. yii\console\controllers\BaseMigrateController
actionTo() Upgrades or downgrades till the specified version. yii\console\controllers\BaseMigrateController
actionUp() Upgrades the application by applying new migrations. yii\console\controllers\BaseMigrateController
actions() 为控制器声明外部动作. yii\base\Controller
afterAction() 执行动作后立即调用该方法. yii\base\Controller
ansiFormat() 使用 ANSI 代码格式化字符串. yii\console\Controller
attachBehavior() 将行为附加到此组件. yii\base\Component
attachBehaviors() 将行为列表附加到组件. 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. yii\console\controllers\BaseMigrateController
behaviors() 返回此组件应该具有的行为列表. yii\base\Component
bindActionParams() 将参数绑定到动作. yii\console\Controller
canGetProperty() 返回一个值, 指示属性是否可读取. yii\base\Component
canSetProperty() 返回一个值, 指示属性是否可设置. yii\base\Component
className() 返回此类的完全限定名称. yii\base\BaseObject
confirm() 要求用户输入yn进行确认. yii\console\Controller
createAction() 根据给定的动作ID创建一个动作. yii\base\Controller
detachBehavior() 从组件中分离一个行为. yii\base\Component
detachBehaviors() 分离组件中的所有行为. yii\base\Component
ensureBehaviors() 确保在 behaviors() 中声明的行为已附加到此组件. yii\base\Component
findLayoutFile() 查找适用的布局文件. yii\base\Controller
getActionArgsHelp() 返回动作的匿名参数的帮助信息. yii\console\Controller
getActionHelp() 返回指定动作的详细帮助信息. yii\console\Controller
getActionHelpSummary() 返回描述指定动作的单行简单摘要. yii\console\Controller
getActionOptionsHelp() 返回动作选项的帮助信息. yii\console\Controller
getBehavior() 返回指定的行为对象. yii\base\Component
getBehaviors() 返回附加到此组件的所有行为. yii\base\Component
getHelp() 返回此控制器的帮助信息. yii\console\Controller
getHelpSummary() 返回描述此控制器的单行简单摘要. yii\console\Controller
getModules() 返回此控制器的所有祖先模块. yii\base\Controller
getOptionValues() 返回与动作id的选项相对应的属性. yii\console\Controller
getPassedOptionValues() 返回与传递的选项相对应的属性. yii\console\Controller
getPassedOptions() 返回执行期间传递的选项的名称. yii\console\Controller
getRoute() 返回当前请求的路由. yii\base\Controller
getUniqueId() 返回控制器的唯一ID. yii\base\Controller
getView() 返回可用于渲染视图或视图文件的视图对象. yii\base\Controller
getViewPath() yii\base\ViewContextInterface
hasEventHandlers() 返回一个值, 指示是否有任何处理程序附加到指定的事件. yii\base\Component
hasMethod() 返回一个值, 指示方法是否已定义. yii\base\Component
hasProperty() 返回一个值, 指示属性是否已定义. yii\base\Component
init() 初始化对象. yii\base\Controller
isColorEnabled() 返回一个值, 该值指示是否启用 ANSI 颜色. yii\console\Controller
off() 从该组件分离现有的事件处理程序. yii\base\Component
on() 将事件处理程序附加到事件. yii\base\Component
optionAliases() 返回选项别名. yii\console\Controller
options() 返回动作的有效操作选项. yii\console\controllers\BaseMigrateController
prompt() 提示用户输入并验证输入. yii\console\Controller
render() 渲染视图并应用布局(如果可用). yii\base\Controller
renderContent() 通过应用布局渲染静态字符串. yii\base\Controller
renderFile() 渲染视图文件. yii\base\Controller
renderPartial() 渲染视图而不使用布局. yii\base\Controller
run() 运行根据路由指定的请求. yii\base\Controller
runAction() 使用指定的动作id和参数运行一个动作. yii\console\Controller
select() 给用户一个选择的选项. yii\console\Controller
setView() 设置此控制器使用的视图对象. yii\base\Controller
setViewPath() 设置包含视图文件的目录. yii\base\Controller
stderr() 将字符串打印到 STDERR. yii\console\Controller
stdout() 将字符串打印到 STDOUT. yii\console\Controller
trigger() 触发事件. yii\base\Component

受保护的方法

隐藏继承的方法

方法描述定义在
addMigrationHistory() Adds new migration entry to the history. yii\console\controllers\BaseMigrateController
bindInjectedParams() 根据操作方法签名中的类型和名称填充参数. yii\base\Controller
createMigration() Creates a new migration instance. yii\console\controllers\BaseMigrateController
generateMigrationSourceCode() Generates new migration source PHP code. yii\console\controllers\BaseMigrateController
getActionMethodReflection() yii\console\Controller
getMigrationHistory() Returns the migration history. yii\console\controllers\BaseMigrateController
getMigrationNameLimit() Return the maximum name length for a migration. yii\console\controllers\BaseMigrateController
getNewMigrations() Returns the migrations that are not applied. yii\console\controllers\BaseMigrateController
includeMigrationFile() Includes the migration file for a given migration class name. yii\console\controllers\BaseMigrateController
migrateDown() Downgrades with the specified migration class. yii\console\controllers\BaseMigrateController
migrateToTime() Migrates to the specified apply time in the past. yii\console\controllers\BaseMigrateController
migrateToVersion() Migrates to the certain version. yii\console\controllers\BaseMigrateController
migrateUp() Upgrades with the specified migration class. yii\console\controllers\BaseMigrateController
parseDocCommentDetail() 从文档块返回完整描述. yii\console\Controller
parseDocCommentSummary() 返回文档块的第一行. yii\console\Controller
parseDocCommentTags() 将注释块解析为标签. yii\console\Controller
removeMigrationHistory() Removes existing migration from the history. yii\console\controllers\BaseMigrateController
truncateDatabase() Truncates the database. yii\console\controllers\BaseMigrateController

事件

隐藏继承的事件

事件类型描述定义在
EVENT_AFTER_ACTION yii\base\ActionEvent 执行控制器动作后立即触发的事件. yii\base\Controller
EVENT_BEFORE_ACTION yii\base\ActionEvent 在执行控制器动作之前触发的事件. yii\base\Controller

常量

隐藏继承的常量

常量描述定义在
BASE_MIGRATION 'm000000_000000_base' The name of the dummy migration that marks the beginning of the whole migration history. yii\console\controllers\BaseMigrateController
EXIT_CODE_ERROR 1 Deprecated since 2.0.13. 改用 yii\console\ExitCode::UNSPECIFIED_ERROR. yii\console\Controller
EXIT_CODE_NORMAL 0 Deprecated since 2.0.13. 改用 yii\console\ExitCode::OK. yii\console\Controller

属性详情

$compact public 属性 (可用自版本: 2.0.13)

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.

public boolean $compact false
$defaultAction public 属性

The default command action.

public string $defaultAction 'up'
$migrationNamespaces public 属性 (可用自版本: 2.0.10)

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',
]

参见 $migrationPath.

$migrationPath public 属性

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.

参见 $migrationNamespaces.

public string|array $migrationPath = ['@app/migrations']
$templateFile public 属性

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 string $templateFile null

方法详情

actionCreate() public 方法

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.

public void actionCreate ( $name )
$name string

The 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.

throws yii\console\Exception

if the name argument is invalid.

actionDown() public 方法

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
public integer actionDown ( $limit 1 )
$limit integer|string

The 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.

return integer

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

throws yii\console\Exception

if the number of the steps specified is less than 1.

actionFresh() public 方法 (可用自版本: 2.0.13)

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

yii migrate/fresh
public void actionFresh ( )
actionHistory() public 方法

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
public void actionHistory ( $limit 10 )
$limit integer|string

The maximum number of migrations to be displayed. If it is "all", the whole migration history will be displayed.

throws yii\console\Exception

if invalid limit value passed

actionMark() public 方法

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
public integer actionMark ( $version )
$version string

The 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.

return integer

CLI exit code

throws yii\console\Exception

if the version argument is invalid or the version cannot be found.

actionNew() public 方法

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
public void actionNew ( $limit 10 )
$limit integer|string

The maximum number of new migrations to be displayed. If it is all, all available new migrations will be displayed.

throws yii\console\Exception

if invalid limit value passed

actionRedo() public 方法

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
public integer actionRedo ( $limit 1 )
$limit integer|string

The 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.

return integer

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

throws yii\console\Exception

if the number of the steps specified is less than 1.

actionTo() public 方法

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
public void actionTo ( $version )
$version string

Either 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.

throws yii\console\Exception

if the version argument is invalid.

actionUp() public 方法

Upgrades the application by applying new migrations.

For example,

yii migrate     # apply all new migrations
yii migrate 3   # apply the first 3 new migrations
public integer actionUp ( $limit 0 )
$limit integer

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

return integer

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

addMigrationHistory() protected abstract 方法

Adds new migration entry to the history.

protected abstract void addMigrationHistory ( $version )
$version string

Migration version name.

beforeAction() public 方法

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

public boolean beforeAction ( $action )
$action yii\base\Action

The action to be executed.

return boolean

Whether the action should continue to be executed.

throws yii\base\InvalidConfigException

if directory specified in migrationPath doesn't exist and action isn't "create".

createMigration() protected 方法

Creates a new migration instance.

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

The migration class name

return yii\db\MigrationInterface

The migration instance

generateMigrationSourceCode() protected 方法 (可用自版本: 2.0.8)

Generates new migration source PHP code.

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

protected string generateMigrationSourceCode ( $params )
$params array

Generation parameters, usually following parameters are present:

  • name: string migration base name
  • className: string migration class name
return string

Generated PHP code.

getMigrationHistory() protected abstract 方法

Returns the migration history.

protected abstract array getMigrationHistory ( $limit )
$limit integer

The maximum number of records in the history to be returned. null for "no limit".

return array

The migration history

getMigrationNameLimit() protected 方法 (可用自版本: 2.0.13)

Return the maximum name length for a migration.

Subclasses may override this method to define a limit.

protected integer|null getMigrationNameLimit ( )
return integer|null

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

getNewMigrations() protected 方法

Returns the migrations that are not applied.

protected array getNewMigrations ( )
return array

List of new migrations

includeMigrationFile() protected 方法 (可用自版本: 2.0.12)

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.

protected void includeMigrationFile ( $class )
$class string

The migration class name.

migrateDown() protected 方法

Downgrades with the specified migration class.

protected boolean migrateDown ( $class )
$class string

The migration class name

return boolean

Whether the migration is successful

migrateToTime() protected 方法

Migrates to the specified apply time in the past.

protected void migrateToTime ( $time )
$time integer

UNIX timestamp value.

migrateToVersion() protected 方法

Migrates to the certain version.

protected integer migrateToVersion ( $version )
$version string

Name in the full format.

return integer

CLI exit code

throws yii\console\Exception

if the provided version cannot be found.

migrateUp() protected 方法

Upgrades with the specified migration class.

protected boolean migrateUp ( $class )
$class string

The migration class name

return boolean

Whether the migration is successful

options() public 方法

返回动作的有效操作选项.

一个选项需要存在一个公共成员变量, 其名称为选项名称. 子类可以重写此方法以指定可能的选项.

Note: 只有在调用 beforeAction() 之后, 才能通过选项设置值.

public string[] options ( $actionID )
$actionID string

当前请求的动作id

return string[]

动作的有效操作选项.

removeMigrationHistory() protected abstract 方法

Removes existing migration from the history.

protected abstract void removeMigrationHistory ( $version )
$version string

Migration version name.

truncateDatabase() protected 方法 (可用自版本: 2.0.13)

Truncates the database.

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

protected void truncateDatabase ( )
throws yii\base\NotSupportedException

if not overridden