Craft 3 Class Reference

Abstract Class craft\db\Migration

Inheritance
craft\db\Migration » yii\db\Migration » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable, yii\db\MigrationInterface
Uses Traits
yii\db\SchemaBuilderTrait
Subclasses
craft\migrations\CreateMatrixContentTable, craft\migrations\Install
Available since version
3.0
Source Code
https://github.com/craftcms/cms/blob/master/src/db/Migration.php

Public Properties
Property Type Description Defined By
$compact boolean Indicates whether the console output should be compacted. yii\db\Migration
$db yii\db\Connection, array, string The DB connection object or the application component ID of the DB connection that this migration should work with. yii\db\Migration
$maxSqlOutputLength integer Max number of characters of the SQL outputted. yii\db\Migration
Public Methods
Method Description Defined By
__call() Calls the named method which is not a class method. yii\base\BaseObject
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__unset() Sets an object property to null. yii\base\BaseObject
addColumn() Builds and executes a SQL statement for adding a new DB column. yii\db\Migration
addCommentOnColumn() Builds and execute a SQL statement for adding comment to column. yii\db\Migration
addCommentOnTable() Builds a SQL statement for adding comment to table. yii\db\Migration
addForeignKey() Builds a SQL statement for adding a foreign key constraint to an existing table. craft\db\Migration
addPrimaryKey() Builds and executes a SQL statement for creating a primary key. craft\db\Migration
alterColumn() Builds and executes a SQL statement for changing the definition of a column. yii\db\Migration
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
batchInsert() Creates and executes an batch INSERT SQL statement. craft\db\Migration
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
bigInteger() Creates a bigint column. yii\db\SchemaBuilderTrait
bigPrimaryKey() Creates a big primary key column. yii\db\SchemaBuilderTrait
binary() Creates a binary column. yii\db\SchemaBuilderTrait
boolean() Creates a boolean column. yii\db\SchemaBuilderTrait
canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
char() Creates a char column. yii\db\SchemaBuilderTrait
className() Returns the fully qualified name of this class. yii\base\BaseObject
createIndex() Builds and executes a SQL statement for creating a new index. craft\db\Migration
createTable() Builds and executes a SQL statement for creating a new DB table. yii\db\Migration
date() Creates a date column. yii\db\SchemaBuilderTrait
dateTime() Creates a datetime column. yii\db\SchemaBuilderTrait
decimal() Creates a decimal column. yii\db\SchemaBuilderTrait
delete() Creates and executes a DELETE SQL statement. yii\db\Migration
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
double() Creates a double column. yii\db\SchemaBuilderTrait
down() This method contains the logic to be executed when removing this migration. craft\db\Migration
dropColumn() Builds and executes a SQL statement for dropping a DB column. yii\db\Migration
dropCommentFromColumn() Builds and execute a SQL statement for dropping comment from column. yii\db\Migration
dropCommentFromTable() Builds a SQL statement for dropping comment from table. yii\db\Migration
dropForeignKey() Builds a SQL statement for dropping a foreign key constraint. yii\db\Migration
dropIndex() Builds and executes a SQL statement for dropping an index. yii\db\Migration
dropPrimaryKey() Builds and executes a SQL statement for dropping a primary key. yii\db\Migration
dropTable() Builds and executes a SQL statement for dropping a DB table. yii\db\Migration
dropTableIfExists() Creates and executes a SQL statement for dropping a DB table, if it exists. craft\db\Migration
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
enum() Creates an enum column for MySQL and PostgreSQL, or a string column with a check constraint for others. craft\db\Migration
execute() Executes a SQL statement. yii\db\Migration
float() Creates a float column. yii\db\SchemaBuilderTrait
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
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\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
init() Initializes the object. yii\base\BaseObject
insert() Creates and executes an INSERT SQL statement. craft\db\Migration
integer() Creates an integer column. yii\db\SchemaBuilderTrait
json() Creates a JSON column. yii\db\SchemaBuilderTrait
longText() Creates a longtext column for MySQL, or text column for others. craft\db\Migration
mediumText() Creates a mediumtext column for MySQL, or text column for others. craft\db\Migration
money() Creates a money column. yii\db\SchemaBuilderTrait
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
primaryKey() Creates a primary key column. yii\db\SchemaBuilderTrait
renameColumn() Builds and executes a SQL statement for renaming a column. yii\db\Migration
renameSequence() Creates and executes a SQL statement for renaming a DB sequence. craft\db\Migration
renameTable() Builds and executes a SQL statement for renaming a DB table. yii\db\Migration
replace() Creates and executes a SQL statement for replacing some text with other text in a given table column. craft\db\Migration
safeDown() This method contains the logic to be executed when removing this migration. yii\db\Migration
safeUp() This method contains the logic to be executed when applying this migration. yii\db\Migration
smallInteger() Creates a smallint column. yii\db\SchemaBuilderTrait
string() Creates a string column. yii\db\SchemaBuilderTrait
text() Creates a text column. yii\db\SchemaBuilderTrait
time() Creates a time column. yii\db\SchemaBuilderTrait
timestamp() Creates a timestamp column. yii\db\SchemaBuilderTrait
tinyInteger() Creates a tinyint column. If tinyint is not supported by the DBMS, smallint will be used. yii\db\SchemaBuilderTrait
tinyText() Creates a tinytext column for MySQL, or text column for others. craft\db\Migration
trigger() Triggers an event. yii\base\Component
truncateTable() Builds and executes a SQL statement for truncating a DB table. yii\db\Migration
uid() Shortcut for creating a uid column craft\db\Migration
up() This method contains the logic to be executed when applying this migration. craft\db\Migration
update() Creates and executes an UPDATE SQL statement. craft\db\Migration
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. craft\db\Migration
Protected Methods
Method Description Defined By
beginCommand() Prepares for a command to be executed, and outputs to the console. yii\db\Migration
endCommand() Finalizes after the command has been executed, and outputs to the console the time elapsed. yii\db\Migration
getDb() yii\db\SchemaBuilderTrait

Method Details

addForeignKey() public method #

Builds a SQL statement for adding a foreign key constraint to an existing table.

The method will properly quote the table and column names.

public void addForeignKey ( $name, $table, $columns, $refTable, $refColumns, $delete null, $update null )
$name string, null The name of the foreign key constraint. If null, a name will be automatically generated.
$table string The table that the foreign key constraint will be added to.
$columns string, array 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 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 The ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
$update string The ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL

addPrimaryKey() public method #

Builds and executes a SQL statement for creating a primary key.

The method will properly quote the table and column names.

public void addPrimaryKey ( $name, $table, $columns )
$name string, null The name of the primary key constraint. If null, a name will be automatically generated.
$table string The table that the primary key constraint will be added to.
$columns string, array Comma separated string or array of columns that the primary key will consist of.

batchInsert() public method #

Creates and executes an batch INSERT SQL statement.

The method will properly escape the column names, and bind the values to be inserted.

public void batchInsert ( $table, $columns, $rows, $includeAuditColumns true )
$table string The table that new rows will be inserted into.
$columns array The column names.
$rows array The rows to be batch inserted into the table.
$includeAuditColumns boolean Whether dateCreated, dateUpdated, and uid values should be added to $columns.

createIndex() public method #

Builds and executes a SQL statement for creating a new index.

public void createIndex ( $name, $table, $columns, $unique false )
$name string, null The name of the index. The name will be properly quoted by the method. If null, a name will be automatically generated.
$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 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 boolean Whether to add UNIQUE constraint on the created index.

down() public method #

This method contains the logic to be executed when removing this migration.

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.

public false, null down ( \craft\db\bool $throwExceptions false )
$throwExceptions boolean Whether exceptions should be thrown
throws Throwable

dropTableIfExists() public method #

Creates and executes a SQL statement for dropping a DB table, if it exists.

public void dropTableIfExists ( \craft\db\string $table )
$table string The table to be dropped. The name will be properly quoted by the method.

enum() public method #

Creates an enum column for MySQL and PostgreSQL, or a string column with a check constraint for others.

public yii\db\ColumnSchemaBuilder enum ( \craft\db\string $columnName, array $values )
$columnName string The column name
$values string[] The allowed column values
return yii\db\ColumnSchemaBuilder The column instance which can be further customized.

insert() public method #

Creates and executes an INSERT SQL statement.

The method will properly escape the column names, and bind the values to be inserted.

public void insert ( $table, $columns, $includeAuditColumns true )
$table string The table that new rows will be inserted into.
$columns array The column data (name=>value) to be inserted into the table.
$includeAuditColumns boolean Whether to include the data for the audit columns (dateCreated, dateUpdated, uid).

longText() public method #

Creates a longtext column for MySQL, or text column for others.

public yii\db\ColumnSchemaBuilder longText ( )
return yii\db\ColumnSchemaBuilder The column instance which can be further customized.

mediumText() public method #

Creates a mediumtext column for MySQL, or text column for others.

public yii\db\ColumnSchemaBuilder mediumText ( )
return yii\db\ColumnSchemaBuilder The column instance which can be further customized.

renameSequence() public method #

Creates and executes a SQL statement for renaming a DB sequence.

public void renameSequence ( \craft\db\string $oldName, \craft\db\string $newName )
$oldName string The sequence to be renamed. The name will be properly quoted by the method.
$newName string The new sequence name. The name will be properly quoted by the method.

replace() public method #

Creates and executes a SQL statement for replacing some text with other text in a given table column.

public void replace ( \craft\db\string $table, \craft\db\string $column, \craft\db\string $find, \craft\db\string $replace, $condition '', array $params = [] )
$table string The table to be updated.
$column string The column to be searched.
$find string The text to be searched for.
$replace string The replacement text.
$condition string, array The condition that will be put in the WHERE part. Please refer to craft\db\Query::where() on how to specify condition.
$params array The parameters to be bound to the command.

tinyText() public method #

Creates a tinytext column for MySQL, or text column for others.

public yii\db\ColumnSchemaBuilder tinyText ( )
return yii\db\ColumnSchemaBuilder The column instance which can be further customized.

uid() public method #

Shortcut for creating a uid column

public yii\db\ColumnSchemaBuilder uid ( )
return yii\db\ColumnSchemaBuilder The column instance which can be further customized.

up() public method #

This method contains the logic to be executed when applying this migration.

Child classes may override this method to provide actual migration logic.

public false, null up ( \craft\db\bool $throwExceptions false )
$throwExceptions boolean Whether exceptions should be thrown
throws Throwable

update() public method #

Creates and executes an UPDATE SQL statement.

The method will properly escape the column names and bind the values to be updated.

public void update ( $table, $columns, $condition '', $params = [], $includeAuditColumns true )
$table string The table to be updated.
$columns array The column data (name => value) to be updated.
$condition string, array The condition that will be put in the WHERE part. Please refer to craft\db\Query::where() on how to specify condition.
$params array The parameters to be bound to the command.
$includeAuditColumns boolean Whether the dateUpdated value should be added to $columns.

upsert() public method (available since version 2.0.14) #

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.

The method will properly escape the column names, and bind the values to be inserted.

public $this upsert ( $table, $insertColumns, $updateColumns true, $params = [], \craft\db\bool $includeAuditColumns true )
$table string The table that new rows will be inserted into/updated in.
$insertColumns array, craft\db\Query The column data (name => value) to be inserted into the table or instance of craft\db\Query to perform INSERT INTO ... SELECT SQL statement.
$updateColumns array, boolean 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. If false is passed, no update will be performed if the column data already exists.
$params array The parameters to be bound to the command.
$includeAuditColumns boolean Whether dateCreated, dateUpdated, and uid values should be added to $columns.
return $this The command object itself.