🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] > [参考](https://www.kancloud.cn/manual/thinkphp6_0/1118028) ## 概述 安装 ``` composer require topthink/think-migration ``` 初始化文件 AnyClassNameYouWant 为你本次常见的类名 ``` php think migrate:create AnyClassNameYouWant //文件名类似/database/migrations/20190615151716_any_class_name_you_want.php ``` 添加内容 <details> <summary>20190615151716_any_class_name_you_want.php</summary> ``` <?php use think\migration\Migrator; use think\migration\db\Column; class AnyClassNameYouWant extends Migrator { /** * Change Method. * * Write your reversible migrations using this method. * * More information on writing migrations is available here: * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class * * The following commands can be used in this method and Phinx will * automatically reverse them when rolling back: * * createTable * renameTable * addColumn * renameColumn * addIndex * addForeignKey * * Remember to call "create()" or "update()" and NOT "save()" when working * with the Table class. */ public function change() { $table = $this->table('user', ['id' => 'user_id','collation'=>'utf8mb4_general_ci']); $table->addColumn('login_name', 'string', ['length' => 50, 'default'=>'', 'comment'=>'账号']) ->addColumn('user_name', 'string', ['length' => 50, 'default'=>'', 'comment'=>'姓名']) ->addColumn('password', 'string', ['length' => 50, 'default'=>'', 'comment'=>'密码']) ->addColumn('status', 'integer',['length'=>1,'default'=>1,'comment'=>"状态;1=正常"]) ->addColumn('email', 'string', ['length' => 50, 'default'=>'', 'comment'=>'邮箱']) ->addColumn('avatar', 'string', ['length' => 100, 'default'=>'', 'comment'=>'头像']) ->addColumn('sex', 'integer', ['length' => 1, 'default'=>0, 'comment'=>'性别,0=未知,1=男,2=女']) ->addColumn('phone', 'integer', ['length' => 11 , 'default'=>0, 'comment'=>'手机']) ->addColumn('last_login_ip', 'integer',array('limit' => 11,'default'=>0,'comment'=>'最后登录IP')) ->addColumn('last_login_time', 'datetime',array('default'=>0,'comment'=>'最后登录时间')) ->addColumn('user_role', 'integer', ['length' => 1 , 'default'=>0, 'comment'=>'系统管理员,0=普通人员,1=超级管理员,2=普通管理员']) ->create(); $user[]=[ "login_name"=>"superadmin", "user_name"=>"超级管理员", "password"=>md5(123456), "user_role"=>1, ]; $user[]=[ "login_name"=>"admin", "user_name"=>"管理员", "password"=>md5(123456), "user_role"=>2, ]; $table->insert($user)->saveData(); } } ``` </details> <br/> 执行 ``` php think migrate:run ``` 添加新的改动: 创建一个新的类名 ``` php think migrate:create AnyClassNameYouWant_new ``` 类文件的change添加变更的操作: 只支持 createTable renameTable addColumn renameColumn addIndex addForeignKey ``` ... public function change() { $table = $this->table('md_user'); $table->addColumn('system_role1', 'integer', ['length' => 1 , 'default'=>0, 'comment'=>'系统管理员,0=普通人员,1=超级管理员,2=普通管理员']) ->save(); } ... ``` 再次执行 ``` php think migrate:run ```