thinkphp5下使用phinx

    xiaoxiao2021-03-25  75

    tp5学习laravel,支持phinx。不过laravel是自己支持,tp是用了开源的第三方库。用法都差不多。

    安装think-migrate

    composer require topthink/think-migration

    注意事项,不支持修改文件配置目录

    在命令行下运行查看帮助,可以看到新增的命令

    php think migrate 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 optimize optimize:autoload Optimizes PSR0 and PSR4 packages to be loaded wit h classmaps too, good for production. optimize:config Build config and common file cache. optimize:route Build route cache. optimize:schema Build database schema cache. seed seed:create Create a new database seeder seed:run Run database seeders

    创建迁移类,类名首字母必须为大写,采用驼峰命名法

    php think migrate:create Users 可以看到目录下有新文件 .\database\migrations\20161117144043_users.php 创建表

    public function change() { $table = $this->table('xxxx',array('engine'=>'InnoDB')); $table->addColumn('xxx', 'integer') ->addColumn('xx', 'string', array('limit' => 50)) ->addColumn('xx', 'string', array('limit' => 50)) ->addColumn('xx', 'string', array('limit' => 50)) ->addColumn('xxxx', 'integer', array('limit' => MysqlAdapter::INT_TINY)) ->addIndex(array('xxxx')) ->create(); }

    创建seed

    php think seed:create UserSeeder

    提示

    Create seeds directory? [y]/n (yes/no) [yes]: Y 生成 .\database\seeds\UserSeeder.php 填充里面的run 方法

    – 注意点:创建的目录文件可能没有权限,需要手动修改权限和所属group和owner

    – 执行

    php think migrate:run

    – 回滚

    php think rollback

    – 执行seeder

    php think seed:run -v

    seeder没有rollback 方法,如果重复执行,会插入重复的数据,因此尽量采用插入包含主键ID的数据,避免重复插入。如果中途执行失败,要执行后续某个指定表的seeder,可以加入 -s XXXSeeder

    php think seed:run -s UserSeeder

    更多资料,请访问 http://docs.phinx.org/en/latest/

    转载请注明原文地址: https://ju.6miu.com/read-23637.html

    最新回复(0)