HumHub Documentation (unofficial)

Migration extends Migration
in package

Migration is the base class for representing a database migration.

Tags
see
Migration

Table of Contents

Constants

LOG_CATEGORY  = 'migration'

Properties

$behaviors  : array<string|int, Behavior>
$compact  : bool
$db  : Connection|array<string|int, mixed>|string
$maxSqlOutputLength  : int
$driverName  : string
$lastException  : Throwable|null
$table  : string
$_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.
getLastException()  : Throwable|null
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()  : void
Initializes static::$driverName
insert()  : mixed
Creates and executes an INSERT SQL statement.
insertSilent()  : void
Creates and executes an INSERT SQL statement without any output The method will properly escape the column names, and bind the values to be inserted.
integer()  : ColumnSchemaBuilder
Creates an integer column.
integerReferenceKey()  : ColumnSchemaBuilder
Returns the field configuration for a FK field
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.
safeAddForeignKeyCreatedBy()  : bool
Add a foreign key constraint to the user table on the `created_by` field.
safeAddForeignKeyToUserTable()  : bool
Add a foreign key constraint to the user table on the field indicated.
safeAddForeignKeyUpdatedBy()  : bool
Add a foreign key constraint to the user table on the `updated_by` field.
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.
timestampWithoutAutoUpdate()  : ColumnSchemaBuilder
Returns the field configuration for a timestamp field that does not get automatically updated by mysql in case it being the first timestamp column in the table.
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.
updateSilent()  : void
Creates and executes an UPDATE SQL statement without any output.
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.
columnExists()  : bool
Check if the column already exists in the table
endCommand()  : mixed
Finalizes after the command has been executed, and outputs to the console the time elapsed.
foreignIndexExists()  : bool
Check if the foreign index already exists in the table
getDb()  : mixed
{@inheritdoc}
indexExists()  : bool
Check if the index already exists in the table
isInitialInstallation()  : bool
Returns whether this is a completely new installation with an empty database (installation process).
logDebug()  : void
logError()  : void
logException()  : void
Get data from database dsn config
logInfo()  : void
logTranslation()  : void
Translate log messages
logWarning()  : void
renameClass()  : void
Renames a class
safeAddColumn()  : bool
safeAddForeignKey()  : bool
Add a foreign index if it doesn't exist yet
safeAddPrimaryKey()  : bool
Add a primary index if it doesn't exist yet
safeCreateIndex()  : bool
Create an index if it doesn't exist yet
safeCreateTable()  : bool
safeDropColumn()  : bool
safeDropForeignKey()  : bool
Drop a foreign key if it exists in the table
safeDropIndex()  : bool
Drop an index if it exists in the table
safeDropPrimaryKey()  : bool
Drop a primary index if it exists in the table
safeDropTable()  : bool
saveUpDown()  : bool|null
Helper function for self::up() and self::down()
attachBehaviorInternal()  : Behavior
Attaches a behavior to this component.
getDsnAttribute()  : string|null
Get data from database dsn config
printException()  : void
Required, since parent is private .

Constants

LOG_CATEGORY

public mixed LOG_CATEGORY = 'migration'

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
since
2.0.13

$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
since
2.0.13

$driverName

protected string $driverName

Name of the current database driver. Initialized during static::init().

Tags
see
static::timestampWithoutAutoUpdate()

$lastException

protected Throwable|null $lastException = null

Exception that occurred during migration

$table

protected string $table

Main table of the current migration. MUST be overridden statically or initialized during static::__construct() or static::init()

Tags
see
static::safeAddForeignKeyToUserTable()

$_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
since
2.0.14

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
throws
UnknownMethodException

when calling unknown method

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
throws
UnknownPropertyException

if the property is not defined

throws
InvalidCallException

if the property is write-only.

see
__set()
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
see
https://www.php.net/manual/en/function.isset.php
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
throws
UnknownPropertyException

if the property is not defined

throws
InvalidCallException

if the property is read-only.

see
__get()

__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
throws
InvalidCallException

if the property is read only.

see
https://www.php.net/manual/en/function.unset.php

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
since
2.0.8

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
since
2.0.8

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
see
detachBehavior()
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
see
attachBehavior()

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
since
2.0.6
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
since
2.0.6
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
since
2.0.6
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
see
canSetProperty()
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
see
canGetProperty()
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
since
2.0.8
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
deprecated

since 2.0.14. On PHP >=5.5, use ::class instead.

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.

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
since
2.0.6
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
since
2.0.6
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
since
2.0.6
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
Tags
inheritdoc
since
1.15.0
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
since
2.0.8

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
since
2.0.8

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
since
2.0.6
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

getLastException()

public getLastException() : Throwable|null
Return values
Throwable|null

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
see
canGetProperty()
see
canSetProperty()
Return values
bool

whether the property is defined

init()

Initializes static::$driverName

public init() : void
Tags
since
1.15
noinspection

ReturnTypeCanBeDeclaredInspection

see
static::$driverName

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.

insertSilent()

Creates and executes an INSERT SQL statement without any output The method will properly escape the column names, and bind the values to be inserted.

public insertSilent(string $table, array<string|int, mixed>|Traversable $columns) : void
Parameters
$table : string

the table that new rows will be inserted into.

$columns : array<string|int, mixed>|Traversable

the column data (name => value) to be inserted into the table.

Tags
throws
Exception

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
since
2.0.6
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
since
2.0.6
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
see
on()
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
see
off()

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
since
2.0.6
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.

safeAddForeignKeyCreatedBy()

Add a foreign key constraint to the user table on the `created_by` field.

public safeAddForeignKeyCreatedBy() : bool
Tags
throws
Exception
see
static::safeAddForeignKeyToUserTable()
since
1.15
noinspection

PhpUnused

noinspection

PhpMissingReturnTypeInspection

Return values
bool

indicates if key has been added

safeAddForeignKeyToUserTable()

Add a foreign key constraint to the user table on the field indicated.

public safeAddForeignKeyToUserTable(string $sourceField) : bool
Parameters
$sourceField : string

Source field referencing user.id

Tags
throws
Exception
see
static::$table
since
1.15
noinspection

PhpMissingReturnTypeInspection

Return values
bool

indicates if key has been added

safeAddForeignKeyUpdatedBy()

Add a foreign key constraint to the user table on the `updated_by` field.

public safeAddForeignKeyUpdatedBy() : bool
Tags
throws
Exception
see
static::safeAddForeignKeyToUserTable()
since
1.15
noinspection

PhpUnused

noinspection

PhpMissingReturnTypeInspection

Return values
bool

indicates if key has been added

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
since
2.0.6
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
since
2.0.6
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
since
2.0.6
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
since
2.0.6
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
since
2.0.14
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
Tags
inheritdoc
since
1.15.0
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.

updateSilent()

Creates and executes an UPDATE SQL statement without any output.

public updateSilent(string $table, array<string|int, mixed>|Traversable $columns[, array<string|int, mixed>|string $condition = '' ][, array<string|int, mixed>|Traversable $params = [] ]) : void

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>|Traversable

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>|Traversable = []

the parameters to be bound to the query.

Tags
throws
Exception

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. If false 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
since
2.0.14

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
since
2.0.13
Return values
float

the time before the command is executed, for the time elapsed to be calculated.

columnExists()

Check if the column already exists in the table

protected columnExists(string $column, string $table) : bool
Parameters
$column : string
$table : string
Tags
since
1.9.1
Return values
bool

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
since
2.0.13

foreignIndexExists()

Check if the foreign index already exists in the table

protected foreignIndexExists(string $index, string $table) : bool
Parameters
$index : string
$table : string
Tags
throws
Exception
since
1.9.1
Return values
bool

getDb()

{@inheritdoc}

protected getDb() : mixed
Tags
since
2.0.6

indexExists()

Check if the index already exists in the table

protected indexExists(string $index, string $table) : bool
Parameters
$index : string
$table : string
Tags
throws
Exception
since
1.9.1
Return values
bool

isInitialInstallation()

Returns whether this is a completely new installation with an empty database (installation process).

protected isInitialInstallation() : bool
Tags
since
1.8
Return values
bool

logDebug()

protected logDebug(string $message[, array<string|int, mixed> $params = [] ]) : void
Parameters
$message : string

Message to be logged

$params : array<string|int, mixed> = []

Parameters to translate in $message

Tags
since
1.15.0

logError()

protected logError(string $message[, array<string|int, mixed> $params = [] ]) : void
Parameters
$message : string

Message to be logged

$params : array<string|int, mixed> = []

Parameters to translate in $message

Tags
since
1.15.0

logException()

Get data from database dsn config

protected logException(Throwable $e, string $method) : void
Parameters
$e : Throwable

The Throwable to be logged

$method : string

The Method that was running

Tags
since
1.15.0

logInfo()

protected logInfo(string $message[, array<string|int, mixed> $params = [] ]) : void
Parameters
$message : string

Message to be logged

$params : array<string|int, mixed> = []

Parameters to translate in $message

Tags
since
1.15.0
noinspection

PhpUnused

logTranslation()

Translate log messages

protected logTranslation(string $message[, array<string|int, mixed> $params = [] ]) : void
Parameters
$message : string

Message to be logged

$params : array<string|int, mixed> = []

Parameters to translate in $message

Tags
since
1.15.0

logWarning()

protected logWarning(string $message[, array<string|int, mixed> $params = [] ]) : void
Parameters
$message : string

Message to be logged

$params : array<string|int, mixed> = []

Parameters to translate in $message

Tags
since
1.15.0

renameClass()

Renames a class

protected renameClass(string $oldClass, string $newClass) : void

This is often required because some classes are also stored in the database, e.g. for polymorphic relations.

This method is also required for 0.20 namespace migration!

Parameters
$oldClass : string
$newClass : string
Tags
throws
Exception

safeAddColumn()

protected safeAddColumn(string $table, string $column, string|ColumnSchemaBuilder $type) : bool
Parameters
$table : string

table name

$column : string

column name

$type : string|ColumnSchemaBuilder

column type

Tags
see
static::addColumn()
noinspection

PhpMissingReturnTypeInspection

Return values
bool

indicates if column has been added

safeAddForeignKey()

Add a foreign index if it doesn't exist yet

protected safeAddForeignKey(string $index, string $table, string|array<string|int, mixed> $columns, string $refTable, string|array<string|int, mixed> $refColumns[, string|null $delete = null ][, string|null $update = null ]) : bool
Parameters
$index : string
$table : string
$columns : string|array<string|int, mixed>
$refTable : string
$refColumns : string|array<string|int, mixed>
$delete : string|null = null
$update : string|null = null
Tags
throws
Exception
since
1.9.1
see
static::addForeignKey()
noinspection

PhpMissingReturnTypeInspection

Return values
bool

indicates if key has been added

safeAddPrimaryKey()

Add a primary index if it doesn't exist yet

protected safeAddPrimaryKey(string $index, string $table, string|array<string|int, mixed> $columns) : bool
Parameters
$index : string
$table : string
$columns : string|array<string|int, mixed>
Tags
throws
Exception
since
1.9.1
see
static::addPrimaryKey()
noinspection

PhpMissingReturnTypeInspection

Return values
bool

indicates if key has been added

safeCreateIndex()

Create an index if it doesn't exist yet

protected safeCreateIndex(string $index, string $table, string|array<string|int, mixed> $columns[, bool $unique = false ]) : bool
Parameters
$index : string
$table : string
$columns : string|array<string|int, mixed>
$unique : bool = false
Tags
throws
Exception
since
1.9.1
see
static::createIndex()
noinspection

PhpMissingReturnTypeInspection

Return values
bool

indicates if the index has been created

safeCreateTable()

protected safeCreateTable(string $table, mixed $columns[, string|null $options = null ]) : bool
Parameters
$table : string
$columns : mixed
$options : string|null = null
Tags
see
static::createTable()
noinspection

PhpMissingReturnTypeInspection

Return values
bool

indicates if the table has been created

safeDropColumn()

protected safeDropColumn(string $table, string $column) : bool
Parameters
$table : string
$column : string
Tags
noinspection

PhpMissingReturnTypeInspection

Return values
bool

indicates if column has been dropped

safeDropForeignKey()

Drop a foreign key if it exists in the table

protected safeDropForeignKey(string $index, string $table) : bool
Parameters
$index : string
$table : string
Tags
throws
Exception
since
1.9.1
noinspection

PhpUnused

noinspection

PhpMissingReturnTypeInspection

Return values
bool

indicates if key has been dropped

safeDropIndex()

Drop an index if it exists in the table

protected safeDropIndex(string $index, string $table) : bool
Parameters
$index : string
$table : string
Tags
throws
Exception
since
1.9.1
see
static::dropIndex()
noinspection

PhpUnused

noinspection

PhpMissingReturnTypeInspection

Return values
bool

indicates if the index has been dropped

safeDropPrimaryKey()

Drop a primary index if it exists in the table

protected safeDropPrimaryKey(string $index, string $table) : bool
Parameters
$index : string
$table : string
Tags
throws
Exception
since
1.9.1
see
static::dropPrimaryKey()
noinspection

PhpUnused

noinspection

PhpMissingReturnTypeInspection

Return values
bool

indicates if key has been added

safeDropTable()

protected safeDropTable(string $table) : bool
Parameters
$table : string
Tags
see
static::dropTable()
noinspection

PhpUnused

noinspection

PhpMissingReturnTypeInspection

Return values
bool

indicates if the table has been dropped

saveUpDown()

Helper function for self::up() and self::down()

protected saveUpDown(array<string|int, mixed> $action) : bool|null
Parameters
$action : array<string|int, mixed>
Tags
since
1.15.0
Return values
bool|null

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.

getDsnAttribute()

Get data from database dsn config

private getDsnAttribute(string $name) : string|null
Parameters
$name : string

'host', 'port', 'dbname'

Tags
since
1.9.3
Return values
string|null

printException()

Required, since parent is private .

private printException(Throwable $t) : void

..

Parameters
$t : Throwable

        
On this page

Search results