Yii2 Migration 使用

1.創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)遷移:?

yii migrate/create <name>

這是一個(gè)通用的創(chuàng)建數(shù)據(jù)遷移格式,其中<name>是必填的參數(shù),用來(lái)描述當(dāng)前遷移。

ps:<name>這個(gè)只能字母、數(shù)字、下劃線,因?yàn)檫@個(gè)指令會(huì)生成一個(gè)遷移類,<name>會(huì)不是這個(gè)類的類名的一部分。

舉例說(shuō)明,執(zhí)行以下指令:

./yii migrate/create?ArticleInit

如果想配置migration 在?console/config/main.php ?中controllerMap添加配置

'migrate' => [

? ? 'class' => yxx\db\controllers\MigrateController::class,

?? ?'migrationNamespaces' => [

? ? ? ? 'console\migrations',

? ? ? ? 'yxx\user\migrations',

? ? ],

],

執(zhí)行完后在create后默認(rèn)會(huì)在console/migrations目錄下生成一個(gè)控制器


up方法

public function safeUp()

{

? ? if ($this->db->driverName === 'mysql') {

? ? ? ? $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB';

? ? }

? ? $this->createTable('{{%article}}', [

? ? ? ? 'id' => $this->pk()->comment('文章ID'),

? ? ? ? 'userId' => $this->integer()->notNull()->comment('用戶ID'),

? ? ? ? 'title' => $this->string(60)->notNull()->comment('標(biāo)題'),

? ? ? ? 'selfTypeId' => $this->integer()->notNull()->comment('自定義分類ID'),

? ? ? ? 'sysTypeId' => $this->integer()->notNull()->comment('系統(tǒng)分類ID'),

? ? ? ? 'isTuijian' => $this->tinyInteger()->notNull()->comment('是否推薦'),

? ? ? ? 'isComment' => $this->tinyInteger()->notNull()->comment('是否可評(píng)論'),

? ? ? ? 'isPrivate' => $this->tinyInteger()->notNull()->comment('是否僅自己可見'),

? ? ? ? 'publishTime' => $this->timestamp()->notNull()->comment('發(fā)布時(shí)間'),

? ? ? ? 'tags' => $this->string(60)->notNull()->comment('標(biāo)簽,最多5個(gè),英文逗號(hào)分隔'),

? ? ? ? 'summary' => $this->string(200)->notNull()->comment('摘要'),

? ? ? ? 'thumbUrl' => $this->string(120)->notNull()->comment('縮略圖地址'),

? ? ? ? 'createTime' => $this->createTime()->comment('創(chuàng)建時(shí)間'),

? ? ? ? 'updateTime' => $this->updateTime()->comment('更新時(shí)間')

? ? ], $tableOptions);

}

down方法一般為回撤

public function safeDown()

{

? ? echo "M190116032929ArticleInit cannot be reverted.\n";

? ? return false;

}

如下是所有這些數(shù)據(jù)庫(kù)訪問(wèn)方法的列表:

yii\db\Migration::execute(): 執(zhí)行一條 SQL 語(yǔ)句

yii\db\Migration::insert(): 插入單行數(shù)據(jù)

yii\db\Migration::batchInsert(): 插入多行數(shù)據(jù)

yii\db\Migration::update(): 更新數(shù)據(jù)

yii\db\Migration::delete(): 刪除數(shù)據(jù)

yii\db\Migration::createTable(): 創(chuàng)建表

yii\db\Migration::renameTable(): 重命名表名

yii\db\Migration::dropTable(): 刪除一張表

yii\db\Migration::truncateTable(): 清空表中的所有數(shù)據(jù)

yii\db\Migration::addColumn(): 加一個(gè)字段

yii\db\Migration::renameColumn(): 重命名字段名稱

yii\db\Migration::dropColumn(): 刪除一個(gè)字段

yii\db\Migration::alterColumn(): 修改字段

yii\db\Migration::addPrimaryKey(): 添加一個(gè)主鍵

yii\db\Migration::dropPrimaryKey(): 刪除一個(gè)主鍵

yii\db\Migration::addForeignKey(): 添加一個(gè)外鍵

yii\db\Migration::dropForeignKey(): 刪除一個(gè)外鍵

yii\db\Migration::createIndex(): 創(chuàng)建一個(gè)索引

yii\db\Migration::dropIndex(): 刪除一個(gè)索引

2.提交遷移:

可以執(zhí)行如下:?

./yii migrate?

這個(gè)指令會(huì)提交所有的遷移?

或者這樣,指定類名,提交一個(gè)遷移?

./yii migrate M190116032929ArticleInit?

刪除某字段:

public function down() {$this->dropColumn('{{app_base}}', 'manager_id');}

刪除某張表:

public function down() {$this->dropTable('{{%file_storage_item}}');}

3.撤銷

./yii migrate/down 執(zhí)行某些撤銷對(duì)表的操作 ./yii migratre/to (遷移文件名)執(zhí)行某個(gè)指定的遷移文件 在創(chuàng)建數(shù)據(jù)表的過(guò)程中可以同時(shí)聲稱多張表,刪除多張表 執(zhí)行過(guò)的遷移文件,會(huì)在數(shù)據(jù)庫(kù)的migration 中生成一條記錄,記錄此遷移文件已經(jīng)執(zhí)行過(guò),下次將執(zhí)行數(shù)據(jù)表中不存在的遷移文件 注意: ./yii migrate/down 此命令執(zhí)行不只刪除了對(duì)數(shù)據(jù)庫(kù)的操作同時(shí)也會(huì)刪除migration數(shù)據(jù)表中的執(zhí)行記錄

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容