码迷,mamicode.com
首页 > 数据库 > 详细

tp5 数据库迁移工具 migrate&seed

时间:2020-04-08 19:21:02      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:remember   安装   填充   cti   reac   color   ocs   base   uniq   

tp5.0时使用migrate工具,composer安装

composer require topthink/think-migration=1.*

注意,tp5.0对应1版本的migration工具,tp5.1及以上对应2版本的migration工具

查看指令

php think

技术图片

 

 创建migrate文件(用来建表)

php migrate create:SqUserLogs

在applicaton/database/migrations 文件夹下会创建一个文件 20200318060333_sq_user_logs.php

<?php

use think\migration\Migrator;

use
think\migration\db\Column; class SqUserLogs 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(‘sq_user_logs‘,array(‘engine‘=>‘InnoDB ‘));//InnoDB //MyISAM $table ->addColumn(‘uid‘, ‘integer‘, array(‘comment‘=>‘用户在users表中的id‘)) ->addColumn(‘access_time‘,‘integer‘,array(‘comment‘=>‘出入时间‘)) ->addColumn(‘access_status‘,‘boolean‘,array(‘default‘=>0,‘comment‘=>‘出入状态,0表示出门,1表示回家‘)) ->addIndex(array(‘id‘), array(‘unique‘=>true)) ->create(); } /** * Migrate Up. */ public function up() { // 创建表格时 } /** * Migrate Down. */ public function down() { // 删除表格时 } }

官方文档不能看了,可以通过源码学习使用,在 项目目录/vendor/topthink/thinkmigration/src/command 中可以找到源码

创建seed文件(用来填充测试数据)

php think seed:create SqUserLogs

在application/database/seeds 文件夹下会创建文件 SqUserLogs.php

<?php

use think\migration\Seeder; class SqUserLogs extends Seeder { /** * 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() { $data = [ [1, 1], [2, 2], [1, 1], [3, 2], [1, 1], [2, 2] ]; foreach($data as $v){ $this->table(‘sq_user_logs‘)->insert([ ‘uid‘=>$v[0], ‘access_time‘=>time() + 360, ‘access_status‘=>$v[1] ])->save(); } } }

 

tp5 数据库迁移工具 migrate&seed

标签:remember   安装   填充   cti   reac   color   ocs   base   uniq   

原文地址:https://www.cnblogs.com/YC-L/p/12635731.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!