system system.base system.caching system.caching.dependencies system.collections system.console system.db system.db.ar system.db.schema system.db.schema.cubrid system.db.schema.mssql system.db.schema.mysql system.db.schema.oci system.db.schema.pgsql system.db.schema.sqlite system.gii system.i18n system.i18n.gettext system.logging system.test system.utils system.validators system.web system.web.actions system.web.auth system.web.filters system.web.form system.web.helpers system.web.renderers system.web.services system.web.widgets system.web.widgets.captcha system.web.widgets.pagers zii.behaviors zii.widgets zii.widgets.grid zii.widgets.jui

CDbMigration

system.db
继承 abstract class CDbMigration » CComponent
可用自 1.1.6
源码 framework/db/CDbMigration.php
CDbMigration is the base class for representing a database migration.

CDbMigration is designed to be used together with the "yiic migrate" command.

Each child class of CDbMigration represents an individual database migration which is identified by the child class name.

Within each migration, the up method contains the logic for "upgrading" the database used in an application; while the down method contains "downgrading" logic. The "yiic migrate" command manages all available migrations in an application.

By overriding safeUp or safeDown methods instead of up and down the migration logic will be wrapped with a DB transaction.

CDbMigration 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 CDbCommand, these methods will display extra information showing the method parameters and execution time, which may be useful when applying migrations.

公共属性

隐藏继承的属性

属性类型描述被定义在
dbConnection CDbConnection Returns the currently active database connection. CDbMigration

公共方法

隐藏继承的方法

方法描述被定义在
__call() Calls the named method which is not a class method. CComponent
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
addColumn() Builds and executes a SQL statement for adding a new DB column. CDbMigration
addForeignKey() Builds a SQL statement for adding a foreign key constraint to an existing table. CDbMigration
addPrimaryKey() Builds and executes a SQL statement for creating a primary key, supports composite primary keys. CDbMigration
alterColumn() Builds and executes a SQL statement for changing the definition of a column. CDbMigration
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
createIndex() Builds and executes a SQL statement for creating a new index. CDbMigration
createTable() Builds and executes a SQL statement for creating a new DB table. CDbMigration
delete() Creates and executes a DELETE SQL statement. CDbMigration
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
down() This method contains the logic to be executed when removing this migration. CDbMigration
dropColumn() Builds and executes a SQL statement for dropping a DB column. CDbMigration
dropForeignKey() Builds a SQL statement for dropping a foreign key constraint. CDbMigration
dropIndex() Builds and executes a SQL statement for dropping an index. CDbMigration
dropPrimaryKey() Builds and executes a SQL statement for removing a primary key, supports composite primary keys. CDbMigration
dropTable() Builds and executes a SQL statement for dropping a DB table. CDbMigration
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
evaluateExpression() Evaluates a PHP expression or callback under the context of this component. CComponent
execute() Executes a SQL statement. CDbMigration
getDbConnection() Returns the currently active database connection. CDbMigration
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
insert() Creates and executes an INSERT SQL statement. CDbMigration
insertMultiple() Creates and executes an INSERT SQL statement with multiple data. CDbMigration
raiseEvent() Raises an event. CComponent
refreshTableSchema() Refreshed schema cache for a table CDbMigration
renameColumn() Builds and executes a SQL statement for renaming a column. CDbMigration
renameTable() Builds and executes a SQL statement for renaming a DB table. CDbMigration
safeDown() This method contains the logic to be executed when removing this migration. CDbMigration
safeUp() This method contains the logic to be executed when applying this migration. CDbMigration
setDbConnection() Sets the currently active database connection. CDbMigration
truncateTable() Builds and executes a SQL statement for truncating a DB table. CDbMigration
up() This method contains the logic to be executed when applying this migration. CDbMigration
update() Creates and executes an UPDATE SQL statement. CDbMigration

属性详情

dbConnection 属性

Returns the currently active database connection. By default, the 'db' application component will be returned and activated. You can call setDbConnection to switch to a different database connection. Methods such as insert, createTable will use this database connection to perform DB queries.

方法详情

addColumn() 方法
public void addColumn(string $table, string $column, string $type)
$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 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'.
源码: framework/db/CDbMigration.php#299 (显示)
public function addColumn($table$column$type)
{
    echo 
"    > add column $column $type to table $table ...";
    
$time=microtime(true);
    
$this->getDbConnection()->createCommand()->addColumn($table$column$type);
    echo 
" done (time: ".sprintf('%.3f'microtime(true)-$time)."s)\n";
}

Builds and executes a SQL statement for adding a new DB column.

addForeignKey() 方法
public void addForeignKey(string $name, string $table, string|array $columns, string $refTable, string|array $refColumns, string $delete=NULL, string $update=NULL)
$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 the name of the column to that the constraint will be added on. If there are multiple columns, separate them with commas or pass as an array of column names.
$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 pass as an array of column names.
$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
源码: framework/db/CDbMigration.php#361 (显示)
public function addForeignKey($name$table$columns$refTable$refColumns$delete=null$update=null)
{
    echo 
"    > add foreign key $name$table (".(is_array($columns) ? implode(','$columns) : $columns).
         
") references $refTable (".(is_array($refColumns) ? implode(','$refColumns) : $refColumns).") ...";
    
$time=microtime(true);
    
$this->getDbConnection()->createCommand()->addForeignKey($name$table$columns$refTable$refColumns$delete$update);
    echo 
" done (time: ".sprintf('%.3f'microtime(true)-$time)."s)\n";
}

Builds a SQL statement for adding a foreign key constraint to an existing table. The method will properly quote the table and column names.

addPrimaryKey() 方法 (自版本 v1.1.13 可用)
public void addPrimaryKey(string $name, string $table, string|array $columns)
$name string name of the primary key constraint to add
$table string name of the table to add primary key to
$columns string|array comma separated string or array of columns that the primary key will consist of. Array value can be passed since 1.1.14.
源码: framework/db/CDbMigration.php#433 (显示)
public function addPrimaryKey($name,$table,$columns)
{
    echo 
"    > alter table $table add constraint $name primary key (".(is_array($columns) ? implode(','$columns) : $columns).") ...";
    
$time=microtime(true);
    
$this->getDbConnection()->createCommand()->addPrimaryKey($name,$table,$columns);
    echo 
" done (time: ".sprintf('%.3f'microtime(true)-$time)."s)\n";
}

Builds and executes a SQL statement for creating a primary key, supports composite primary keys.

alterColumn() 方法
public void alterColumn(string $table, string $column, string $type)
$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 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'.
源码: framework/db/CDbMigration.php#342 (显示)
public function alterColumn($table$column$type)
{
    echo 
"    > alter column $column in table $table to $type ...";
    
$time=microtime(true);
    
$this->getDbConnection()->createCommand()->alterColumn($table$column$type);
    echo 
" done (time: ".sprintf('%.3f'microtime(true)-$time)."s)\n";
}

Builds and executes a SQL statement for changing the definition of a column.

createIndex() 方法
public void createIndex(string $name, string $table, string|array $columns, boolean $unique=false)
$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 the column(s) that should be included in the index. If there are multiple columns, please separate them by commas or pass as an array of column names. Each column name will be properly quoted by the method, unless a parenthesis is found in the name.
$unique boolean whether to add UNIQUE constraint on the created index.
源码: framework/db/CDbMigration.php#391 (显示)
public function createIndex($name$table$columns$unique=false)
{
    echo 
"    > create".($unique ' unique':'')." index $name on $table (".(is_array($columns) ? implode(','$columns) : $columns).") ...";
    
$time=microtime(true);
    
$this->getDbConnection()->createCommand()->createIndex($name$table$columns$unique);
    echo 
" done (time: ".sprintf('%.3f'microtime(true)-$time)."s)\n";
}

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

createTable() 方法
public void createTable(string $table, array $columns, string $options=NULL)
$table string the name of the table to be created. The name will be properly quoted by the method.
$columns array the columns (name=>definition) in the new table.
$options string additional SQL fragment that will be appended to the generated SQL.
源码: framework/db/CDbMigration.php#246 (显示)
public function createTable($table$columns$options=null)
{
    echo 
"    > create table $table ...";
    
$time=microtime(true);
    
$this->getDbConnection()->createCommand()->createTable($table$columns$options);
    echo 
" done (time: ".sprintf('%.3f'microtime(true)-$time)."s)\n";
}

Builds and executes a SQL statement for creating a new DB table.

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 can contain an abstract DB type. The 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 inserted into the generated SQL.

delete() 方法
public void delete(string $table, mixed $conditions='', array $params=array ( ))
$table string the table where the data will be deleted from.
$conditions mixed the conditions that will be put in the WHERE part. Please refer to CDbCommand::where on how to specify conditions.
$params array the parameters to be bound to the query.
源码: framework/db/CDbMigration.php#223 (显示)
public function delete($table$conditions=''$params=array())
{
    echo 
"    > delete from $table ...";
    
$time=microtime(true);
    
$this->getDbConnection()->createCommand()->delete($table$conditions$params);
    echo 
" done (time: ".sprintf('%.3f'microtime(true)-$time)."s)\n";
}

Creates and executes a DELETE SQL statement.

down() 方法
public boolean down()
{return} boolean Returning false means, the migration will not be applied.
源码: framework/db/CDbMigration.php#74 (显示)
public function down()
{
    
$transaction=$this->getDbConnection()->beginTransaction();
    try
    {
        if(
$this->safeDown()===false)
        {
            
$transaction->rollback();
            return 
false;
        }
        
$transaction->commit();
    }
    catch(
Exception $e)
    {
        echo 
"Exception: ".$e->getMessage().' ('.$e->getFile().':'.$e->getLine().")\n";
        echo 
$e->getTraceAsString()."\n";
        
$transaction->rollback();
        return 
false;
    }
}

This method contains the logic to be executed when removing this migration. Child classes may override this method if the corresponding migrations can be removed.

dropColumn() 方法
public void dropColumn(string $table, string $column)
$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.
源码: framework/db/CDbMigration.php#312 (显示)
public function dropColumn($table$column)
{
    echo 
"    > drop column $column from table $table ...";
    
$time=microtime(true);
    
$this->getDbConnection()->createCommand()->dropColumn($table$column);
    echo 
" done (time: ".sprintf('%.3f'microtime(true)-$time)."s)\n";
}

Builds and executes a SQL statement for dropping a DB column.

dropForeignKey() 方法
public void dropForeignKey(string $name, string $table)
$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.
源码: framework/db/CDbMigration.php#375 (显示)
public function dropForeignKey($name$table)
{
    echo 
"    > drop foreign key $name from table $table ...";
    
$time=microtime(true);
    
$this->getDbConnection()->createCommand()->dropForeignKey($name$table);
    echo 
" done (time: ".sprintf('%.3f'microtime(true)-$time)."s)\n";
}

Builds a SQL statement for dropping a foreign key constraint.

dropIndex() 方法
public void dropIndex(string $name, string $table)
$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.
源码: framework/db/CDbMigration.php#404 (显示)
public function dropIndex($name$table)
{
    echo 
"    > drop index $name ...";
    
$time=microtime(true);
    
$this->getDbConnection()->createCommand()->dropIndex($name$table);
    echo 
" done (time: ".sprintf('%.3f'microtime(true)-$time)."s)\n";
}

Builds and executes a SQL statement for dropping an index.

dropPrimaryKey() 方法 (自版本 v1.1.13 可用)
public void dropPrimaryKey(string $name, string $table)
$name string name of the constraint to remove
$table string name of the table to remove primary key from
源码: framework/db/CDbMigration.php#447 (显示)
public function dropPrimaryKey($name,$table)
{
    echo 
"    > alter table $table drop primary key $name ...";
    
$time=microtime(true);
    
$this->getDbConnection()->createCommand()->dropPrimaryKey($name,$table);
    echo 
" done (time: ".sprintf('%.3f'microtime(true)-$time)."s)\n";
}

Builds and executes a SQL statement for removing a primary key, supports composite primary keys.

dropTable() 方法
public void dropTable(string $table)
$table string the table to be dropped. The name will be properly quoted by the method.
源码: framework/db/CDbMigration.php#271 (显示)
public function dropTable($table)
{
    echo 
"    > drop table $table ...";
    
$time=microtime(true);
    
$this->getDbConnection()->createCommand()->dropTable($table);
    echo 
" done (time: ".sprintf('%.3f'microtime(true)-$time)."s)\n";
}

Builds and executes a SQL statement for dropping a DB table.

execute() 方法 (自版本 v1.1.7 可用)
public void execute(string $sql, array $params=array ( ))
$sql string the SQL statement to be executed
$params array input parameters (name=>value) for the SQL execution. See CDbCommand::execute for more details.
源码: framework/db/CDbMigration.php#160 (显示)
public function execute($sql$params=array())
{
    echo 
"    > execute SQL: $sql ...";
    
$time=microtime(true);
    
$this->getDbConnection()->createCommand($sql)->execute($params);
    echo 
" done (time: ".sprintf('%.3f'microtime(true)-$time)."s)\n";
}

Executes a SQL statement. This method executes the specified SQL statement using dbConnection.

getDbConnection() 方法
public CDbConnection getDbConnection()
{return} CDbConnection the currently active database connection
源码: framework/db/CDbMigration.php#132 (显示)
public function getDbConnection()
{
    if(
$this->_db===null)
    {
        
$this->_db=Yii::app()->getComponent('db');
        if(!
$this->_db instanceof CDbConnection)
            throw new 
CException(Yii::t('yii''The "db" application component must be configured to be a CDbConnection object.'));
    }
    return 
$this->_db;
}

Returns the currently active database connection. By default, the 'db' application component will be returned and activated. You can call setDbConnection to switch to a different database connection. Methods such as insert, createTable will use this database connection to perform DB queries.

insert() 方法
public void insert(string $table, array $columns)
$table string the table that new rows will be inserted into.
$columns array the column data (name=>value) to be inserted into the table.
源码: framework/db/CDbMigration.php#174 (显示)
public function insert($table$columns)
{
    echo 
"    > insert into $table ...";
    
$time=microtime(true);
    
$this->getDbConnection()->createCommand()->insert($table$columns);
    echo 
" done (time: ".sprintf('%.3f'microtime(true)-$time)."s)\n";
}

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

insertMultiple() 方法 (自版本 v1.1.16 可用)
public void insertMultiple(string $table, array $data)
$table string the table that new rows will be inserted into.
$data array an array of various column data (name=>value) to be inserted into the table.
源码: framework/db/CDbMigration.php#189 (显示)
public function insertMultiple($table$data)
{
    echo 
"    > insert into $table ...";
    
$time=microtime(true);
    
$builder=$this->getDbConnection()->getSchema()->getCommandBuilder();
    
$command=$builder->createMultipleInsertCommand($table,$data);
    
$command->execute();
    echo 
" done (time: ".sprintf('%.3f'microtime(true)-$time)."s)\n";
}

Creates and executes an INSERT SQL statement with multiple data. The method will properly escape the column names, and bind the values to be inserted.

refreshTableSchema() 方法 (自版本 v1.1.9 可用)
public void refreshTableSchema(string $table)
$table string name of the table to refresh
源码: framework/db/CDbMigration.php#417 (显示)
public function refreshTableSchema($table)
{
    echo 
"    > refresh table $table schema cache ...";
    
$time=microtime(true);
    
$this->getDbConnection()->getSchema()->getTable($table,true);
    echo 
" done (time: ".sprintf('%.3f'microtime(true)-$time)."s)\n";
}

Refreshed schema cache for a table

renameColumn() 方法
public void renameColumn(string $table, string $name, string $newName)
$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.
源码: framework/db/CDbMigration.php#326 (显示)
public function renameColumn($table$name$newName)
{
    echo 
"    > rename column $name in table $table to $newName ...";
    
$time=microtime(true);
    
$this->getDbConnection()->createCommand()->renameColumn($table$name$newName);
    echo 
" done (time: ".sprintf('%.3f'microtime(true)-$time)."s)\n";
}

Builds and executes a SQL statement for renaming a column.

renameTable() 方法
public void renameTable(string $table, string $newName)
$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.
源码: framework/db/CDbMigration.php#259 (显示)
public function renameTable($table$newName)
{
    echo 
"    > rename table $table to $newName ...";
    
$time=microtime(true);
    
$this->getDbConnection()->createCommand()->renameTable($table$newName);
    echo 
" done (time: ".sprintf('%.3f'microtime(true)-$time)."s)\n";
}

Builds and executes a SQL statement for renaming a DB table.

safeDown() 方法 (自版本 v1.1.7 可用)
public boolean safeDown()
{return} boolean Returning false means, the migration will not be applied and the transaction will be rolled back.
源码: framework/db/CDbMigration.php#119 (显示)
public function safeDown()
{
}

This method contains the logic to be executed when removing this migration. 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 up if the DB logic needs to be within a transaction.

safeUp() 方法 (自版本 v1.1.7 可用)
public boolean safeUp()
{return} boolean Returning false means, the migration will not be applied and the transaction will be rolled back.
源码: framework/db/CDbMigration.php#105 (显示)
public function safeUp()
{
}

This method contains the logic to be executed when applying this migration. 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.

setDbConnection() 方法
public void setDbConnection(CDbConnection $db)
$db CDbConnection the database connection component
源码: framework/db/CDbMigration.php#148 (显示)
public function setDbConnection($db)
{
    
$this->_db=$db;
}

Sets the currently active database connection. The database connection will be used by the methods such as insert, createTable.

truncateTable() 方法
public void truncateTable(string $table)
$table string the table to be truncated. The name will be properly quoted by the method.
源码: framework/db/CDbMigration.php#283 (显示)
public function truncateTable($table)
{
    echo 
"    > truncate table $table ...";
    
$time=microtime(true);
    
$this->getDbConnection()->createCommand()->truncateTable($table);
    echo 
" done (time: ".sprintf('%.3f'microtime(true)-$time)."s)\n";
}

Builds and executes a SQL statement for truncating a DB table.

up() 方法
public boolean up()
{return} boolean Returning false means, the migration will not be applied.
源码: framework/db/CDbMigration.php#48 (显示)
public function up()
{
    
$transaction=$this->getDbConnection()->beginTransaction();
    try
    {
        if(
$this->safeUp()===false)
        {
            
$transaction->rollback();
            return 
false;
        }
        
$transaction->commit();
    }
    catch(
Exception $e)
    {
        echo 
"Exception: ".$e->getMessage().' ('.$e->getFile().':'.$e->getLine().")\n";
        echo 
$e->getTraceAsString()."\n";
        
$transaction->rollback();
        return 
false;
    }
}

This method contains the logic to be executed when applying this migration. Child classes may implement this method to provide actual migration logic.

update() 方法
public void update(string $table, array $columns, mixed $conditions='', array $params=array ( ))
$table string the table to be updated.
$columns array the column data (name=>value) to be updated.
$conditions mixed the conditions that will be put in the WHERE part. Please refer to CDbCommand::where on how to specify conditions.
$params array the parameters to be bound to the query.
源码: framework/db/CDbMigration.php#208 (显示)
public function update($table$columns$conditions=''$params=array())
{
    echo 
"    > update $table ...";
    
$time=microtime(true);
    
$this->getDbConnection()->createCommand()->update($table$columns$conditions$params);
    echo 
" done (time: ".sprintf('%.3f'microtime(true)-$time)."s)\n";
}

Creates and executes an UPDATE SQL statement. The method will properly escape the column names and bind the values to be updated.