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 //運行填充器
#創建遷移類,首字母必須為大寫 php think migrate:create Users
參考: http://docs.phinx.org/en/latest/index.html
注意:
- Please be aware that when a
changemethod exists, Phinx will automatically ignore theupanddownmethods. 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支持在數據庫表上創建外鍵約束。讓我們在示例表中添加一個外鍵:
<?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(請參閱下面的示例)。
<?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 |
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 檢查列是否存在
<?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
}
}
}
