数据表操作

Table 对象

Table对象是Phinx中最有用的API之一。它可以让你方便的用 PHP 代码操作数据库。我们可以通过 table() 方法取到Table对象。

  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('tableName');
  11. }
  12. /**
  13. * Migrate Down.
  14. */
  15. public function down()
  16. {
  17. }
  18. }

接下来,你可以用 Table 对象提供的方法来操作修改数据库了

Save 方法

当操作 Table 对象时,Phinx 提供了一些操作来改变数据库。

如果你不清楚该使用什么操作,建议你使用 save 方法。它将自动识别插入或者更新操作,并将改变应用到数据库。

创建一个表

使用 Table 可以很简单的创建一个表,现在我们创建一个存储用户信息的表

  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->addColumn('username', 'string', array('limit' => 20))
  12. ->addColumn('password', 'string', array('limit' => 40))
  13. ->addColumn('password_salt', 'string', array('limit' => 40))
  14. ->addColumn('email', 'string', array('limit' => 100))
  15. ->addColumn('first_name', 'string', array('limit' => 30))
  16. ->addColumn('last_name', 'string', array('limit' => 30))
  17. ->addColumn('created', 'datetime')
  18. ->addColumn('updated', 'datetime', array('null' => true))
  19. ->addIndex(array('username', 'email'), array('unique' => true))
  20. ->save();
  21. }
  22. /**
  23. * Migrate Down.
  24. */
  25. public function down()
  26. {
  27. }
  28. }

字段使用 addColumn() 方法创建。并且用 addIndex() 方法将 username 和 email 设置为唯一。最后调用 save() 提交我们的改变。

Phinx 会为每个表自动创建一个自增的主键字段 id

id 选项会自动创建一个唯一字段,primary_key 选项设置哪个字段为主键。 primary_key 选项默认值是 id 。这2个选项可以设置为false。

如果要指定一个主键,你可以设置 primary_key 选项,关闭自动生成 id 选项,并使用2个字段定义为主键。

  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('followers', array('id' => false, 'primary_key' => array('user_id', 'follower_id')));
  11. $table->addColumn('user_id', 'integer')
  12. ->addColumn('follower_id', 'integer')
  13. ->addColumn('created', 'datetime')
  14. ->save();
  15. }
  16. /**
  17. * Migrate Down.
  18. */
  19. public function down()
  20. {
  21. }
  22. }

单独设置 primary_key 选项并不能开启 AUTO_INCREMENT 选项。如果想简单的改变主键名,我们只有覆盖 id 字段名即可。

  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('followers', array('id' => 'user_id'));
  11. $table->addColumn('follower_id', 'integer')
  12. ->addColumn('created', 'timestamp', array('default' => 'CURRENT_TIMESTAMP'))
  13. ->save();
  14. }
  15. /**
  16. * Migrate Down.
  17. */
  18. public function down()
  19. {
  20. }
  21. }

另外,MySQL adapter 支持以下选项

选项 描述
comment 给表设置注释
engine 定义表的引擎(默认 InnoDB)
collation 定义表的语言(默认 utf8-general-ci)

字段类型

字段类型如下:

  • 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 或者更高)

表是否存在

可以使用 hasTable() 判断表是否存在。

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

删除表

可以用 dropTable() 方法删除表。这时可以在 `down()` 方法中重新创建表,可以在回滚的时候恢复。

  1. <?php
  2. use Phinx\Migration\AbstractMigration;
  3. class MyNewMigration extends AbstractMigration
  4. {
  5. /**
  6. * Migrate Up.
  7. */
  8. public function up()
  9. {
  10. $this->dropTable('users');
  11. }
  12. /**
  13. * Migrate Down.
  14. */
  15. public function down()
  16. {
  17. $users = $this->table('users');
  18. $users->addColumn('username', 'string', array('limit' => 20))
  19. ->addColumn('password', 'string', array('limit' => 40))
  20. ->addColumn('password_salt', 'string', array('limit' => 40))
  21. ->addColumn('email', 'string', array('limit' => 100))
  22. ->addColumn('first_name', 'string', array('limit' => 30))
  23. ->addColumn('last_name', 'string', array('limit' => 30))
  24. ->addColumn('created', 'datetime')
  25. ->addColumn('updated', 'datetime', array('null' => true))
  26. ->addIndex(array('username', 'email'), array('unique' => true))
  27. ->save();
  28. }
  29. }

重命名表名

可以用 rename() 方法重命名表名。

  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->rename('legacy_users');
  12. }
  13. /**
  14. * Migrate Down.
  15. */
  16. public function down()
  17. {
  18. $table = $this->table('legacy_users');
  19. $table->rename('users');
  20. }
  21. }