我正在使用phinx处理新项目上的迁移,现在我需要创建一个新表并向其中插入一些行,我有:

$tableStatus = $this->table('status');
$tableStatus->addColumn('code', 'string');
$tableStatus->addColumn('description', 'string');
$tableStatus->save();

这添加了新表,但是我在文档中找不到如何插入行,但是似乎可行:



有可能的?我该怎么做?

最佳答案

正如igrossiter所指出的,有一个方法可以实现,该方法的名称为insert。

use Phinx\Migration\AbstractMigration;

class NewStatus extends AbstractMigration
{
    protected $statusId = 1234; //It'd be nice to use an entity constant instead of magic numbers, but that's up to you.
    protected $statusName = 'In Progress';

    /**
    * Migrate Up.
    */
    public function up()
    {
        $columns = ['id', 'name'];
        $data = [[$this->statusId, $this->statusName]];
        $table = $this->table('status');
        $table->insert($columns, $data);
        $table->saveData();
    }

    /**
    * Migrate Down.
    */
    public function down()
    {
        $this->execute('Delete from status where id = ' . $this->statusId);
    }
}

编辑截至2015年12月2日

此方法的签名在将来的稳定版本中将变为
$data = [
    ['id' => 1, 'name' => 'foo'],
    ['id' => 2, 'name' => 'bar']
];
$table = $this->table('status');
$table->insert($data);

更多info here

09-10 08:52
查看更多