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琚璁′负鍜屸測iic migrate鈥濆懡浠や竴璧蜂娇鐢ㄣ
姣忎竴涓 CDbMigration 鐨勫瓙绫昏〃绀轰竴涓崟涓暟鎹簱杩佺Щ锛 瀹冮氳繃瀛愮被鍚嶇О璇嗗埆銆
鍦ㄦ瘡涓涓縼绉诲唴閮紝 up 鏂规硶鍖呭惈浜 "upgrading"閫昏緫 鍦ㄥ簲鐢ㄧ▼搴忎腑浣跨敤鏁版嵁搴擄紱褰 down 鏂规硶鍖呭惈 "downgrading" 閫昏緫鏃躲 "yiic migrate" 鍛戒护绠$悊鍦ㄤ竴涓簲鐢ㄧ▼搴忎腑鎵鏈夊彲鐢ㄧ殑杩佺Щ銆
CDbMigration鎻愪緵浜嗕竴濂椾究鍒╃殑鏂规硶鏉ユ搷绾垫暟鎹簱鏁版嵁鍜宻chema銆 渚嬪锛insert 鏂规硶鑳藉緢瀹规槗鐨勫湪涓涓暟鎹簱琛ㄤ腑鎻掑叆涓琛屾暟鎹 createTable 鏂规硶鑳界敤浣滃垱寤轰竴涓暟鎹簱銆 鍜CDbCommand涓殑鐩稿悓鏂规硶姣旇緝锛岃繖浜涙柟娉曞皢鏄剧ず闄勫姞淇℃伅, 濡傛柟娉曞弬鏁板拰鎵ц鏃堕棿锛 瀹冧滑鍦ㄥ簲鐢ㄨ縼绉绘椂鏄潪甯告湁鐢ㄧ殑銆
鍏叡鏂规硶
鏂规硶 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|
__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 |
灞炴ц缁
杩斿洖褰撳墠娲诲姩鐨勬暟鎹簱杩炴帴銆 榛樿鎯呭喌涓嬶紝鈥榙b鈥欑殑搴旂敤缁勪欢灏嗚繑鍥炲苟婵娲汇 浣犺兘璋冪敤 setDbConnection 鍒囨崲鍒颁竴涓笉鍚岀殑鏁版嵁搴撹繛鎺ャ 鏂规硶濡 insert, createTable 灏嗕娇鐢ㄨ繖涓暟鎹簱杩炴帴 鍘绘墽琛孌B鏌ヨ銆
鏂规硶璇︾粏
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鈥欍 |
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璇彞涓哄鍔犱竴涓柊鐨勬暟鎹簱鍒椼
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 |
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‘寮曠敤琛ㄥ拰鍒楃殑鍚嶇О銆
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鈥欍 |
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璇彞涓烘敼鍙樹竴涓垪鐨勫畾涔夈
public void createIndex(string $name, string $table, string $column, boolean $unique=false)
| ||
$name | string | 绱㈠紩鍚嶇О. 璇ュ悕绉颁細琚鏂规硶鎭板綋鐨勫紩鐢ㄣ |
$table | string | 瑕佷负鍏跺垱寤烘柊绱㈠紩鐨勮〃銆傝琛ㄥ悕灏嗕細琚鏂规硶鎭板綋鐨勫紩鐢ㄣ |
$column | string | 璇ュ垪锛堟垨澶氬垪锛夊皢琚寘鍚湪绱㈠紩涓傚鏋滄湁澶氬垪璇风敤閫楀彿鍒嗗紑瀹冧滑銆 璇ュ垪鍚嶅皢浼氳姝ゆ柟娉曟伆褰撶殑寮曠敤銆 |
$unique | boolean | 鏄惁鍦ㄥ垱寤虹殑绱㈠紩涓鍔 UNIQUE 绾︽潫銆 |
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璇彞涓哄垱寤烘柊鐨勭储寮曘
public void createTable(string $table, array $columns, string $options=NULL)
| ||
$table | string | 瑕佸垱寤虹殑琛ㄧ殑鍚嶇О銆傛鍚嶇О灏嗚姝ゆ柟娉曟伆褰撶殑寮曠敤銆 |
$columns | array | 鏂拌〃涓殑鍒 (name=>definition) 銆 |
$options | string | 闄勫姞鐨凷QL鐗囨锛屽皢琚檮鍔犲埌鐢熸垚鐨凷QL銆 |
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銆
public void delete(string $table, mixed $conditions='', array $params=array (
))
| ||
$table | string | 瑕佸垹闄ょ殑鏁版嵁鎵鍦ㄧ殑琛ㄣ |
$conditions | mixed | 鏀惧湪 WHERE 閮ㄥ垎鐨勬潯浠躲 璇峰弬鑰 CDbCommand::where 鍏充簬鎬庢牱鎸囧畾鏉′欢銆 |
$params | array | 缁戝畾鍒拌鏌ヨ鐨勬潯浠躲 |
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 璇彞銆
public boolean down()
| ||
{return} | boolean |
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;
}
}
褰撳垹闄ゆ杩佺Щ鏃讹紝姝ゆ柟娉曞寘鍚鎵ц鐨勯昏緫銆 榛樿瀹炵幇灏嗗紩鍙戝紓甯告寚绀烘棤娉曞垹闄よ縼绉汇 濡傛灉鍙互鍒犻櫎鐩稿簲鐨勮縼绉伙紝瀛愮被鍙互閲嶅啓姝ゆ柟娉曘
public void dropColumn(string $table, string $column)
| ||
$table | string | 鍒楀皢琚垹闄ょ殑琛ㄣ傝鍚嶇О浼氳姝ゆ柟娉曟伆褰撶殑寮曠敤銆 |
$column | string | 鍒楀皢琚垹闄ょ殑琛ㄧ殑鍚嶇О銆傝鍚嶇О浼氳姝ゆ柟娉曟伆褰撶殑寮曠敤銆 |
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璇彞涓哄垹闄や竴涓暟鎹簱鍒椼
public void dropForeignKey(string $name, string $table)
| ||
$name | string | 瑕佸垹闄ょ殑澶栭敭绾︽潫鐨勫悕绉般傝鍚嶇О浼氳姝ゆ柟娉曟伆褰撶殑寮曠敤銆 |
$table | string | 澶栭敭灏嗚鍒犻櫎鐨勮〃銆傝鍚嶇О浼氳姝ゆ柟娉曟伆褰撶殑寮曠敤銆 |
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璇彞涓哄垹闄ゅ閿害鏉熴
public void dropIndex(string $name, string $table)
| ||
$name | string | 瑕佸垹闄ょ储寮曠殑鍚嶇О銆傝鍚嶇О浼氳姝ゆ柟娉曟伆褰撶殑寮曠敤銆 |
$table | string | 绱㈠紩瑕佽鍒犻櫎鐨勮〃銆傝鍚嶇О浼氳姝ゆ柟娉曟伆褰撶殑寮曠敤銆 |
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绱㈠紩鐨勮鍙ャ
public void dropTable(string $table)
| ||
$table | string | 瑕佸垹闄ょ殑琛ㄣ傝〃鍚嶅皢琚鏂规硶鎭板綋鐨勫紩鐢ㄣ |
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璇彞涓哄垹闄や竴涓暟鎹簱琛ㄣ
public void execute(string $sql, array $params=array (
))
| ||
$sql | string | 瑕佹墽琛岀殑 SQL 璇彞銆 |
$params | array | 鎵цSQL璇彞鐨勮緭鍏ュ弬鏁 (name=>value) 銆 鍙傝CDbCommand::execute 鑾峰彇鏇村缁嗚妭銆 |
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璇彞銆
public CDbConnection getDbConnection()
| ||
{return} | CDbConnection | 杩斿洖褰撳墠娲诲姩鐨勬暟鎹簱杩炴帴銆 |
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鏌ヨ銆
public void insert(string $table, array $columns)
| ||
$table | string | 鏂拌灏嗚鎻掑叆鐨勮〃銆 |
$columns | array | 琚彃鍏ュ埌姝よ〃鐨勫垪鏁版嵁 (name=>value) 銆 |
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 璇彞銆 杩欎釜鏂规硶灏嗘纭浆涔夊垪鍚嶇О锛屽苟缁戝畾瑕佹彃鍏ョ殑鍊笺
public void refreshTableSchema(string $table)
| ||
$table | string | 瑕佸埛鏂拌〃鐨勫悕绉 |
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";
}
鍒锋柊鏋舵瀯缂撳瓨琛ㄣ
public void renameColumn(string $table, string $name, string $newName)
| ||
$table | string | 鍒楀皢琚噸鍛藉悕鐨勮〃銆傝鍚嶇О浼氳姝ゆ柟娉曟伆褰撶殑寮曠敤銆 |
$name | string | 鏃х殑鍒楀悕銆傝鍚嶇О浼氳姝ゆ柟娉曟伆褰撶殑寮曠敤銆 |
$newName | string | 鏂扮殑鍒楀悕銆傝鍚嶇О浼氳姝ゆ柟娉曟伆褰撶殑寮曠敤銆 |
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璇彞涓洪噸鍛藉悕涓鍒椼
public void renameTable(string $table, string $newName)
| ||
$table | string | 瑕侀噸鍛藉悕鐨勮〃銆傝〃鍚嶅皢琚鏂规硶鎭板綋鐨勫紩鐢ㄣ |
$newName | string | 鏂扮殑琛ㄥ悕銆傝〃鍚嶅皢琚鏂规硶鎭板綋鐨勫紩鐢ㄣ |
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璇彞涓洪噸鍛藉悕鏁版嵁琛ㄣ
public boolean safeDown()
| ||
{return} | boolean |
褰撳垹闄ゆ杩佺Щ鏃讹紝姝ゆ柟娉曞寘鍚鎵ц鐨勯昏緫銆 杩欎釜鏂规硶涓嶅悓浜 up 鍥犱负杩欓噷鏄暟鎹簱閫昏緫鐨勫疄鐜 琚皝闂湪涓涓暟鎹簱浜嬪姟鍐呫 濡傛灉DB閫昏緫闇瑕佸湪涓涓簨鍔″唴瀹炵幇锛屽瓙绫诲彲浠ュ疄鐜拌繖涓柟娉曪紝 鑰屼笉鏄斁鍦 up銆
public boolean safeUp()
| ||
{return} | boolean |
褰撳簲鐢ㄦ杩佺Щ鏃讹紝姝ゆ柟娉曞寘鍚鎵ц鐨勯昏緫銆 杩欎釜鏂规硶涓嶅悓浜 up 鍥犱负杩欓噷鏁版嵁搴撻昏緫鐨勫疄鐜 琚皝闂湪涓涓暟鎹簱浜嬪姟鍐呫 濡傛灉DB閫昏緫闇瑕佸湪涓涓簨鍔″唴瀹炵幇锛屽瓙绫诲彲浠ュ疄鐜拌繖涓柟娉曪紝 鑰屼笉鏄斁鍦 up銆
public void setDbConnection(CDbConnection $db)
| ||
$db | CDbConnection | 璇ユ暟鎹簱杩炴帴缁勪欢 |
璁剧疆褰撳墠娲诲姩鐨勬暟鎹簱杩炴帴銆 姝ゆ暟鎹簱杩炴帴灏嗚杩欎簺鏂规硶浣跨敤锛屽 insert, createTable.
public void truncateTable(string $table)
| ||
$table | string | 瑕佹埅鏂殑琛ㄣ傝〃鍚嶅皢琚鏂规硶鎭板綋鐨勫紩鐢ㄣ |
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璇彞涓烘埅鏂竴涓暟鎹簱琛ㄣ
public boolean up()
| ||
{return} | boolean |
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;
}
}
褰撳簲鐢ㄦ杩佺Щ鏃讹紝姝ゆ柟娉曞寘鍚鎵ц鐨勯昏緫銆 瀛愮被鍙互瀹炵幇姝ゆ柟娉曚互鎻愪緵瀹為檯鐨勮縼绉婚昏緫銆
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 | 缁戝畾鍒拌鏌ヨ鐨勬潯浠躲 |
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 璇彞銆 杩欎釜鏂规硶灏嗘纭浆涔夊垪鍚嶇О锛屽苟缁戝畾瑕佹洿鏂扮殑鍊笺