通知短信+运营短信,5秒速达,支持群发助手一键发送🚀高效触达和通知客户 广告
[TOC] > [官方手册](http://docs.phinx.org/en/latest/migrations.html#executing-queries) > [参考网址](https://www.jianshu.com/p/a01c5b6ea7f6) > [参考网址](http://www.bijishequ.com/detail/525120?p=) > migrations 就是通过php 的命令去生成和修改数据库,甚至可以回退 ## 安装 `composer require topthink/think-migration` ## 查看命令 ``` php thinkphp migrate //用于数据库迁移相关操作 migrate:breakpoint Manage breakpoints migrate:create Create a new migration migrate:rollback Rollback the last or to a specific migration migrate:run Migrate the database migrate:status Show migration status seed //用于数据库填充相关操作 seed:create Create a new database seeder seed:run Run database seeders ``` ## 使用`change`方法 迁移(生成数据表) ### 生成 1. 生成迁移类,类名必须使用 驼峰式 命名法, `$ php think migrate:create CreateUserTable` 2. 添加数据库信息 ```php public function change() { $table = $this->table('users',array('engine'=>'MyISAM')); $table ->addColumn('username', 'string',array('limit' => 26,'default'=>'','comment'=>'用户名,登陆使用')) ->addColumn('password', 'string',array('limit' => 32,'default'=>md5('123456'),'comment'=>'用户密码')) ->addColumn('login_status', 'boolean',array('limit' => 1,'default'=>0,'comment'=>'登陆状态')) ->addColumn('login_code', 'string',array('limit' => 32,'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('is_delete', 'boolean',array('limit' => 1,'default'=>0,'comment'=>'删除状态,1已删除')) ->addIndex(array('username'), array('unique' => true)) ->create(); } ``` 3. 执行所有未执行的迁移类 `$ php think migrate:run` 执行某个迁移类 加 `-t`操作 `php think migrate:run -t YYYYMMDDHHMMSS //后面跟上迁移文件文件名的 时间戳部` ### 回滚 回滚迁移,默认只会回滚 最近一次 执行的迁移: `php think migrate:rollback` ` -t` 选项来指定需要回滚的迁移: `$ php think migrate:rollback -t YYYYMMDDHHMMSS` ## 删除`change` 用而使用 `up`和`down`方法 删除默认自带的 change 方法,创建 up() 方法和 down() 方法。 up() 方法是在执行 run 命令执行的, down() 是在执行 rollback 命令执行的。 ``` public function up() { $table = $this->table('users'); $table->addColumn('nickname', 'string', ['limit' => 16, 'null' => false]) ->addColumn('email', 'string', ['limit' => 32, 'null' => false]) ->addColumn('password', 'string', ['limit' => 64, 'null' => false]) ->create(); } public function down() { $this->dropTable('users'); } ``` ## seed 方法生成数据 也可以用来生成非migrations生成的数据库 生成 seen类 `php think seed:create User` 在类中创建数据 ``` public function run(){ public function run(){ $data = [ [ 'password' => $this->_pwd(), 'username' => date('Ymd H:i:s'), ], [ 'password' => $this->_pwd(), 'username' => date('Ymd H:i:s').'_1', ], ]; $posts = $this->table('users'); $posts->insert($data)->save(); } private function _pwd(){ return md5(rand(1111,9999)); } ``` 执行生成命令 `php think seed:run` 如果需要数据伪装更好 可以用安装第三方库 `composer require fzaninotto/faker` ``` public function run() { $faker = Faker\Factory::create(); $data = []; for ($i = 0; $i < 100; $i++) { $data[] = [ 'username' => $faker->userName, 'password' => sha1($faker->password), 'password_salt' => sha1('foo'), 'email' => $faker->email, 'first_name' => $faker->firstName, 'last_name' => $faker->lastName, 'created' => date('Y-m-d H:i:s'), ]; } $this->insert('users', $data); } ```