yii2 migrate 数据库迁移

5 篇文章 0 订阅

开发中经常会用到的方法小结:

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数据库迁移遇到的问题,对于项目中接触到新的会持续更新



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值