Migration
extends Component
in package
implements
MigrationInterface
uses
SchemaBuilderTrait
Migration is the base class for representing a database migration.
Migration is designed to be used together with the "yii migrate" command.
Each child class of Migration represents an individual database migration which is identified by the child class name.
Within each migration, the [[up()]] method should be overridden to contain the logic for "upgrading" the database; while the [[down()]] method for the "downgrading" logic. The "yii migrate" command manages all available migrations in an application.
If the database supports transactions, you may also override [[safeUp()]] and [[safeDown()]] so that if anything wrong happens during the upgrading or downgrading, the whole migration can be reverted in a whole.
Note that some DB queries in some DBMS cannot be put into a transaction. For some examples,
please refer to implicit commit. If this is the case,
you should still implement up()
and down()
, instead.
Migration provides a set of convenient methods for manipulating database data and schema. For example, the [[insert()]] method can be used to easily insert a row of data into a database table; the [[createTable()]] method can be used to create a database table. Compared with the same methods in [[Command]], these methods will display extra information showing the method parameters and execution time, which may be useful when applying migrations.
For more details and usage information on Migration, see the guide article on Migration.
Tags
Table of Contents
Interfaces
- MigrationInterface
- The MigrationInterface defines the minimum set of methods to be implemented by a database migration.
Properties
- $behaviors : array<string|int, Behavior>
- $compact : bool
- $db : Connection|array<string|int, mixed>|string
- $maxSqlOutputLength : int
- $_behaviors : array<string|int, Behavior>|null
- $_events : array<string|int, mixed>
- $_eventWildcards : array<string|int, mixed>
Methods
- __call() : mixed
- Calls the named method which is not a class method.
- __clone() : mixed
- This method is called after the object is created by cloning an existing one.
- __construct() : mixed
- Constructor.
- __get() : mixed
- Returns the value of a component property.
- __isset() : bool
- Checks if a property is set, i.e. defined and not null.
- __set() : mixed
- Sets the value of a component property.
- __unset() : mixed
- Sets a component property to be null.
- addCheck() : mixed
- Creates a SQL command for adding a check constraint to an existing table.
- addColumn() : mixed
- Builds and executes a SQL statement for adding a new DB column.
- addCommentOnColumn() : mixed
- Builds and execute a SQL statement for adding comment to column.
- addCommentOnTable() : mixed
- Builds a SQL statement for adding comment to table.
- addForeignKey() : mixed
- Builds a SQL statement for adding a foreign key constraint to an existing table.
- addPrimaryKey() : mixed
- Builds and executes a SQL statement for creating a primary key.
- alterColumn() : mixed
- Builds and executes a SQL statement for changing the definition of a column.
- attachBehavior() : Behavior
- Attaches a behavior to this component.
- attachBehaviors() : mixed
- Attaches a list of behaviors to the component.
- batchInsert() : mixed
- Creates and executes a batch INSERT SQL statement.
- behaviors() : array<string|int, mixed>
- Returns a list of behaviors that this component should behave as.
- bigInteger() : ColumnSchemaBuilder
- Creates a bigint column.
- bigPrimaryKey() : ColumnSchemaBuilder
- Creates a big primary key column.
- binary() : ColumnSchemaBuilder
- Creates a binary column.
- boolean() : ColumnSchemaBuilder
- Creates a boolean column.
- canGetProperty() : bool
- Returns a value indicating whether a property can be read.
- canSetProperty() : bool
- Returns a value indicating whether a property can be set.
- char() : ColumnSchemaBuilder
- Creates a char column.
- className() : string
- Returns the fully qualified name of this class.
- createIndex() : mixed
- Builds and executes a SQL statement for creating a new index.
- createTable() : mixed
- Builds and executes a SQL statement for creating a new DB table.
- date() : ColumnSchemaBuilder
- Creates a date column.
- dateTime() : ColumnSchemaBuilder
- Creates a datetime column.
- decimal() : ColumnSchemaBuilder
- Creates a decimal column.
- delete() : mixed
- Creates and executes a DELETE SQL statement.
- detachBehavior() : Behavior|null
- Detaches a behavior from the component.
- detachBehaviors() : mixed
- Detaches all behaviors from the component.
- double() : ColumnSchemaBuilder
- Creates a double column.
- down() : false|void|mixed
- This method contains the logic to be executed when removing this migration.
- dropCheck() : mixed
- Creates a SQL command for dropping a check constraint.
- dropColumn() : mixed
- Builds and executes a SQL statement for dropping a DB column.
- dropCommentFromColumn() : mixed
- Builds and execute a SQL statement for dropping comment from column.
- dropCommentFromTable() : mixed
- Builds a SQL statement for dropping comment from table.
- dropForeignKey() : mixed
- Builds a SQL statement for dropping a foreign key constraint.
- dropIndex() : mixed
- Builds and executes a SQL statement for dropping an index.
- dropPrimaryKey() : mixed
- Builds and executes a SQL statement for dropping a primary key.
- dropTable() : mixed
- Builds and executes a SQL statement for dropping a DB table.
- ensureBehaviors() : mixed
- Makes sure that the behaviors declared in [[behaviors()]] are attached to this component.
- execute() : mixed
- Executes a SQL statement.
- float() : ColumnSchemaBuilder
- Creates a float column.
- getBehavior() : Behavior|null
- Returns the named behavior object.
- getBehaviors() : array<string|int, Behavior>
- Returns all behaviors attached to this component.
- hasEventHandlers() : bool
- Returns a value indicating whether there is any handler attached to the named event.
- hasMethod() : bool
- Returns a value indicating whether a method is defined.
- hasProperty() : bool
- Returns a value indicating whether a property is defined for this component.
- init() : mixed
- Initializes the migration.
- insert() : mixed
- Creates and executes an INSERT SQL statement.
- integer() : ColumnSchemaBuilder
- Creates an integer column.
- json() : ColumnSchemaBuilder
- Creates a JSON column.
- money() : ColumnSchemaBuilder
- Creates a money column.
- off() : bool
- Detaches an existing event handler from this component.
- on() : mixed
- Attaches an event handler to an event.
- primaryKey() : ColumnSchemaBuilder
- Creates a primary key column.
- renameColumn() : mixed
- Builds and executes a SQL statement for renaming a column.
- renameTable() : mixed
- Builds and executes a SQL statement for renaming a DB table.
- safeDown() : false|void|mixed
- This method contains the logic to be executed when removing this migration.
- safeUp() : false|void|mixed
- This method contains the logic to be executed when applying this migration.
- smallInteger() : ColumnSchemaBuilder
- Creates a smallint column.
- string() : ColumnSchemaBuilder
- Creates a string column.
- text() : ColumnSchemaBuilder
- Creates a text column.
- time() : ColumnSchemaBuilder
- Creates a time column.
- timestamp() : ColumnSchemaBuilder
- Creates a timestamp column.
- tinyInteger() : ColumnSchemaBuilder
- Creates a tinyint column. If tinyint is not supported by the DBMS, smallint will be used.
- trigger() : mixed
- Triggers an event.
- truncateTable() : mixed
- Builds and executes a SQL statement for truncating a DB table.
- up() : false|void|mixed
- This method contains the logic to be executed when applying this migration.
- update() : mixed
- Creates and executes an UPDATE SQL statement.
- upsert() : mixed
- Creates and executes a command to insert rows into a database table if they do not already exist (matching unique constraints), or update them if they do.
- beginCommand() : float
- Prepares for a command to be executed, and outputs to the console.
- endCommand() : mixed
- Finalizes after the command has been executed, and outputs to the console the time elapsed.
- getDb() : mixed
- {@inheritdoc}
- attachBehaviorInternal() : Behavior
- Attaches a behavior to this component.
- printException() : mixed
Properties
$behaviors read-only
public
array<string|int, Behavior>
$behaviors
List of behaviors attached to this component.
$compact
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.
Tags
$db
public
Connection|array<string|int, mixed>|string
$db
= 'db'
the DB connection object or the application component ID of the DB connection that this migration should work with. Starting from version 2.0.2, this can also be a configuration array for creating the object.
Note that when a Migration object is created by the migrate
command, this property will be overwritten
by the command. If you do not want to use the DB connection provided by the command, you may override
the [[init()]] method like the following:
public function init()
{
$this->db = 'db2';
parent::init();
}
$maxSqlOutputLength
public
int
$maxSqlOutputLength
max number of characters of the SQL outputted. Useful for reduction of long statements and making console output more compact.
Tags
$_behaviors
private
array<string|int, Behavior>|null
$_behaviors
the attached behaviors (behavior name => behavior). This is null
when not initialized.
$_events
private
array<string|int, mixed>
$_events
= []
the attached event handlers (event name => handlers)
$_eventWildcards
private
array<string|int, mixed>
$_eventWildcards
= []
the event handlers attached for wildcard patterns (event name wildcard => handlers)
Tags
Methods
__call()
Calls the named method which is not a class method.
public
__call(string $name, array<string|int, mixed> $params) : mixed
This method will check if any attached behavior has the named method and will execute it if available.
Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.
Parameters
- $name : string
-
the method name
- $params : array<string|int, mixed>
-
method parameters
Tags
Return values
mixed —the method return value
__clone()
This method is called after the object is created by cloning an existing one.
public
__clone() : mixed
It removes all behaviors because they are attached to the old object.
__construct()
Constructor.
public
__construct([array<string|int, mixed> $config = [] ]) : mixed
The default implementation does two things:
- Initializes the object with the given configuration
$config
. - Call [[init()]].
If this method is overridden in a child class, it is recommended that
- the last parameter of the constructor is a configuration array, like
$config
here. - call the parent implementation at the end of the constructor.
Parameters
- $config : array<string|int, mixed> = []
-
name-value pairs that will be used to initialize the object properties
__get()
Returns the value of a component property.
public
__get(string $name) : mixed
This method will check in the following order and act accordingly:
- a property defined by a getter: return the getter result
- a property of a behavior: return the behavior property value
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $value = $component->property;
.
Parameters
- $name : string
-
the property name
Tags
Return values
mixed —the property value or the value of a behavior's property
__isset()
Checks if a property is set, i.e. defined and not null.
public
__isset(string $name) : bool
This method will check in the following order and act accordingly:
- a property defined by a setter: return whether the property is set
- a property of a behavior: return whether the property is set
- return
false
for non existing properties
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing isset($component->property)
.
Parameters
- $name : string
-
the property name or the event name
Tags
Return values
bool —whether the named property is set
__set()
Sets the value of a component property.
public
__set(string $name, mixed $value) : mixed
This method will check in the following order and act accordingly:
- a property defined by a setter: set the property value
- an event in the format of "on xyz": attach the handler to the event "xyz"
- a behavior in the format of "as xyz": attach the behavior named as "xyz"
- a property of a behavior: set the behavior property value
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $component->property = $value;
.
Parameters
- $name : string
-
the property name or the event name
- $value : mixed
-
the property value
Tags
__unset()
Sets a component property to be null.
public
__unset(string $name) : mixed
This method will check in the following order and act accordingly:
- a property defined by a setter: set the property value to be null
- a property of a behavior: set the property value to be null
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing unset($component->property)
.
Parameters
- $name : string
-
the property name
Tags
addCheck()
Creates a SQL command for adding a check constraint to an existing table.
public
addCheck(string $name, string $table, string $expression) : mixed
Parameters
- $name : string
-
the name of the check constraint. The name will be properly quoted by the method.
- $table : string
-
the table that the check constraint will be added to. The name will be properly quoted by the method.
- $expression : string
-
the SQL of the
CHECK
constraint.
addColumn()
Builds and executes a SQL statement for adding a new DB column.
public
addColumn(string $table, string $column, string $type) : mixed
Parameters
- $table : string
-
the table that the new column will be added to. The table name will be properly quoted by the method.
- $column : string
-
the name of the new column. The name will be properly quoted by the method.
- $type : string
-
the column type. The [[QueryBuilder::getColumnType()]] method will be invoked to convert abstract column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'.
addCommentOnColumn()
Builds and execute a SQL statement for adding comment to column.
public
addCommentOnColumn(string $table, string $column, string $comment) : mixed
Parameters
- $table : string
-
the table whose column is to be commented. The table name will be properly quoted by the method.
- $column : string
-
the name of the column to be commented. The column name will be properly quoted by the method.
- $comment : string
-
the text of the comment to be added. The comment will be properly quoted by the method.
Tags
addCommentOnTable()
Builds a SQL statement for adding comment to table.
public
addCommentOnTable(string $table, string $comment) : mixed
Parameters
- $table : string
-
the table to be commented. The table name will be properly quoted by the method.
- $comment : string
-
the text of the comment to be added. The comment will be properly quoted by the method.
Tags
addForeignKey()
Builds a SQL statement for adding a foreign key constraint to an existing table.
public
addForeignKey(string $name, string $table, string|array<string|int, mixed> $columns, string $refTable, string|array<string|int, mixed> $refColumns[, string|null $delete = null ][, string|null $update = null ]) : mixed
The method will properly quote the table and column names.
Parameters
- $name : string
-
the name of the foreign key constraint.
- $table : string
-
the table that the foreign key constraint will be added to.
- $columns : string|array<string|int, mixed>
-
the name of the column to that the constraint will be added on. If there are multiple columns, separate them with commas or use an array.
- $refTable : string
-
the table that the foreign key references to.
- $refColumns : string|array<string|int, mixed>
-
the name of the column that the foreign key references to. If there are multiple columns, separate them with commas or use an array.
- $delete : string|null = null
-
the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
- $update : string|null = null
-
the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
addPrimaryKey()
Builds and executes a SQL statement for creating a primary key.
public
addPrimaryKey(string $name, string $table, string|array<string|int, mixed> $columns) : mixed
The method will properly quote the table and column names.
Parameters
- $name : string
-
the name of the primary key constraint.
- $table : string
-
the table that the primary key constraint will be added to.
- $columns : string|array<string|int, mixed>
-
comma separated string or array of columns that the primary key will consist of.
alterColumn()
Builds and executes a SQL statement for changing the definition of a column.
public
alterColumn(string $table, string $column, string $type) : mixed
Parameters
- $table : string
-
the table whose column is to be changed. The table name will be properly quoted by the method.
- $column : string
-
the name of the column to be changed. The name will be properly quoted by the method.
- $type : string
-
the new column type. The [[QueryBuilder::getColumnType()]] method will be invoked to convert abstract column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'.
attachBehavior()
Attaches a behavior to this component.
public
attachBehavior(string $name, string|array<string|int, mixed>|Behavior $behavior) : Behavior
This method will create the behavior object based on the given configuration. After that, the behavior object will be attached to this component by calling the [[Behavior::attach()]] method.
Parameters
- $name : string
-
the name of the behavior.
- $behavior : string|array<string|int, mixed>|Behavior
-
the behavior configuration. This can be one of the following:
- a [[Behavior]] object
- a string specifying the behavior class
- an object configuration array that will be passed to [[Yii::createObject()]] to create the behavior object.
Tags
Return values
Behavior —the behavior object
attachBehaviors()
Attaches a list of behaviors to the component.
public
attachBehaviors(array<string|int, mixed> $behaviors) : mixed
Each behavior is indexed by its name and should be a [[Behavior]] object, a string specifying the behavior class, or an configuration array for creating the behavior.
Parameters
- $behaviors : array<string|int, mixed>
-
list of behaviors to be attached to the component
Tags
batchInsert()
Creates and executes a batch INSERT SQL statement.
public
batchInsert(string $table, array<string|int, mixed> $columns, array<string|int, mixed> $rows) : mixed
The method will properly escape the column names, and bind the values to be inserted.
Parameters
- $table : string
-
the table that new rows will be inserted into.
- $columns : array<string|int, mixed>
-
the column names.
- $rows : array<string|int, mixed>
-
the rows to be batch inserted into the table
behaviors()
Returns a list of behaviors that this component should behave as.
public
behaviors() : array<string|int, mixed>
Child classes may override this method to specify the behaviors they want to behave as.
The return value of this method should be an array of behavior objects or configurations indexed by behavior names. A behavior configuration can be either a string specifying the behavior class or an array of the following structure:
'behaviorName' => [
'class' => 'BehaviorClass',
'property1' => 'value1',
'property2' => 'value2',
]
Note that a behavior class must extend from [[Behavior]]. Behaviors can be attached using a name or anonymously. When a name is used as the array key, using this name, the behavior can later be retrieved using [[getBehavior()]] or be detached using [[detachBehavior()]]. Anonymous behaviors can not be retrieved or detached.
Behaviors declared in this method will be attached to the component automatically (on demand).
Return values
array<string|int, mixed> —the behavior configurations.
bigInteger()
Creates a bigint column.
public
bigInteger([int|null $length = null ]) : ColumnSchemaBuilder
Parameters
- $length : int|null = null
-
column size or precision definition. This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
bigPrimaryKey()
Creates a big primary key column.
public
bigPrimaryKey([int|null $length = null ]) : ColumnSchemaBuilder
Parameters
- $length : int|null = null
-
column size or precision definition. This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
binary()
Creates a binary column.
public
binary([int|null $length = null ]) : ColumnSchemaBuilder
Parameters
- $length : int|null = null
-
column size or precision definition. This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
boolean()
Creates a boolean column.
public
boolean() : ColumnSchemaBuilder
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
canGetProperty()
Returns a value indicating whether a property can be read.
public
canGetProperty(string $name[, bool $checkVars = true ][, bool $checkBehaviors = true ]) : bool
A property can be read if:
- the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVars
is true); - an attached behavior has a readable property of the given name (when
$checkBehaviors
is true).
Parameters
- $name : string
-
the property name
- $checkVars : bool = true
-
whether to treat member variables as properties
- $checkBehaviors : bool = true
-
whether to treat behaviors' properties as properties of this component
Tags
Return values
bool —whether the property can be read
canSetProperty()
Returns a value indicating whether a property can be set.
public
canSetProperty(string $name[, bool $checkVars = true ][, bool $checkBehaviors = true ]) : bool
A property can be written if:
- the class has a setter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVars
is true); - an attached behavior has a writable property of the given name (when
$checkBehaviors
is true).
Parameters
- $name : string
-
the property name
- $checkVars : bool = true
-
whether to treat member variables as properties
- $checkBehaviors : bool = true
-
whether to treat behaviors' properties as properties of this component
Tags
Return values
bool —whether the property can be written
char()
Creates a char column.
public
char([int|null $length = null ]) : ColumnSchemaBuilder
Parameters
- $length : int|null = null
-
column size definition i.e. the maximum string length. This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
className()
Returns the fully qualified name of this class.
public
static className() : string
Tags
Return values
string —the fully qualified name of this class.
createIndex()
Builds and executes a SQL statement for creating a new index.
public
createIndex(string $name, string $table, string|array<string|int, mixed> $columns[, bool $unique = false ]) : mixed
Parameters
- $name : string
-
the name of the index. The name will be properly quoted by the method.
- $table : string
-
the table that the new index will be created for. The table name will be properly quoted by the method.
- $columns : string|array<string|int, mixed>
-
the column(s) that should be included in the index. If there are multiple columns, please separate them by commas or use an array. Each column name will be properly quoted by the method. Quoting will be skipped for column names that include a left parenthesis "(".
- $unique : bool = false
-
whether to add UNIQUE constraint on the created index.
createTable()
Builds and executes a SQL statement for creating a new DB table.
public
createTable(string $table, array<string|int, mixed> $columns[, string|null $options = null ]) : mixed
The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), where name stands for a column name which will be properly quoted by the method, and definition stands for the column type which must contain an abstract DB type.
The [[QueryBuilder::getColumnType()]] method will be invoked to convert any abstract type into a physical one.
If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly put into the generated SQL.
Example usage:
class m200000_000000_create_table_fruits extends \yii\db\Migration
{
public function safeUp()
{
$this->createTable('{{%fruits}}', [
// ...
'column_name double precision null default null',
Parameters
- $table : string
-
the name of the table to be created. The name will be properly quoted by the method.
- $columns : array<string|int, mixed>
-
the columns (name => definition) in the new table.
- $options : string|null = null
-
additional SQL fragment that will be appended to the generated SQL.
date()
Creates a date column.
public
date() : ColumnSchemaBuilder
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
dateTime()
Creates a datetime column.
public
dateTime([int|null $precision = null ]) : ColumnSchemaBuilder
Parameters
- $precision : int|null = null
-
column value precision. First parameter passed to the column type, e.g. DATETIME(precision). This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
decimal()
Creates a decimal column.
public
decimal([int|null $precision = null ][, int|null $scale = null ]) : ColumnSchemaBuilder
Parameters
- $precision : int|null = null
-
column value precision, which is usually the total number of digits. First parameter passed to the column type, e.g. DECIMAL(precision, scale). This parameter will be ignored if not supported by the DBMS.
- $scale : int|null = null
-
column value scale, which is usually the number of digits after the decimal point. Second parameter passed to the column type, e.g. DECIMAL(precision, scale). This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
delete()
Creates and executes a DELETE SQL statement.
public
delete(string $table[, array<string|int, mixed>|string $condition = '' ][, array<string|int, mixed> $params = [] ]) : mixed
Parameters
- $table : string
-
the table where the data will be deleted from.
- $condition : array<string|int, mixed>|string = ''
-
the conditions that will be put in the WHERE part. Please refer to [[Query::where()]] on how to specify conditions.
- $params : array<string|int, mixed> = []
-
the parameters to be bound to the query.
detachBehavior()
Detaches a behavior from the component.
public
detachBehavior(string $name) : Behavior|null
The behavior's [[Behavior::detach()]] method will be invoked.
Parameters
- $name : string
-
the behavior's name.
Return values
Behavior|null —the detached behavior. Null if the behavior does not exist.
detachBehaviors()
Detaches all behaviors from the component.
public
detachBehaviors() : mixed
double()
Creates a double column.
public
double([int|null $precision = null ]) : ColumnSchemaBuilder
Parameters
- $precision : int|null = null
-
column value precision. First parameter passed to the column type, e.g. DOUBLE(precision). This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
down()
This method contains the logic to be executed when removing this migration.
public
down() : false|void|mixed
The default implementation throws an exception indicating the migration cannot be removed. Child classes may override this method if the corresponding migrations can be removed.
Return values
false|void|mixed —return a false value to indicate the migration fails and should not proceed further. All other return values mean the migration succeeds.
dropCheck()
Creates a SQL command for dropping a check constraint.
public
dropCheck(string $name, string $table) : mixed
Parameters
- $name : string
-
the name of the check constraint to be dropped. The name will be properly quoted by the method.
- $table : string
-
the table whose check constraint is to be dropped. The name will be properly quoted by the method.
dropColumn()
Builds and executes a SQL statement for dropping a DB column.
public
dropColumn(string $table, string $column) : mixed
Parameters
- $table : string
-
the table whose column is to be dropped. The name will be properly quoted by the method.
- $column : string
-
the name of the column to be dropped. The name will be properly quoted by the method.
dropCommentFromColumn()
Builds and execute a SQL statement for dropping comment from column.
public
dropCommentFromColumn(string $table, string $column) : mixed
Parameters
- $table : string
-
the table whose column is to be commented. The table name will be properly quoted by the method.
- $column : string
-
the name of the column to be commented. The column name will be properly quoted by the method.
Tags
dropCommentFromTable()
Builds a SQL statement for dropping comment from table.
public
dropCommentFromTable(string $table) : mixed
Parameters
- $table : string
-
the table whose column is to be commented. The table name will be properly quoted by the method.
Tags
dropForeignKey()
Builds a SQL statement for dropping a foreign key constraint.
public
dropForeignKey(string $name, string $table) : mixed
Parameters
- $name : string
-
the name of the foreign key constraint to be dropped. The name will be properly quoted by the method.
- $table : string
-
the table whose foreign is to be dropped. The name will be properly quoted by the method.
dropIndex()
Builds and executes a SQL statement for dropping an index.
public
dropIndex(string $name, string $table) : mixed
Parameters
- $name : string
-
the name of the index to be dropped. The name will be properly quoted by the method.
- $table : string
-
the table whose index is to be dropped. The name will be properly quoted by the method.
dropPrimaryKey()
Builds and executes a SQL statement for dropping a primary key.
public
dropPrimaryKey(string $name, string $table) : mixed
Parameters
- $name : string
-
the name of the primary key constraint to be removed.
- $table : string
-
the table that the primary key constraint will be removed from.
dropTable()
Builds and executes a SQL statement for dropping a DB table.
public
dropTable(string $table) : mixed
Parameters
- $table : string
-
the table to be dropped. The name will be properly quoted by the method.
ensureBehaviors()
Makes sure that the behaviors declared in [[behaviors()]] are attached to this component.
public
ensureBehaviors() : mixed
execute()
Executes a SQL statement.
public
execute(string $sql[, array<string|int, mixed> $params = [] ]) : mixed
This method executes the specified SQL statement using [[db]].
Parameters
- $sql : string
-
the SQL statement to be executed
- $params : array<string|int, mixed> = []
-
input parameters (name => value) for the SQL execution. See [[Command::execute()]] for more details.
float()
Creates a float column.
public
float([int|null $precision = null ]) : ColumnSchemaBuilder
Parameters
- $precision : int|null = null
-
column value precision. First parameter passed to the column type, e.g. FLOAT(precision). This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
getBehavior()
Returns the named behavior object.
public
getBehavior(string $name) : Behavior|null
Parameters
- $name : string
-
the behavior name
Return values
Behavior|null —the behavior object, or null if the behavior does not exist
getBehaviors()
Returns all behaviors attached to this component.
public
getBehaviors() : array<string|int, Behavior>
Return values
array<string|int, Behavior> —list of behaviors attached to this component
hasEventHandlers()
Returns a value indicating whether there is any handler attached to the named event.
public
hasEventHandlers(string $name) : bool
Parameters
- $name : string
-
the event name
Return values
bool —whether there is any handler attached to the event.
hasMethod()
Returns a value indicating whether a method is defined.
public
hasMethod(string $name[, bool $checkBehaviors = true ]) : bool
A method is defined if:
- the class has a method with the specified name
- an attached behavior has a method with the given name (when
$checkBehaviors
is true).
Parameters
- $name : string
-
the property name
- $checkBehaviors : bool = true
-
whether to treat behaviors' methods as methods of this component
Return values
bool —whether the method is defined
hasProperty()
Returns a value indicating whether a property is defined for this component.
public
hasProperty(string $name[, bool $checkVars = true ][, bool $checkBehaviors = true ]) : bool
A property is defined if:
- the class has a getter or setter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVars
is true); - an attached behavior has a property of the given name (when
$checkBehaviors
is true).
Parameters
- $name : string
-
the property name
- $checkVars : bool = true
-
whether to treat member variables as properties
- $checkBehaviors : bool = true
-
whether to treat behaviors' properties as properties of this component
Tags
Return values
bool —whether the property is defined
init()
Initializes the migration.
public
init() : mixed
This method will set [[db]] to be the 'db' application component, if it is null
.
insert()
Creates and executes an INSERT SQL statement.
public
insert(string $table, array<string|int, mixed> $columns) : mixed
The method will properly escape the column names, and bind the values to be inserted.
Parameters
- $table : string
-
the table that new rows will be inserted into.
- $columns : array<string|int, mixed>
-
the column data (name => value) to be inserted into the table.
integer()
Creates an integer column.
public
integer([int|null $length = null ]) : ColumnSchemaBuilder
Parameters
- $length : int|null = null
-
column size or precision definition. This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
json()
Creates a JSON column.
public
json() : ColumnSchemaBuilder
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
money()
Creates a money column.
public
money([int|null $precision = null ][, int|null $scale = null ]) : ColumnSchemaBuilder
Parameters
- $precision : int|null = null
-
column value precision, which is usually the total number of digits. First parameter passed to the column type, e.g. DECIMAL(precision, scale). This parameter will be ignored if not supported by the DBMS.
- $scale : int|null = null
-
column value scale, which is usually the number of digits after the decimal point. Second parameter passed to the column type, e.g. DECIMAL(precision, scale). This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
off()
Detaches an existing event handler from this component.
public
off(string $name[, callable|null $handler = null ]) : bool
This method is the opposite of [[on()]].
Note: in case wildcard pattern is passed for event name, only the handlers registered with this wildcard will be removed, while handlers registered with plain names matching this wildcard will remain.
Parameters
- $name : string
-
event name
- $handler : callable|null = null
-
the event handler to be removed. If it is null, all handlers attached to the named event will be removed.
Tags
Return values
bool —if a handler is found and detached
on()
Attaches an event handler to an event.
public
on(string $name, callable $handler[, mixed $data = null ][, bool $append = true ]) : mixed
The event handler must be a valid PHP callback. The following are some examples:
function ($event) { ... } // anonymous function
[$object, 'handleClick'] // $object->handleClick()
['Page', 'handleClick'] // Page::handleClick()
'handleClick' // global function handleClick()
The event handler must be defined with the following signature,
function ($event)
where $event
is an [[Event]] object which includes parameters associated with the event.
Since 2.0.14 you can specify event name as a wildcard pattern:
$component->on('event.group.*', function ($event) {
Yii::trace($event->name . ' is triggered.');
});
Parameters
- $name : string
-
the event name
- $handler : callable
-
the event handler
- $data : mixed = null
-
the data to be passed to the event handler when the event is triggered. When the event handler is invoked, this data can be accessed via [[Event::data]].
- $append : bool = true
-
whether to append new event handler to the end of the existing handler list. If false, the new handler will be inserted at the beginning of the existing handler list.
Tags
primaryKey()
Creates a primary key column.
public
primaryKey([int|null $length = null ]) : ColumnSchemaBuilder
Parameters
- $length : int|null = null
-
column size or precision definition. This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
renameColumn()
Builds and executes a SQL statement for renaming a column.
public
renameColumn(string $table, string $name, string $newName) : mixed
Parameters
- $table : string
-
the table whose column is to be renamed. The name will be properly quoted by the method.
- $name : string
-
the old name of the column. The name will be properly quoted by the method.
- $newName : string
-
the new name of the column. The name will be properly quoted by the method.
renameTable()
Builds and executes a SQL statement for renaming a DB table.
public
renameTable(string $table, string $newName) : mixed
Parameters
- $table : string
-
the table to be renamed. The name will be properly quoted by the method.
- $newName : string
-
the new table name. The name will be properly quoted by the method.
safeDown()
This method contains the logic to be executed when removing this migration.
public
safeDown() : false|void|mixed
This method differs from [[down()]] in that the DB logic implemented here will be enclosed within a DB transaction. Child classes may implement this method instead of [[down()]] if the DB logic needs to be within a transaction.
Note: Not all DBMS support transactions. And some DB queries cannot be put into a transaction. For some examples, please refer to implicit commit.
Return values
false|void|mixed —return a false value to indicate the migration fails and should not proceed further. All other return values mean the migration succeeds.
safeUp()
This method contains the logic to be executed when applying this migration.
public
safeUp() : false|void|mixed
This method differs from [[up()]] in that the DB logic implemented here will be enclosed within a DB transaction. Child classes may implement this method instead of [[up()]] if the DB logic needs to be within a transaction.
Note: Not all DBMS support transactions. And some DB queries cannot be put into a transaction. For some examples, please refer to implicit commit.
Return values
false|void|mixed —return a false value to indicate the migration fails and should not proceed further. All other return values mean the migration succeeds.
smallInteger()
Creates a smallint column.
public
smallInteger([int|null $length = null ]) : ColumnSchemaBuilder
Parameters
- $length : int|null = null
-
column size or precision definition. This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
string()
Creates a string column.
public
string([int|null $length = null ]) : ColumnSchemaBuilder
Parameters
- $length : int|null = null
-
column size definition i.e. the maximum string length. This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
text()
Creates a text column.
public
text() : ColumnSchemaBuilder
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
time()
Creates a time column.
public
time([int|null $precision = null ]) : ColumnSchemaBuilder
Parameters
- $precision : int|null = null
-
column value precision. First parameter passed to the column type, e.g. TIME(precision). This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
timestamp()
Creates a timestamp column.
public
timestamp([int|null $precision = null ]) : ColumnSchemaBuilder
Parameters
- $precision : int|null = null
-
column value precision. First parameter passed to the column type, e.g. TIMESTAMP(precision). This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
tinyInteger()
Creates a tinyint column. If tinyint is not supported by the DBMS, smallint will be used.
public
tinyInteger([int|null $length = null ]) : ColumnSchemaBuilder
Parameters
- $length : int|null = null
-
column size or precision definition. This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
trigger()
Triggers an event.
public
trigger(string $name[, Event|null $event = null ]) : mixed
This method represents the happening of an event. It invokes all attached handlers for the event including class-level handlers.
Parameters
- $name : string
-
the event name
- $event : Event|null = null
-
the event instance. If not set, a default [[Event]] object will be created.
truncateTable()
Builds and executes a SQL statement for truncating a DB table.
public
truncateTable(string $table) : mixed
Parameters
- $table : string
-
the table to be truncated. The name will be properly quoted by the method.
up()
This method contains the logic to be executed when applying this migration.
public
up() : false|void|mixed
Child classes may override this method to provide actual migration logic.
Return values
false|void|mixed —return a false value to indicate the migration fails and should not proceed further. All other return values mean the migration succeeds.
update()
Creates and executes an UPDATE SQL statement.
public
update(string $table, array<string|int, mixed> $columns[, array<string|int, mixed>|string $condition = '' ][, array<string|int, mixed> $params = [] ]) : mixed
The method will properly escape the column names and bind the values to be updated.
Parameters
- $table : string
-
the table to be updated.
- $columns : array<string|int, mixed>
-
the column data (name => value) to be updated.
- $condition : array<string|int, mixed>|string = ''
-
the conditions that will be put in the WHERE part. Please refer to [[Query::where()]] on how to specify conditions.
- $params : array<string|int, mixed> = []
-
the parameters to be bound to the query.
upsert()
Creates and executes a command to insert rows into a database table if they do not already exist (matching unique constraints), or update them if they do.
public
upsert(string $table, array<string|int, mixed>|Query $insertColumns[, array<string|int, mixed>|bool $updateColumns = true ][, array<string|int, mixed> $params = [] ]) : mixed
The method will properly escape the column names, and bind the values to be inserted.
Parameters
- $table : string
-
the table that new rows will be inserted into/updated in.
- $insertColumns : array<string|int, mixed>|Query
-
the column data (name => value) to be inserted into the table or instance of [[Query]] to perform
INSERT INTO ... SELECT
SQL statement. - $updateColumns : array<string|int, mixed>|bool = true
-
the column data (name => value) to be updated if they already exist. If
true
is passed, the column data will be updated to match the insert column data. Iffalse
is passed, no update will be performed if the column data already exists. - $params : array<string|int, mixed> = []
-
the parameters to be bound to the command.
Tags
beginCommand()
Prepares for a command to be executed, and outputs to the console.
protected
beginCommand(string $description) : float
Parameters
- $description : string
-
the description for the command, to be output to the console.
Tags
Return values
float —the time before the command is executed, for the time elapsed to be calculated.
endCommand()
Finalizes after the command has been executed, and outputs to the console the time elapsed.
protected
endCommand(float $time) : mixed
Parameters
- $time : float
-
the time before the command was executed.
Tags
getDb()
{@inheritdoc}
protected
getDb() : mixed
Tags
attachBehaviorInternal()
Attaches a behavior to this component.
private
attachBehaviorInternal(string|int $name, string|array<string|int, mixed>|Behavior $behavior) : Behavior
Parameters
- $name : string|int
-
the name of the behavior. If this is an integer, it means the behavior is an anonymous one. Otherwise, the behavior is a named one and any existing behavior with the same name will be detached first.
- $behavior : string|array<string|int, mixed>|Behavior
-
the behavior to be attached
Return values
Behavior —the attached behavior.
printException()
private
printException(Throwable $e) : mixed
Parameters
- $e : Throwable