thinkphp使用migration/Seeder

1.安装migration

composer require topthink/think-migration=2.0.* 	//加上版本控制 报错版本就使用1.0

2.安装完成执行

php think

看到如下内容安装完成
在这里插入图片描述
3.切换项目根目录

php think migrate:create TableName
//TableName 格式:首字母大写的驼峰法。该命令是用来创建一个 migration 文件,比如这里我们创建一个 User 的 migration 文件:

4.我们创建表User

php think migrate:create Users	//提示yes的时候 输入yes

5.然后tp的目录结构中会出现database的文件 打开对应的Users.php
6.删除change()方法

migration(数据库操作)

 public function up()
    {
        /*if ($this->hasTable('Users')) {
            $this->dropTable('Users');
        }*/
        $table = $this->table('Users');
        $table->addColumn('account', 'string', array('limit' => 100, 'null' => false, 'comment' => '名称'))
            ->addColumn('password', 'string', array('limit' => 32, 'null' => false, 'comment' => '密码'))
            ->addColumn('name', 'string', array('limit' => 32,  'null' => false, 'comment' => '名称'))
            ->addColumn('role', 'string', array('limit' => 32,  'null' => false, 'comment' => '角色'))
            ->addColumn('parent_id', 'integer', array('limit' => 20,  'null' => false, 'comment' => '上级id'))
            ->addColumn('auth', 'string', array('limit' => 255, 'comment' => '验证'))
            ->addIndex(array('account'), array('unique' => true))
            ->addTimestamps()
    }
 public function down()
    {
        $exists = $this->hasTable('Users');
        if ($exists) {
            $this->dropTable('Users');
        }

    }
php think migrate:run	执行migrate(up方法 change不存在)
php think migrate:rollback	回滚上次操作(down方法 change不存在)

Seeder (虚拟数据的创建)

php think seed:create UserSeeder

创建一个 UserSeeder 文件,创建成功之后你可以在 database/seeds 目录下面看到

$rows = [];
        for ($i = 0; $i < 100; $i++) {
            $rows[] = [
                'nickname' => mt_rand(10000, 99999),
                'email' => mt_rand(10000, 99999).'@qq.com',
                'password' => md5('123456'),
            ];
        }
        $this->table('Users')->insert($rows)->save();
php think seed:run	//执行插入数据

若想使用中文随机数据可以参考Faker

https://www.jianshu.com/p/af5b95ac1ed5

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值