我在用着phinx http://docs.phinx.org/为了处理新项目的迁移,现在我需要创建一个新表并向其中插入一些行,我有:
$tableStatus = $this->table('status');
$tableStatus->addColumn('code', 'string');
$tableStatus->addColumn('description', 'string');
$tableStatus->save();
这会添加新表,但我在文档中找不到如何插入行,但似乎有可能:
AbstractMigration 类 所有 Phinx 迁移都从
抽象迁移类。此类提供必要的支持
创建您的数据库迁移。数据库迁移可以改变
您的数据库有多种方式,例如创建新表,插入
行,添加索引和修改列。
有可能的?我该怎么做?
正如 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);
More 信息在这里 https://github.com/robmorgan/phinx/pull/639#event-480065126
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)