gpt4 book ai didi

php - 如何编写迁移以使用 phinx 插入记录?

转载 作者:可可西里 更新时间:2023-11-01 06:27:52 24 4
gpt4 key购买 nike

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

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

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

The AbstractMigration Class All Phinx migrations extend from the AbstractMigration class. This class provides the necessary support to create your database migrations. Database migrations can transform your database in many ways such as creating new tables, inserting rows, adding indexes and modifying columns.

这可能吗?我该怎么做?

最佳答案

正如 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

关于php - 如何编写迁移以使用 phinx 插入记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28265669/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com