开发中经常会用到的方法小结:
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 "绑定状态,0:解绑 1:绑定" AFTER `auditor`');
}
修改表中某字段:
public function up()
{
$this->alterColumn('{{cp_application_service_binding}}', 'status', 'SMALLINT(4) NOT NULL DEFAULT 0 COMMENT "绑定状态,0:解绑 1:未绑定 2:审核中 3:审核通过 4:审核拒绝 5:禁用"');
}
修改字段名:
public function up()
{
$this->renameColumn('{{pay_method}}','updata_time','update_time');
}
增加索引:
public function up()
{
$this->createIndex('created_at', "{{common_app_base}}", ['created_at'],true);
}
创建数据表:
public function up()
{
$tableOptions = null;
if ($this->db->driverName === 'mysql') {
// http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB COMMENT="菜单表"';
}
$this->createTable('{{%wx_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代码:图标'),
'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('{{common_app_base}}', 'manager_id');
}
删除某张表:
public function down()
{
$this->dropTable('{{%common_file_storage_item}}');
}
2. ./yii migrate 默认执行 ./yii migrate/up
./yii migrate/down 执行某些撤销对表的操作
./yii migratre/to (迁移文件名)执行某个指定的迁移文件
在创建数据表的过程中可以同时声称多张表,删除多张表
执行过的迁移文件,会在数据库的migration 中生成一条记录,记录此迁移文件已经执行过,下次将执行数据表中不存在的迁移文件
注意:
./yii migrate/down 此命令执行不只删除了对数据库的操作同时也会删除migration数据表中的执行记录
此篇博文基本上能满足初步使用yii2数据库迁移遇到的问题,对于项目中接触到新的会持续更新