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(); } } }
原文地址:https://www.cnblogs.com/YC-L/p/12635731.html
时间: 2024-10-08 20:21:57