zoukankan      html  css  js  c++  java
  • 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(); } } }
  • 相关阅读:
    使用LAMP创建基于wordpress的个从博客网站
    【solr基础教程之一】Solr相关知识点串讲
    solr源码导入eclipse
    【Nutch2.2.1基础教程之2.2】集成Nutch/Hbase/Solr构建搜索引擎之二:内容分析
    设置secureCRT中vim的字体颜色
    何时使用hadoop fs、hadoop dfs与hdfs dfs命令
    Hadoop常见异常及其解决方案
    Java HashMap遍历的两种方式
    android json 解析简单实例
    Android客户端与数据库交互数据的简单学习
  • 原文地址:https://www.cnblogs.com/YC-L/p/12635731.html
Copyright © 2011-2022 走看看