首先通过 composer 安装
composer require topthink/think-migration:^1 PS:注意版本 tp5.0 支持到 版本1
在命令行下运行查看帮助,可以看到新增的命令
php think
.
.
.
make
make:controller Create a new resource controller class
make:model Create a new model class
migrate
migrate:breakpoint Manage breakpoints
migrate:create Create a new migration
migrate:rollback Rollback the last or to a specific migration
migrate:run Migrate the database
migrate:status Show migration status
.
.
.
创建迁移类,大写驼峰
php think migrate:create CreatForm1Table
PS: 命名安装规范, 例如 2014_10_12_000000_create_users_table.php 2022_03_28_172515_add_avatar_and_introduction_to_users_table 2022_03_29_100658_seed_categories_data
可以看到目录下有新文件
代码编写如下
删除了之前的 change() 方法
<?php
use think\migration\Migrator;
class CreateForm1Table extends Migrator
{
public function up()
{
$table = $this->table('form1')->setComment('数据提交');
$table->addColumn('name', 'string', array('default' => '', 'comment' => '姓名'))
->addColumn('phone', 'string', array('limit' => 20, 'default' => '', 'comment' => '联系方式'))
->addColumn('site', 'string', array('limit' => 250, 'default' => '', 'comment' => '来源页面'))
->addColumn('remark', 'text', array('comment' => '备注'))
->addColumn('createtime', 'integer', array('limit' => 10, 'null' => true, 'comment' => '创建时间'))
->addColumn('updatetime', 'integer', array('limit' => 10, 'null' => true, 'comment' => '更新时间'))
->addColumn('deletetime', 'integer', array('limit' => 10, 'null' => true, 'comment' => '删除时间'))
->addColumn('status', 'enum', array('values' => '0,1,2', 'default' => '0',
'comment' => '状态值:0=未回访,1=已回访,2=无效'))
->save();
}
public function down()
{
$this->dropTable('form1');
}
}
执行
php think migrate:run
回滚
php think migrate:rollback
|