字段操作

字段类型

字段类型如下:

  • biginteger
  • binary
  • boolean
  • date
  • datetime
  • decimal
  • float
  • integer
  • string
  • text
  • time
  • timestamp
  • uuid

另外,MySQL adapter 支持 enumsetblobjsonjson 需要 MySQL 5.7 或者更高)

Postgres adapter 支持 smallintjsonjsonbuuid (需要 PostgresSQL 9.3 或者更高)

字段选项

以下是有效的字段选项:

所有字段:

选项 描述
limit 为string设置最大长度
length limit 的别名
default 设置默认值
null 允许空
after 指定字段放置在哪个字段后面
comment 字段注释

decimal 类型字段:

选项 描述
precision 和 scale 组合设置精度
scale 和 precision 组合设置精度
signed 开启或关闭 unsigned 选项(仅适用于 MySQL)

enumset 类型字段:

选项 描述
values 用逗号分隔代表值

integerbiginteger 类型字段:

选项 描述
identity 开启或关闭自增长
signed 开启或关闭 unsigned 选项(仅适用于 MySQL)

timestamp 类型字段:

选项 描述
default 设置默认值 (CURRENT_TIMESTAMP)
update 当数据更新时的触发动作 (CURRENT_TIMESTAMP)
timezone 开启或关闭 with time zone 选项

可以在标准使用 addTimestamps() 方法添加 created_atupdated_at 。方法支持自定义名字。

  1. <?php
  2. use Phinx\Migration\AbstractMigration;
  3. class MyNewMigration extends AbstractMigration
  4. {
  5. /**
  6. * Migrate Change.
  7. */
  8. public function change()
  9. {
  10. // Override the 'updated_at' column name with 'amended_at'.
  11. $table = $this->table('users')->addTimestamps(null, 'amended_at')->create();
  12. }
  13. }

boolean 类型字段:

选项 描述
signed 开启或关闭 unsigned 选项(仅适用于 MySQL)

stringtext 类型字段:

选项 描述
collation 设置字段的 collation (仅适用于 MySQL)
encoding 设置字段的 encoding (仅适用于 MySQL)

外键定义:

选项 描述
update 设置一个触发器当数据更新时
delete 设置一个触发器当数据删除时

你可以通过可选的第三个数组型参数将上述选项中的一个或者多个传递到任意字段中。

Limit 选项 和 PostgreSQL

当使用 PostgreSQL adapter,一些其他的字段类型可以通过 integer 创建。使用下面的 Limit 选项。

Limit 字段类型
INT_SMALL SMALLINT
  1. use Phinx\Db\Adapter\PostgresAdapter;
  2. //...
  3. $table = $this->table('cart_items');
  4. $table->addColumn('user_id', 'integer')
  5. ->addColumn('subtype_id', 'integer', array('limit' => PostgresAdapter::INT_SMALL))
  6. ->create();

Limit 选项 和 MySQL

当使用 MySQL adapter,一些其他的字段类型可以通过 integertextblob 创建。使用下面的 Limit 选项

Limit 字段类型
BLOG_TINY TINYBLOB
BLOB_REGULAR BLOG
BLOG_MEDIUM MEDIUMELOG
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. use Phinx\Db\Adapter\MysqlAdapter;
  2. //...
  3. $table = $this->table('cart_items');
  4. $table->addColumn('user_id', 'integer')
  5. ->addColumn('product_id', 'integer', array('limit' => MysqlAdapter::INT_BIG))
  6. ->addColumn('subtype_id', 'integer', array('limit' => MysqlAdapter::INT_SMALL))
  7. ->addColumn('quantity', 'integer', array('limit' => MysqlAdapter::INT_TINY))
  8. ->create();

获取字段List

调用 getColumns() 可以获得表的所有字段。该方法返回 Column 类的数组。如下例子

  1. <?php
  2. use Phinx\Migration\AbstractMigration;
  3. class ColumnListMigration extends AbstractMigration
  4. {
  5. /**
  6. * Migrate Up.
  7. */
  8. public function up()
  9. {
  10. $columns = $this->table('users')->getColumns();
  11. ...
  12. }
  13. /**
  14. * Migrate Down.
  15. */
  16. public function down()
  17. {
  18. ...
  19. }
  20. }

检查字段是否存在

调用 hasColumn() 方法判断指定字段是否存在

  1. <?php
  2. use Phinx\Migration\AbstractMigration;
  3. class MyNewMigration extends AbstractMigration
  4. {
  5. /**
  6. * Change Method.
  7. */
  8. public function change()
  9. {
  10. $table = $this->table('user');
  11. $column = $table->hasColumn('username');
  12. if ($column) {
  13. // do something
  14. }
  15. }
  16. }

重命名字段

调用 renameColumn() 方法重命名字段

  1. <?php
  2. use Phinx\Migration\AbstractMigration;
  3. class MyNewMigration extends AbstractMigration
  4. {
  5. /**
  6. * Migrate Up.
  7. */
  8. public function up()
  9. {
  10. $table = $this->table('users');
  11. $table->renameColumn('bio', 'biography');
  12. }
  13. /**
  14. * Migrate Down.
  15. */
  16. public function down()
  17. {
  18. $table = $this->table('users');
  19. $table->renameColumn('biography', 'bio');
  20. }
  21. }

在一个字段后创建字段

可以使用 after 选项指定字段创建的位置

  1. <?php
  2. use Phinx\Migration\AbstractMigration;
  3. class MyNewMigration extends AbstractMigration
  4. {
  5. /**
  6. * Change Method.
  7. */
  8. public function change()
  9. {
  10. $table = $this->table('users');
  11. $table->addColumn('city', 'string', array('after' => 'email'))
  12. ->update();
  13. }
  14. }

删除字段

使用 removeColumn() 方法删除字段

  1. <?php
  2. use Phinx\Migration\AbstractMigration;
  3. class MyNewMigration extends AbstractMigration
  4. {
  5. /**
  6. * Migrate up.
  7. */
  8. public function up()
  9. {
  10. $table = $this->table('users');
  11. $table->removeColumn('short_name')
  12. ->save();
  13. }
  14. }

指定字段Limit

使用 limit 选项设置字段的最大长度

  1. <?php
  2. use Phinx\Migration\AbstractMigration;
  3. class MyNewMigration extends AbstractMigration
  4. {
  5. /**
  6. * Change Method.
  7. */
  8. public function change()
  9. {
  10. $table = $this->table('tags');
  11. $table->addColumn('short_name', 'string', array('limit' => 30))
  12. ->update();
  13. }
  14. }

修改字段属性

使用 changeColumn() 方法修改字段属性

  1. <?php
  2. use Phinx\Migration\AbstractMigration;
  3. class MyNewMigration extends AbstractMigration
  4. {
  5. /**
  6. * Migrate Up.
  7. */
  8. public function up()
  9. {
  10. $users = $this->table('users');
  11. $users->changeColumn('email', 'string', array('limit' => 255))
  12. ->save();
  13. }
  14. /**
  15. * Migrate Down.
  16. */
  17. public function down()
  18. {
  19. }
  20. }