插入数据

Phinx 可以很简单的帮助你在表中插入数据。尽管这个功能也在 seed 中实现了。你也可以在迁移脚本中实现插入数据。

  1. <?php
  2. use Phinx\Migration\AbstractMigration;
  3. class NewStatus extends AbstractMigration
  4. {
  5. /**
  6. * Migrate Up.
  7. */
  8. public function up()
  9. {
  10. // inserting only one row
  11. $singleRow = [
  12. 'id' => 1,
  13. 'name' => 'In Progress'
  14. ]
  15. $table = $this->table('status');
  16. $table->insert($singleRow);
  17. $table->saveData();
  18. // inserting multiple rows
  19. $rows = [
  20. [
  21. 'id' => 2,
  22. 'name' => 'Stopped'
  23. ],
  24. [
  25. 'id' => 3,
  26. 'name' => 'Queued'
  27. ]
  28. ];
  29. // this is a handy shortcut
  30. $this->insert('status', $rows);
  31. }
  32. /**
  33. * Migrate Down.
  34. */
  35. public function down()
  36. {
  37. $this->execute('DELETE FROM status');
  38. }
  39. }

不能在 change() 方法中使用插入数据,只能在 up()down() 中使用