Yii內(nèi)有migrate組件丈积,可以幫助開發(fā)者簡介和規(guī)范地維護(hù)數(shù)據(jù)庫穷缤,方便在多個(gè)開發(fā)者間進(jìn)行數(shù)據(jù)遷移夫壁。
- 在Yii項(xiàng)目的根目錄打開終端丹壕,執(zhí)行指令創(chuàng)建migrate遷移文件庆械,指令如下:
yii migrate\create 名稱
migrate指令創(chuàng)建
執(zhí)行完指令后在“console\migrations”下會(huì)生成剛剛執(zhí)行同名的migrate文件。
migrations文件目錄
- 打開對應(yīng)文件雀费,輸入相應(yīng)的sql語句干奢。
<?php
use yii\db\Migration;
/**
* Handles the creation of table `{{%file}}`.
*/
class m200219_112137_create_test_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$sql = " ";//在此處寫入需要執(zhí)行的sql語句
$this->db->createCommand($sql)->execute();
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropTable('{{%file}}');
}
}
說明:在該migrate中的safeUp為執(zhí)行的語句,而safeDown為回滾的語句盏袄。
- 執(zhí)行對應(yīng)的migrate文件忿峻,在根目錄打開終端薄啥,執(zhí)行下面的語句:
yii migrate migrate指定文件路徑
執(zhí)行指定migrate文件