Yii Framework v1.1.10 绫诲弬鑰

CDbMigration

system.db
缁ф壙 abstract class CDbMigration » CComponent
婧愯嚜 1.1.6
鐗堟湰 $Id: CDbMigration.php 3514 2011-12-27 20:28:26Z alexander.makarow $
婧愮爜 framework/db/CDbMigration.php
CDbMigration鏄〃绀轰竴涓暟鎹簱杩佺Щ鐨勫熀绫汇

CDbMigration琚璁′负鍜屸測iic migrate鈥濆懡浠や竴璧蜂娇鐢ㄣ

姣忎竴涓 CDbMigration 鐨勫瓙绫昏〃绀轰竴涓崟涓暟鎹簱杩佺Щ锛 瀹冮氳繃瀛愮被鍚嶇О璇嗗埆銆

鍦ㄦ瘡涓涓縼绉诲唴閮紝 up 鏂规硶鍖呭惈浜 "upgrading"閫昏緫 鍦ㄥ簲鐢ㄧ▼搴忎腑浣跨敤鏁版嵁搴擄紱褰 down 鏂规硶鍖呭惈 "downgrading" 閫昏緫鏃躲 "yiic migrate" 鍛戒护绠$悊鍦ㄤ竴涓簲鐢ㄧ▼搴忎腑鎵鏈夊彲鐢ㄧ殑杩佺Щ銆

CDbMigration鎻愪緵浜嗕竴濂椾究鍒╃殑鏂规硶鏉ユ搷绾垫暟鎹簱鏁版嵁鍜宻chema銆 渚嬪锛insert 鏂规硶鑳藉緢瀹规槗鐨勫湪涓涓暟鎹簱琛ㄤ腑鎻掑叆涓琛屾暟鎹 createTable 鏂规硶鑳界敤浣滃垱寤轰竴涓暟鎹簱銆 鍜CDbCommand涓殑鐩稿悓鏂规硶姣旇緝锛岃繖浜涙柟娉曞皢鏄剧ず闄勫姞淇℃伅, 濡傛柟娉曞弬鏁板拰鎵ц鏃堕棿锛 瀹冧滑鍦ㄥ簲鐢ㄨ縼绉绘椂鏄潪甯告湁鐢ㄧ殑銆

鍏叡灞炴

闅愯棌缁ф壙灞炴

灞炴绫诲瀷鎻忚堪瀹氫箟鍦
dbConnection CDbConnection 杩斿洖褰撳墠娲诲姩鐨勬暟鎹簱杩炴帴銆 CDbMigration

鍏叡鏂规硶

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
__call() 濡傛灉绫讳腑娌℃湁璋冪殑鏂规硶鍚嶏紝鍒欒皟鐢ㄨ繖涓柟娉曘 CComponent
__get() 杩斿洖涓涓睘鎬у笺佷竴涓簨浠跺鐞嗙▼搴忓垪琛ㄦ垨涓涓涓哄悕绉般 CComponent
__isset() 妫鏌ヤ竴涓睘鎬ф槸鍚︿负null銆 CComponent
__set() 璁剧疆涓涓粍浠剁殑灞炴у笺 CComponent
__unset() 璁剧疆涓涓粍浠剁殑灞炴т负null銆 CComponent
addColumn() 寤虹珛骞舵墽琛屼竴鏉QL璇彞涓哄鍔犱竴涓柊鐨勬暟鎹簱鍒椼 CDbMigration
addForeignKey() 寤虹珛涓涓猄QL璇彞娣诲姞鍒扮幇鏈夎〃鐨勪竴涓閿害鏉熴 CDbMigration
alterColumn() 寤虹珛骞舵墽琛屼竴鏉QL璇彞涓烘敼鍙樹竴涓垪鐨勫畾涔夈 CDbMigration
asa() 杩斿洖杩欎釜鍚嶅瓧鐨勮涓哄璞° CComponent
attachBehavior() 闄勫姞涓涓涓哄埌缁勪欢銆 CComponent
attachBehaviors() 闄勫姞涓涓涓哄垪琛ㄥ埌缁勪欢銆 CComponent
attachEventHandler() 涓轰簨浠堕檮鍔犱竴涓簨浠跺鐞嗙▼搴忋 CComponent
canGetProperty() 纭畾灞炴ф槸鍚﹀彲璇汇 CComponent
canSetProperty() 纭畾灞炴ф槸鍚﹀彲鍐欍 CComponent
createIndex() 寤虹珛骞舵墽琛孲QL璇彞涓哄垱寤烘柊鐨勭储寮曘 CDbMigration
createTable() 寤虹珛骞舵墽琛屼竴鏉QL璇彞涓烘彃鍏ヤ竴鏉℃柊鐨勬暟鎹〃銆 CDbMigration
delete() 鍒涘缓鍜屾墽琛屼竴鏉 DELETE SQL 璇彞銆 CDbMigration
detachBehavior() 浠庣粍浠朵腑鍒嗙涓涓涓恒 CComponent
detachBehaviors() 浠庣粍浠朵腑鍒嗙鎵鏈夎涓恒 CComponent
detachEventHandler() 鍒嗙涓涓瓨鍦ㄧ殑浜嬩欢澶勭悊绋嬪簭銆 CComponent
disableBehavior() 绂佺敤涓涓檮鍔犺涓恒 CComponent
disableBehaviors() 绂佺敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
down() 褰撳垹闄ゆ杩佺Щ鏃讹紝姝ゆ柟娉曞寘鍚鎵ц鐨勯昏緫銆 CDbMigration
dropColumn() 鍒涘缓骞舵墽琛屼竴鏉QL璇彞涓哄垹闄や竴涓暟鎹簱鍒椼 CDbMigration
dropForeignKey() 寤虹珛涓鏉QL璇彞涓哄垹闄ゅ閿害鏉熴 CDbMigration
dropIndex() 寤虹珛骞舵墽琛屼竴鏉″垹闄や竴鏉QL绱㈠紩鐨勮鍙ャ CDbMigration
dropTable() 寤虹珛骞舵墽琛屼竴鏉QL璇彞涓哄垹闄や竴涓暟鎹簱琛ㄣ CDbMigration
enableBehavior() 鍚敤涓涓檮鍔犺涓恒 CComponent
enableBehaviors() 鍚敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
evaluateExpression() 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 CComponent
execute() 鎵ц涓鏉QL璇彞銆 CDbMigration
getDbConnection() 杩斿洖褰撳墠娲诲姩鐨勬暟鎹簱杩炴帴銆 CDbMigration
getEventHandlers() 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 CComponent
hasEvent() 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 CComponent
hasEventHandler() 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 CComponent
hasProperty() 纭畾灞炴ф槸鍚﹁瀹氫箟銆 CComponent
insert() 鍒涘缓鍜屾墽琛屼竴鏉 INSERT SQL 璇彞銆 CDbMigration
raiseEvent() 鍙戣捣涓涓簨浠躲 CComponent
refreshTableSchema() 鍒锋柊鏋舵瀯缂撳瓨琛ㄣ CDbMigration
renameColumn() 寤虹珛骞舵墽琛屼竴鏉QL璇彞涓洪噸鍛藉悕涓鍒椼 CDbMigration
renameTable() 寤虹珛骞舵墽琛屼竴鏉QL璇彞涓洪噸鍛藉悕鏁版嵁琛ㄣ CDbMigration
safeDown() 褰撳垹闄ゆ杩佺Щ鏃讹紝姝ゆ柟娉曞寘鍚鎵ц鐨勯昏緫銆 CDbMigration
safeUp() 褰撳簲鐢ㄦ杩佺Щ鏃讹紝姝ゆ柟娉曞寘鍚鎵ц鐨勯昏緫銆 CDbMigration
setDbConnection() 璁剧疆褰撳墠娲诲姩鐨勬暟鎹簱杩炴帴銆 CDbMigration
truncateTable() 寤虹珛骞舵墽琛孲QL璇彞涓烘埅鏂竴涓暟鎹簱琛ㄣ CDbMigration
up() 褰撳簲鐢ㄦ杩佺Щ鏃讹紝姝ゆ柟娉曞寘鍚鎵ц鐨勯昏緫銆 CDbMigration
update() 鍒涘缓鍜屾墽琛屼竴鏉 UPDATE SQL 璇彞銆 CDbMigration

灞炴ц缁

dbConnection 灞炴

杩斿洖褰撳墠娲诲姩鐨勬暟鎹簱杩炴帴銆 榛樿鎯呭喌涓嬶紝鈥榙b鈥欑殑搴旂敤缁勪欢灏嗚繑鍥炲苟婵娲汇 浣犺兘璋冪敤 setDbConnection 鍒囨崲鍒颁竴涓笉鍚岀殑鏁版嵁搴撹繛鎺ャ 鏂规硶濡 insert, createTable 灏嗕娇鐢ㄨ繖涓暟鎹簱杩炴帴 鍘绘墽琛孌B鏌ヨ銆

鏂规硶璇︾粏

addColumn() 鏂规硶
public void addColumn(string $table, string $column, string $type)
$table string 鏂板垪灏嗚澧炲姞鍒扮殑琛ㄣ傝琛ㄥ悕浼氳姝ゆ柟娉曟伆褰撶殑寮曠敤銆
$column string 鏂板垪鐨勫悕绉般傝鍚嶇О浼氳姝ゆ柟娉曟伆褰撶殑寮曠敤銆
$type string 琛ㄧ被鍨嬨 getColumnType 鏂规硶灏嗚瑙﹀彂杞崲鎶借薄绫诲瀷锛堣嫢鏈夛級涓哄疄闄呯被鍨嬨 浠讳綍鏃犳硶璇嗗埆涓烘娊璞$被鍨嬪皢琚繚瀛樺湪鐢熸垚鐨凷QL涓 渚嬪, 鈥榮tring鈥欏皢琚浆鎹㈡垚鈥榲archar(255)鈥欙紝鍚屾椂鈥榮tring not null鈥欏皢鍙樻垚鈥榲archar(255) not null鈥欍
婧愮爜锛 framework/db/CDbMigration.php#278 (鏄剧ず)
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";
}

寤虹珛骞舵墽琛屼竴鏉QL璇彞涓哄鍔犱竴涓柊鐨勬暟鎹簱鍒椼

addForeignKey() 鏂规硶
public void addForeignKey(string $name, string $table, string $columns, string $refTable, string $refColumns, string $delete=NULL, string $update=NULL)
$name string 澶栭敭绾︽潫鐨勫悕绉般
$table string 澶栭敭绾︽潫琚坊鍔犲埌鐨勮〃銆
$columns string 绾︽潫灏嗚澧炲姞鍒扮殑鍒楃殑鍚嶇О銆傚鏋滄湁澶氫釜鍒楃敤閫楀彿鍒嗗紑瀹冧滑銆
$refTable string 澶栭敭寮曠敤鐨勮〃銆
$refColumns string 璇ュ閿紩鐢ㄧ殑鍒楃殑鍚嶇О銆傚鏋滄湁澶氫釜鍒楃敤閫楀彿鍒嗗紑瀹冧滑銆
$delete string ON DELETE 閫夐」銆 澶ч儴鍒 DBMS 鏀寔杩欎簺閫夐」: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
$update string ON UPDATE 閫夐」銆 澶ч儴鍒 DBMS 鏀寔杩欎簺閫夐」: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
婧愮爜锛 framework/db/CDbMigration.php#340 (鏄剧ず)
public function addForeignKey($name$table$columns$refTable$refColumns$delete=null$update=null)
{
    echo 
"    > add foreign key $name$table ($columns) references $refTable ($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";
}

寤虹珛涓涓猄QL璇彞娣诲姞鍒扮幇鏈夎〃鐨勪竴涓閿害鏉熴 璇ユ柟娉曞皢姝g‘寮曠敤琛ㄥ拰鍒楃殑鍚嶇О銆

alterColumn() 鏂规硶
public void alterColumn(string $table, string $column, string $type)
$table string 瑕佽鏀瑰彉鐨勮〃鐨勫悕绉般傝琛ㄥ悕浼氳姝ゆ柟娉曟伆褰撶殑寮曠敤銆
$column string 瑕佽鏀瑰彉鐨勫垪鐨勫悕绉般傝鍚嶇О浼氳姝ゆ柟娉曟伆褰撶殑寮曠敤銆
$type string 鏂板垪鐨勭被鍨嬨 getColumnType 鏂规硶灏嗚瑙﹀彂杞崲鎶借薄绫诲瀷锛堣嫢鏈夛級涓哄疄闄呯被鍨嬨 浠讳綍鏃犳硶璇嗗埆涓烘娊璞$被鍨嬪皢琚繚瀛樺湪鐢熸垚鐨凷QL涓 渚嬪锛屸榮tring鈥欏皢杩欐垚鈥榲archar(255)鈥欙紝鍚屾椂鈥榮tring not null鈥欏皢鍙樻垚鈥榲archar(255) not null鈥欍
婧愮爜锛 framework/db/CDbMigration.php#321 (鏄剧ず)
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";
}

寤虹珛骞舵墽琛屼竴鏉QL璇彞涓烘敼鍙樹竴涓垪鐨勫畾涔夈

createIndex() 鏂规硶
public void createIndex(string $name, string $table, string $column, boolean $unique=false)
$name string 绱㈠紩鍚嶇О. 璇ュ悕绉颁細琚鏂规硶鎭板綋鐨勫紩鐢ㄣ
$table string 瑕佷负鍏跺垱寤烘柊绱㈠紩鐨勮〃銆傝琛ㄥ悕灏嗕細琚鏂规硶鎭板綋鐨勫紩鐢ㄣ
$column string 璇ュ垪锛堟垨澶氬垪锛夊皢琚寘鍚湪绱㈠紩涓傚鏋滄湁澶氬垪璇风敤閫楀彿鍒嗗紑瀹冧滑銆 璇ュ垪鍚嶅皢浼氳姝ゆ柟娉曟伆褰撶殑寮曠敤銆
$unique boolean 鏄惁鍦ㄥ垱寤虹殑绱㈠紩涓鍔 UNIQUE 绾︽潫銆
婧愮爜锛 framework/db/CDbMigration.php#369 (鏄剧ず)
public function createIndex($name$table$column$unique=false)
{
    echo 
"    > create".($unique ' unique':'')." index $name on $table ($column) ...";
    
$time=microtime(true);
    
$this->getDbConnection()->createCommand()->createIndex($name$table$column$unique);
    echo 
" done (time: ".sprintf('%.3f'microtime(true)-$time)."s)\n";
}

寤虹珛骞舵墽琛孲QL璇彞涓哄垱寤烘柊鐨勭储寮曘

createTable() 鏂规硶
public void createTable(string $table, array $columns, string $options=NULL)
$table string 瑕佸垱寤虹殑琛ㄧ殑鍚嶇О銆傛鍚嶇О灏嗚姝ゆ柟娉曟伆褰撶殑寮曠敤銆
$columns array 鏂拌〃涓殑鍒 (name=>definition) 銆
$options string 闄勫姞鐨凷QL鐗囨锛屽皢琚檮鍔犲埌鐢熸垚鐨凷QL銆
婧愮爜锛 framework/db/CDbMigration.php#225 (鏄剧ず)
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";
}

寤虹珛骞舵墽琛屼竴鏉QL璇彞涓烘彃鍏ヤ竴鏉℃柊鐨勬暟鎹〃銆

鏂拌〃鐨勫垪搴旇瀹氫箟鍚嶇О瀵(渚嬪 'name'=>'string'), 杩欓噷 name 浠h〃涓涓垪鍚嶏紝瀹冨皢琚鏂规硶鎭板綋鐨勫紩鐢紝definition 浠h〃鍒楃殑绫诲瀷锛屽畠鍖呭惈涓涓娊璞$殑鏁版嵁搴撶被鍨嬨 getColumnType 鏂规硶灏嗚瑙﹀彂杞崲浠讳綍鎶借薄绫诲瀷涓哄疄闄呯被鍨嬨

濡傛灉涓涓垪鐨勬寚瀹氫粎甯︽湁 (渚嬪 'PRIMARY KEY (name, type)')锛 瀹冨皢琚洿鎺ユ彃鍏ュ埌鐢熸垚鐨凷QL銆

delete() 鏂规硶
public void delete(string $table, mixed $conditions='', array $params=array ( ))
$table string 瑕佸垹闄ょ殑鏁版嵁鎵鍦ㄧ殑琛ㄣ
$conditions mixed 鏀惧湪 WHERE 閮ㄥ垎鐨勬潯浠躲 璇峰弬鑰 CDbCommand::where 鍏充簬鎬庢牱鎸囧畾鏉′欢銆
$params array 缁戝畾鍒拌鏌ヨ鐨勬潯浠躲
婧愮爜锛 framework/db/CDbMigration.php#202 (鏄剧ず)
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";
}

鍒涘缓鍜屾墽琛屼竴鏉 DELETE SQL 璇彞銆

down() 鏂规硶
public boolean down()
{return} boolean
婧愮爜锛 framework/db/CDbMigration.php#73 (鏄剧ず)
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;
    }
}

褰撳垹闄ゆ杩佺Щ鏃讹紝姝ゆ柟娉曞寘鍚鎵ц鐨勯昏緫銆 榛樿瀹炵幇灏嗗紩鍙戝紓甯告寚绀烘棤娉曞垹闄よ縼绉汇 濡傛灉鍙互鍒犻櫎鐩稿簲鐨勮縼绉伙紝瀛愮被鍙互閲嶅啓姝ゆ柟娉曘

dropColumn() 鏂规硶
public void dropColumn(string $table, string $column)
$table string 鍒楀皢琚垹闄ょ殑琛ㄣ傝鍚嶇О浼氳姝ゆ柟娉曟伆褰撶殑寮曠敤銆
$column string 鍒楀皢琚垹闄ょ殑琛ㄧ殑鍚嶇О銆傝鍚嶇О浼氳姝ゆ柟娉曟伆褰撶殑寮曠敤銆
婧愮爜锛 framework/db/CDbMigration.php#291 (鏄剧ず)
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";
}

鍒涘缓骞舵墽琛屼竴鏉QL璇彞涓哄垹闄や竴涓暟鎹簱鍒椼

dropForeignKey() 鏂规硶
public void dropForeignKey(string $name, string $table)
$name string 瑕佸垹闄ょ殑澶栭敭绾︽潫鐨勫悕绉般傝鍚嶇О浼氳姝ゆ柟娉曟伆褰撶殑寮曠敤銆
$table string 澶栭敭灏嗚鍒犻櫎鐨勮〃銆傝鍚嶇О浼氳姝ゆ柟娉曟伆褰撶殑寮曠敤銆
婧愮爜锛 framework/db/CDbMigration.php#353 (鏄剧ず)
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";
}

寤虹珛涓鏉QL璇彞涓哄垹闄ゅ閿害鏉熴

dropIndex() 鏂规硶
public void dropIndex(string $name, string $table)
$name string 瑕佸垹闄ょ储寮曠殑鍚嶇О銆傝鍚嶇О浼氳姝ゆ柟娉曟伆褰撶殑寮曠敤銆
$table string 绱㈠紩瑕佽鍒犻櫎鐨勮〃銆傝鍚嶇О浼氳姝ゆ柟娉曟伆褰撶殑寮曠敤銆
婧愮爜锛 framework/db/CDbMigration.php#382 (鏄剧ず)
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";
}

寤虹珛骞舵墽琛屼竴鏉″垹闄や竴鏉QL绱㈠紩鐨勮鍙ャ

dropTable() 鏂规硶
public void dropTable(string $table)
$table string 瑕佸垹闄ょ殑琛ㄣ傝〃鍚嶅皢琚鏂规硶鎭板綋鐨勫紩鐢ㄣ
婧愮爜锛 framework/db/CDbMigration.php#250 (鏄剧ず)
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";
}

寤虹珛骞舵墽琛屼竴鏉QL璇彞涓哄垹闄や竴涓暟鎹簱琛ㄣ

execute() 鏂规硶 锛堝彲鐢ㄨ嚜 v1.1.7锛
public void execute(string $sql, array $params=array ( ))
$sql string 瑕佹墽琛岀殑 SQL 璇彞銆
$params array 鎵цSQL璇彞鐨勮緭鍏ュ弬鏁 (name=>value) 銆 鍙傝CDbCommand::execute 鑾峰彇鏇村缁嗚妭銆
婧愮爜锛 framework/db/CDbMigration.php#156 (鏄剧ず)
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";
}

鎵ц涓鏉QL璇彞銆 姝ゆ柟娉曚娇鐢 dbConnection 鎵ц鎸囧畾鐨凷QL璇彞銆

getDbConnection() 鏂规硶
public CDbConnection getDbConnection()
{return} CDbConnection 杩斿洖褰撳墠娲诲姩鐨勬暟鎹簱杩炴帴銆
婧愮爜锛 framework/db/CDbMigration.php#128 (鏄剧ず)
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;
}

杩斿洖褰撳墠娲诲姩鐨勬暟鎹簱杩炴帴銆 榛樿鎯呭喌涓嬶紝鈥榙b鈥欑殑搴旂敤缁勪欢灏嗚繑鍥炲苟婵娲汇 浣犺兘璋冪敤 setDbConnection 鍒囨崲鍒颁竴涓笉鍚岀殑鏁版嵁搴撹繛鎺ャ 鏂规硶濡 insert, createTable 灏嗕娇鐢ㄨ繖涓暟鎹簱杩炴帴 鍘绘墽琛孌B鏌ヨ銆

insert() 鏂规硶
public void insert(string $table, array $columns)
$table string 鏂拌灏嗚鎻掑叆鐨勮〃銆
$columns array 琚彃鍏ュ埌姝よ〃鐨勫垪鏁版嵁 (name=>value) 銆
婧愮爜锛 framework/db/CDbMigration.php#170 (鏄剧ず)
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";
}

鍒涘缓鍜屾墽琛屼竴鏉 INSERT SQL 璇彞銆 杩欎釜鏂规硶灏嗘纭浆涔夊垪鍚嶇О锛屽苟缁戝畾瑕佹彃鍏ョ殑鍊笺

refreshTableSchema() 鏂规硶 锛堝彲鐢ㄨ嚜 v1.1.9锛
public void refreshTableSchema(string $table)
$table string 瑕佸埛鏂拌〃鐨勫悕绉
婧愮爜锛 framework/db/CDbMigration.php#395 (鏄剧ず)
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";
}

鍒锋柊鏋舵瀯缂撳瓨琛ㄣ

renameColumn() 鏂规硶
public void renameColumn(string $table, string $name, string $newName)
$table string 鍒楀皢琚噸鍛藉悕鐨勮〃銆傝鍚嶇О浼氳姝ゆ柟娉曟伆褰撶殑寮曠敤銆
$name string 鏃х殑鍒楀悕銆傝鍚嶇О浼氳姝ゆ柟娉曟伆褰撶殑寮曠敤銆
$newName string 鏂扮殑鍒楀悕銆傝鍚嶇О浼氳姝ゆ柟娉曟伆褰撶殑寮曠敤銆
婧愮爜锛 framework/db/CDbMigration.php#305 (鏄剧ず)
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";
}

寤虹珛骞舵墽琛屼竴鏉QL璇彞涓洪噸鍛藉悕涓鍒椼

renameTable() 鏂规硶
public void renameTable(string $table, string $newName)
$table string 瑕侀噸鍛藉悕鐨勮〃銆傝〃鍚嶅皢琚鏂规硶鎭板綋鐨勫紩鐢ㄣ
$newName string 鏂扮殑琛ㄥ悕銆傝〃鍚嶅皢琚鏂规硶鎭板綋鐨勫紩鐢ㄣ
婧愮爜锛 framework/db/CDbMigration.php#238 (鏄剧ず)
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";
}

寤虹珛骞舵墽琛屼竴鏉QL璇彞涓洪噸鍛藉悕鏁版嵁琛ㄣ

safeDown() 鏂规硶 锛堝彲鐢ㄨ嚜 v1.1.7锛
public boolean safeDown()
{return} boolean
婧愮爜锛 framework/db/CDbMigration.php#116 (鏄剧ず)
public function safeDown()
{
}

褰撳垹闄ゆ杩佺Щ鏃讹紝姝ゆ柟娉曞寘鍚鎵ц鐨勯昏緫銆 杩欎釜鏂规硶涓嶅悓浜 up 鍥犱负杩欓噷鏄暟鎹簱閫昏緫鐨勫疄鐜 琚皝闂湪涓涓暟鎹簱浜嬪姟鍐呫 濡傛灉DB閫昏緫闇瑕佸湪涓涓簨鍔″唴瀹炵幇锛屽瓙绫诲彲浠ュ疄鐜拌繖涓柟娉曪紝 鑰屼笉鏄斁鍦 up

safeUp() 鏂规硶 锛堝彲鐢ㄨ嚜 v1.1.7锛
public boolean safeUp()
{return} boolean
婧愮爜锛 framework/db/CDbMigration.php#103 (鏄剧ず)
public function safeUp()
{
}

褰撳簲鐢ㄦ杩佺Щ鏃讹紝姝ゆ柟娉曞寘鍚鎵ц鐨勯昏緫銆 杩欎釜鏂规硶涓嶅悓浜 up 鍥犱负杩欓噷鏁版嵁搴撻昏緫鐨勫疄鐜 琚皝闂湪涓涓暟鎹簱浜嬪姟鍐呫 濡傛灉DB閫昏緫闇瑕佸湪涓涓簨鍔″唴瀹炵幇锛屽瓙绫诲彲浠ュ疄鐜拌繖涓柟娉曪紝 鑰屼笉鏄斁鍦 up

setDbConnection() 鏂规硶
public void setDbConnection(CDbConnection $db)
$db CDbConnection 璇ユ暟鎹簱杩炴帴缁勪欢
婧愮爜锛 framework/db/CDbMigration.php#144 (鏄剧ず)
public function setDbConnection($db)
{
    
$this->_db=$db;
}

璁剧疆褰撳墠娲诲姩鐨勬暟鎹簱杩炴帴銆 姝ゆ暟鎹簱杩炴帴灏嗚杩欎簺鏂规硶浣跨敤锛屽 insert, createTable.

truncateTable() 鏂规硶
public void truncateTable(string $table)
$table string 瑕佹埅鏂殑琛ㄣ傝〃鍚嶅皢琚鏂规硶鎭板綋鐨勫紩鐢ㄣ
婧愮爜锛 framework/db/CDbMigration.php#262 (鏄剧ず)
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";
}

寤虹珛骞舵墽琛孲QL璇彞涓烘埅鏂竴涓暟鎹簱琛ㄣ

up() 鏂规硶
public boolean up()
{return} boolean
婧愮爜锛 framework/db/CDbMigration.php#46 (鏄剧ず)
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;
    }
}

褰撳簲鐢ㄦ杩佺Щ鏃讹紝姝ゆ柟娉曞寘鍚鎵ц鐨勯昏緫銆 瀛愮被鍙互瀹炵幇姝ゆ柟娉曚互鎻愪緵瀹為檯鐨勮縼绉婚昏緫銆

update() 鏂规硶
public void update(string $table, array $columns, mixed $conditions='', array $params=array ( ))
$table string 瑕佹洿鏂扮殑琛ㄣ
$columns array 瑕佹洿鏂扮殑鍒楁暟鎹 (name=>value) 銆
$conditions mixed 鏀惧湪 WHERE 閮ㄥ垎鐨勬潯浠躲 璇峰弬鑰 CDbCommand::where 鍏充簬鎬庢牱鎸囧畾鏉′欢銆
$params array 缁戝畾鍒拌鏌ヨ鐨勬潯浠躲
婧愮爜锛 framework/db/CDbMigration.php#187 (鏄剧ず)
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";
}

鍒涘缓鍜屾墽琛屼竴鏉 UPDATE SQL 璇彞銆 杩欎釜鏂规硶灏嗘纭浆涔夊垪鍚嶇О锛屽苟缁戝畾瑕佹洿鏂扮殑鍊笺

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.