thinkphp之migration 迁移文件的使用
创建迁移类,首字母必须为大写
php think migrate:create Users
可以看到目录下有新文件 .\database\migrations\20161117144043_users.php 使用实例
<?php use Phinx\Migration\AbstractMigration; class Users extends AbstractMigration { /** * Change Method. */ public function change() { // create the table $table = $this->table(‘users‘,array(‘engine‘=>‘MyISAM‘)); $table->addColumn(‘username‘, ‘string‘,array(‘limit‘ => 15,‘default‘=>‘‘,‘comment‘=>‘用户名,登陆使用‘)) ->addColumn(‘password‘, ‘string‘,array(‘limit‘ => 32,‘default‘=>md5(‘123456‘),‘comment‘=>‘用户密码‘)) ->addColumn(‘login_status‘, ‘boolean‘,array(‘limit‘ => 1,‘default‘=>0,‘comment‘=>‘登陆状态‘)) ->addColumn(‘login_code‘, ‘string‘,array(‘limit‘ => 32,‘default‘=>0,‘comment‘=>‘排他性登陆标识‘)) ->addColumn(‘last_login_ip‘, ‘integer‘,array(‘limit‘ => 11,‘default‘=>0,‘comment‘=>‘最后登录IP‘)) ->addColumn(‘last_login_time‘, ‘datetime‘,array(‘default‘=>0,‘comment‘=>‘最后登录时间‘)) ->addColumn(‘is_delete‘, ‘boolean‘,array(‘limit‘ => 1,‘default‘=>0,‘comment‘=>‘删除状态,1已删除‘)) ->addIndex(array(‘username‘), array(‘unique‘ => true)) ->create(); } /** * Migrate Up. */ public function up() { } /** * Migrate Down. */ public function down() { } }
以上为thinkphp手册提供的 介绍
======================================================分割线============================================================================
1.创建数据表
使用 up 和 down方法
Up 方法up方法会在Phinx执行迁移命令时自动执行,并且该脚本之前并没有执行过。你应该将修改数据库的方法写在这个方法里。
Down 方法
down方法会在Phinx执行回滚命令时自动执行,并且该脚本之前已经执行过迁移。你应该将回滚代码放在这个方法里。
Save 方法当操作 Table 对象时,Phinx 提供了一些操作来改变数据库。如果你不清楚该使用什么操作,,建议你使用 save 方法。它将自动识别插入或者更新操作,并将改变应用到数据库。
创建数据表时推荐使用 这三个方法!
字段操作 字段类型字段类型如下:
biginteger
binary
boolean
date
datetime
decimal
float
integer
string
text
time
timestamp
uuid
另外,MySQL adapter 支持 enum 、set 、blob 和 json (json 需要 MySQL 5.7 或者更高)
Postgres adapter 支持 smallint 、json 、jsonb 和 uuid (需要 PostgresSQL 9.3 或者更高)
2 字段选项以下是有效的字段选项:
所有字段:
选项描述limit 为string设置最大长度
length limit 的别名
default 设置默认值
null 允许空
after 指定字段放置在哪个字段后面
comment 字段注释
decimal 类型字段:
选项描述precision 和 scale 组合设置精度
scale 和 precision 组合设置精度
signed 开启或关闭 unsigned 选项(仅适用于 MySQL)
enum 和 set 类型字段:
选项描述values 用逗号分隔代表值
integer 和 biginteger 类型字段:
选项描述identity 开启或关闭自增长
signed 开启或关闭 unsigned 选项(仅适用于 MySQL)
timestamp 类型字段:
选项描述default 设置默认值 (CURRENT_TIMESTAMP)
update 当数据更新时的触发动作 (CURRENT_TIMESTAMP)
timezone 开启或关闭 with time zone 选项
可以在标准使用 addTimestamps() 方法添加 created_at 和 updated_at 。方法支持自定义名字 。使用addSoftDelete 添加软删除字段
更多请参考
https://tsy12321.gitbooks.io/phinx-doc/content/writing-migrations-working-with-tables.html3 执行命令
php think migrate:run
执行所有迁移文件中的run方法
php think migrate:rollback
回滚命令 执行最后执行的down命令
thinkphp之migration 迁移文件的使用
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/41934.html
- 上一篇:netcore 版本升级 导致的cookie验证失败
- 下一篇:JS简单数据类型