yii2 migrate 数据库迁移的简单分享
開發(fā)中經(jīng)常會(huì)用到的方法小結(jié):
1、./yii migrate xxx_xx 在表中插入某字段 :
public function up(){$this->addColumn('{{application_service}}', 'auditor', 'INT(10) NOT NULL COMMENT "審核人" AFTER 'user_id', CHANGE COLUMN `status` `status` tinyint(4) NOT NULL COMMENT "綁定狀態(tài),0:解綁 1:綁定" AFTER 'auditor''); }
2.?修改表中某字段:
public function up(){$this->alterColumn('{{application_service}}', 'status', 'SMALLINT(4) NOT NULL DEFAULT 0 COMMENT "綁定狀態(tài),0:解綁 1:未綁定 2:審核中 3:審核通過 4:審核拒絕 5:禁用"'); }
增加索引:
public function up(){$this->createIndex('created_at', "{{app_base}}", ['created_at'],true); }
創(chuàng)建數(shù)據(jù)表:
public function up() {$tableOptions = null;if ($this->db->driverName === 'mysql') {$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB COMMENT="菜單表"';}$this->createTable('{{%menu}}', ['id' => $this->primaryKey(),'parent_id' => $this->integer(11)->defaultValue(0)->comment('父級菜單id'),'menu_name' => $this->string(100)->notNull()->comment('菜單名稱'),'menu_type' => $this->string(100)->notNull()->comment('菜單類型(menu菜單,sub_menu子菜單)'),'menu_action' => $this->string(100)->notNull()->comment('菜單鏈接'),'menu_roles' => $this->string(100)->comment('角色'),'menu_depth' => $this->smallInteger(1)->defaultValue(0)->comment('菜單深度'),'menu_icon' => $this->text()->comment('ICON代碼:圖標(biāo)'),'menu_des' => $this->text()->comment('菜單簡介'),'menu_order' => $this->smallInteger(1)->defaultValue(0)->comment('顯示順序'),'menu_show' => $this->smallInteger(1)->defaultValue(0)->comment('是否顯示(0:顯示, 1:不顯示)'),'created_at' => $this->integer(),'updated_at' => $this->integer(),], $tableOptions); }
刪除某字段:
public function down(){$this->dropColumn('{{app_base}}', 'manager_id');}
刪除某張表:
public function down(){$this->dropTable('{{%file_storage_item}}'); }
2/./yii migrate 默認(rèn)執(zhí)行 ./yii migrate/up?
./yii migrate/down 執(zhí)行某些撤銷對表的操作 ./yii migratre/to (遷移文件名)執(zhí)行某個(gè)指定的遷移文件 在創(chuàng)建數(shù)據(jù)表的過程中可以同時(shí)聲稱多張表,刪除多張表 執(zhí)行過的遷移文件,會(huì)在數(shù)據(jù)庫的migration 中生成一條記錄,記錄此遷移文件已經(jīng)執(zhí)行過,下次將執(zhí)行數(shù)據(jù)表中不存在的遷移文件 注意: ./yii migrate/down 此命令執(zhí)行不只刪除了對數(shù)據(jù)庫的操作同時(shí)也會(huì)刪除migration數(shù)據(jù)表中的執(zhí)行記錄
?
轉(zhuǎn)載于:https://www.cnblogs.com/echojson/p/10895232.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的yii2 migrate 数据库迁移的简单分享的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 47.QT-QChart之曲线图,饼状图
- 下一篇: Python与MySQL连接