thinkphp 迁移数据库 -Phinx 简单说明文档
1 | php think |
migrate
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 //状态查看
optimize
optimize:autoload Optimizes PSR0 and PSR4 packages to be loaded wit
h classmaps too, good for production.//朗读优化PSR0和PSR4软件包,也可以通过类映射加载,有利于生产。
optimize:config Build config and common file cache.//构建公共配置文件缓存
optimize:route Build route cache.//构建路由缓存
optimize:schema Build database schema cache. //构建数据库构建缓存
seed
seed:create Create a new database seeder //创建新的数据填充器
seed:run Run database seeders //运行填充器
1 2 | #创建迁移类,首字母必须为大写 php think migrate:create Users |
参考: http://docs.phinx.org/en/latest/index.html
注意:
- Please be aware that when a
change
method exists, Phinx will automatically ignore theup
anddown
methods. If you need to use these methods it is recommended to create a separate migration file.
当change方法存在时,将会自动忽略up /down 方法,如果想要生效需要单独创建文件; - When creating or updating tables inside a
change()
method you must use the Tablecreate()
andupdate()
methods. Phinx cannot automatically determine whether asave()
call is creating a new table or modifying an existing one.
在change()
方法内创建或更新表时,必须使用表create()
和update()
方法。Phinx无法自动确定save()
是创建新表还是修改现有表。 - Phinx 只能撤销 createTable / renameTable / addColumn / renameColumn / addIndex / addForeignKey 命令;
—— Phinx支持在数据库表上创建外键约束。让我们在示例表中添加一个外键:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <?php use Phinx\Migration\AbstractMigration; class MyNewMigration extends AbstractMigration { /** * Migrate Up. */ public function up() { $table = $this ->table( 'tags' ); $table ->addColumn( 'tag_name' , 'string' ) ->save(); $refTable = $this ->table( 'tag_relationships' ); $refTable ->addColumn( 'tag_id' , 'integer' , [ 'null' => true]) ->addForeignKey( 'tag_id' , 'tags' , 'id' , [ 'delete' => 'SET_NULL' , 'update' => 'NO_ACTION' ]) ->save(); } /** * Migrate Down. */ public function down() { } } |
——有效列类型
In addition, the MySQL adapter supports enum
, set
and blob
column types.
In addition, the Postgres adapter supports json
, jsonb
, uuid
, cidr
, inet
and macaddr
column types (PostgreSQL 9.3 and above).
以下是有效的列选项:对于任何列类型:
decimal
columns:
For enum
and set
columns:

For integer
and biginteger
columns:

For timestamp
columns:

朗读您可以使用addTimestamps()方法将created_at和updated_at时间戳添加到表中。此方法还允许您提供替代名称。可选的第三个参数允许您更改要添加的列的时区选项。此外,您可以使用addTimestampsWithTimezone()方法,该方法是addTimestamps()的别名,它始终将第三个参数设置为true(请参阅下面的示例)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?php use Phinx\Migration\AbstractMigration; class MyNewMigration extends AbstractMigration { /** * Migrate Change. */ public function change() { // Use defaults (without timezones) $table = $this ->table( 'users' )->addTimestamps()->create(); // Use defaults (with timezones) $table = $this ->table( 'users' )->addTimestampsWithTimezone()->create(); // Override the 'created_at' column name with 'recorded_at'. $table = $this ->table( 'books' )->addTimestamps( 'recorded_at' )->create(); // Override the 'updated_at' column name with 'amended_at', preserving timezones. // The two lines below do the same, the second one is simply cleaner. $table = $this ->table( 'books' )->addTimestamps(null, 'amended_at' , true)->create(); $table = $this ->table( 'users' )->addTimestampsWithTimezone(null, 'amended_at' )->create(); } } |
For boolean
columns:

For string
and text
columns:

For foreign key definitions:
Limit Option and MySQL
When using the MySQL adapter, additional hinting of database column type can be made for integer
, text
and blob
columns. Using limit
with one the following options will modify the column type accordingly:
Limit | Column Type |
---|---|
BLOB_TINY | TINYBLOB |
BLOB_REGULAR | BLOB |
BLOB_MEDIUM | MEDIUMBLOB |
BLOB_LONG | LONGBLOB |
TEXT_TINY | TINYTEXT |
TEXT_REGULAR | TEXT |
TEXT_MEDIUM | MEDIUMTEXT |
TEXT_LONG | LONGTEXT |
INT_TINY | TINYINT |
INT_SMALL | SMALLINT |
INT_MEDIUM | MEDIUMINT |
INT_REGULAR | INT |
INT_BIG | BIGINT |
1 2 3 4 5 6 7 8 9 10 | use Phinx\Db\Adapter\MysqlAdapter; //... $table = $this ->table( 'cart_items' ); $table ->addColumn( 'user_id' , 'integer' ) ->addColumn( 'product_id' , 'integer' , [ 'limit' => MysqlAdapter::INT_BIG]) ->addColumn( 'subtype_id' , 'integer' , [ 'limit' => MysqlAdapter::INT_SMALL]) ->addColumn( 'quantity' , 'integer' , [ 'limit' => MysqlAdapter::INT_TINY]) ->create(); |
Get a column list
$columns = $this->table('users')->getColumns();
Get a column by name
$column = $this->table('users')->getColumn('email');
Checking whether a column exists 检查列是否存在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?php use Phinx\Migration\AbstractMigration; class MyNewMigration extends AbstractMigration { /** * Change Method. */ public function change() { $table = $this ->table( 'user' ); $column = $table ->hasColumn( 'username' ); if ( $column ) { // do something } } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性