Thinkphp5.0的migration使用方法
咻兔哔 / 技术文章

用过Laravel的朋友可能都知道Migration是个很好用的东西,新版的Thinkphp5.0也引入了这一方式,不过需要大家手动引入

composer require topthink/think-migration 

如果还不会composer的同学建议直接下载官方完整包或者去学习下如何使用composer
很好的官方中文教程
http://www.kancloud.cn/thinkphp/composer
在thinkphp中使用migration之前我建议大家在引入一个包,在做测试数据的时候会很有用的,这一点TP官方手册上根本没有提起,

composer require fzaninotto/faker

具体来说这个包是填充数据用的配合Thinkphp中的Seed来使用

好了言归正传
migration怎么使用呢,直接上代码
在命令行下执行

php think migrate:create ArticleMigration
//命名规则驼峰法+Migration
//如果是中间表会自动转换成下划线
//如:
php think migrate:create ArticlesTagsMigration
//这样生成的表就是articles_tags

有关于Migration的详细使用方法可以参照
http://docs.phinx.org/en/latest/intro.html
不过是英文的,创建好文件后会在根目录database目录下生成一个migration文件内如大概如下:

<?php

use Phinx\Migration\AbstractMigration;

class ArticlesMigration extends AbstractMigration
{
    /**
     * 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(){
        
    }
}

不过用这个方法建成的表不能进行回滚操作等方法,官方建议使用up 和 down 方法也就是变成下面的方式

<?php

use Phinx\Migration\AbstractMigration;

class ArticlesMigration extends AbstractMigration
{
    
    public function up()
    {
        $table= $this->table('articles');
        $table->addColumn('title','string')
            ->addColumn('content','text')
            ->addColumn('category_id','integer')
            ->addColumn('user_id','integer')
            ->addTimestamps()
            ->addIndex(['title'],['unique'=>true])
            ->addForeignKey('category_id','category')
            ->addForeignKey('user_id','users')
            ->create();
    }

    public function down()
    {
        $this->dropTable('articles');
    }
}

这就是一个具体的创建表的例子,至于具体的表字段的类型还请大家自己参考上面的文档.

下面说下如何自动填充数据

大概这个可以理解为Laravel的ModelFactory吧
新建一个seed

php think seed:create Articles 

同样文件生成在database目录seeds下

<?php

use Phinx\Seed\AbstractSeed;

class Articles extends AbstractSeed
{
    /**
     * Run Method.
     *
     * Write your database seeder using this method.
     *
     * More information on writing seeders is available here:
     * http://docs.phinx.org/en/latest/seeding.html
     */
    public function run()
    {
            $users_id = \app\common\model\User::column('id');
            $category_id= \app\common\model\Category::column('id');
            $fake = Faker\Factory::create();
            $data = [];
            for ($i=0;$i<10;$i++){
            $data[] =[
                'title'=>$fake->sentence,
                'content'=>$fake->paragraph,
                'category_id'=>$fake->randomElement($category_id),
                'user_id'=>$fake->randomElement($users_id),
                ];
            }
        $this->insert('articles',$data);
    }
}

之需要在run()方法内写好规则在命令行下执行

php seed:run Article

数据库就会自动填充规则所需要的数据了

$fake = Faker\Factory::create();
// 这就是文章最开始要大家引入的另外一个包
// 不然都是要大家手动去填写数据,关于fake大家可以自行百度一下如何使用
支付宝捐赠
请使用支付宝扫一扫进行捐赠
微信捐赠
请使用微信扫一扫进行赞赏
有 0 篇文章