• tp5 数据库迁移工具 migrate&seed


    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 thinkmigrationMigrator;

    use
    thinkmigrationdbColumn; 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 thinkmigrationSeeder; 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(); } } }
  • 相关阅读:
    Tomcat设置web 点击劫持 X-Frame-Options
    Spring boot 防止 xss 攻击 和 LDAP注入
    Spring Boot
    Spring Boot 跨域设置
    SpringBoot 引入redis
    Mybatis常用增删改以及过程的xml配置文件编写
    关于mybatis的一些注意点
    盒子的定位和布局
    vuex随学笔记
    JavaScript数据结构
  • 原文地址:https://www.cnblogs.com/YC-L/p/12635731.html
Copyright © 2020-2023  润新知