尛尛尛 2015-12-29 17:23:46 34417次浏览 5条评论 16 2 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('{{application_service}}', 'status', 'SMALLINT(4) NOT NULL DEFAULT 0 COMMENT       "绑定状态,0:解绑 1:未绑定 2:审核中 3:审核通过 4:审核拒绝 5:禁用"');}

增加索引:

public function up()
    {$this->createIndex('created_at', "{{app_base}}", ['created_at'],true); }

创建数据表:

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代码:图标'),
            '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 默认执行 ./yii migrate/up
./yii migrate/down 执行某些撤销对表的操作
./yii migratre/to (迁移文件名)执行某个指定的迁移文件
在创建数据表的过程中可以同时声称多张表,删除多张表
执行过的迁移文件,会在数据库的migration 中生成一条记录,记录此迁移文件已经执行过,下次将执行数据表中不存在的迁移文件
注意:
./yii migrate/down 此命令执行不只删除了对数据库的操作同时也会删除migration数据表中的执行记录

觉得很赞
  • 评论于 2015-12-30 20:22 举报

    个人感觉 migrate 功能有点鸡肋 数据表结构变化不能保存源表的数据

    6 条回复
    评论于 2015-12-31 09:53 回复

    我觉得对于一些修改和建表操作在团队协作中还是挺方便的

    评论于 2015-12-31 10:55 回复

    什么意思啊?表结构变化,原来的结构你不知道?一个up一个down还原不了吗

    评论于 2016-01-01 09:04 回复

    我说的是表中的数据会丢失啊

    评论于 2016-07-28 14:16 回复

    还是看得不太明白,这个要怎么备份数据库 恢复数据库

    评论于 2017-12-02 16:23 回复

    您现在会备份数据库了吗?

    评论于 2017-12-03 13:16 回复

    用定时脚本备份

    觉得很赞
  • 评论于 2016-03-07 14:17 举报

    请问怎样设置编码格式

    2 条回复
    评论于 2017-08-26 14:16 回复

    参见Advanced版创建user表的migrate

    评论于 2018-03-06 19:40 回复

    createTable 有第三个参数,在第三个参数里就可以设置了

  • 评论于 2016-05-23 13:32 举报

    最近我也试用了一下yii的migrate,感觉确实挺鸡肋的!

    1 条回复
    评论于 2017-04-09 00:40 回复

    这个可以让我们在开发的过程中关注数据库的变化,暂时发现这个挺好的

  • 评论于 2019-01-23 17:31 举报

    很有用!!

  • 评论于 2019-05-06 14:24 举报

    从某些方面使用上来讲 还是很方便的,但是任何工具都不可能十全十美吧

您需要登录后才可以评论。登录 | 立即注册