如何编写迁移以使用 phinx 插入记录?
How can I write migrations to insert records using phinx?
我正在使用 phinx 来处理新项目的迁移,现在我需要创建一个新的 table 并向其中插入一些行,我有:
$tableStatus = $this->table('status');
$tableStatus->addColumn('code', 'string');
$tableStatus->addColumn('description', 'string');
$tableStatus->save();
这添加了新的 table 但我在文档中找不到如何插入行,但它似乎是可能的:
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.
有可能吗?我该怎么做?
你可以的。阅读文档以获取更多信息。
http://docs.phinx.org/en/latest/migrations.html#executing-queries
use Phinx\Migration\AbstractMigration;
class MyNewMigration extends AbstractMigration
{
/**
* Migrate Up.
*/
public function up()
{
// execute()
$count = $this->execute('insert into users(id, name) values (5, "john")');
}
/**
* Migrate Down.
*/
public function down()
{
}
}
has a using CakePHP 3's Migrations 插件:
public function up() {
// Save records to the newly created schema
$UsersTable = TableRegistry::get('Users');
$user = $UsersTable->newEntity();
$user->name = 'Joe Bloggs';
$user->email = 'joe@example.com';
$UsersTable->save($user);
}
正如 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);
- 运行 此命令生成 StatusMigration class:
php vendor/bin/phinx create StatusMigration
- 像这样编辑该文件:
<?php
use Phinx\Migration\AbstractMigration;
class StatusMigration extends AbstractMigration
{
public function change()
{
$this->table('status')
->addColumn('code', 'string')
->addColumn('description', 'string')
->create();
}
}
- 您可以使用 Phinx 的数据库播种机制将行插入 tables。首先 运行 以下命令:
php vendor/bin/phinx seed:create StatusSeeder
它将在 Phinx 的种子文件夹中生成 StatusSeeder.php 文件。
- 编辑StatusSeader.php喜欢:
use Phinx\Seed\AbstractSeed;
class StatusSeeder extends AbstractSeed
{
public function run()
{
$data = [
['code' => 'c1', 'description' => 'Some description'],
['code' => 'c2', 'description' => 'Another description'],
];
$this->table('status')
->insert($data)
->save();
}
}
- 现在,运行 按照以下命令创建 table 和种子数据:
php vendor/bin/phinx migrate
php vendor/bin/phinx seed:run
我正在使用 phinx 来处理新项目的迁移,现在我需要创建一个新的 table 并向其中插入一些行,我有:
$tableStatus = $this->table('status');
$tableStatus->addColumn('code', 'string');
$tableStatus->addColumn('description', 'string');
$tableStatus->save();
这添加了新的 table 但我在文档中找不到如何插入行,但它似乎是可能的:
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.
有可能吗?我该怎么做?
你可以的。阅读文档以获取更多信息。
http://docs.phinx.org/en/latest/migrations.html#executing-queries
use Phinx\Migration\AbstractMigration;
class MyNewMigration extends AbstractMigration
{
/**
* Migrate Up.
*/
public function up()
{
// execute()
$count = $this->execute('insert into users(id, name) values (5, "john")');
}
/**
* Migrate Down.
*/
public function down()
{
}
}
public function up() {
// Save records to the newly created schema
$UsersTable = TableRegistry::get('Users');
$user = $UsersTable->newEntity();
$user->name = 'Joe Bloggs';
$user->email = 'joe@example.com';
$UsersTable->save($user);
}
正如 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);
- 运行 此命令生成 StatusMigration class:
php vendor/bin/phinx create StatusMigration
- 像这样编辑该文件:
<?php
use Phinx\Migration\AbstractMigration;
class StatusMigration extends AbstractMigration
{
public function change()
{
$this->table('status')
->addColumn('code', 'string')
->addColumn('description', 'string')
->create();
}
}
- 您可以使用 Phinx 的数据库播种机制将行插入 tables。首先 运行 以下命令:
php vendor/bin/phinx seed:create StatusSeeder
它将在 Phinx 的种子文件夹中生成 StatusSeeder.php 文件。
- 编辑StatusSeader.php喜欢:
use Phinx\Seed\AbstractSeed;
class StatusSeeder extends AbstractSeed
{
public function run()
{
$data = [
['code' => 'c1', 'description' => 'Some description'],
['code' => 'c2', 'description' => 'Another description'],
];
$this->table('status')
->insert($data)
->save();
}
}
- 现在,运行 按照以下命令创建 table 和种子数据:
php vendor/bin/phinx migrate
php vendor/bin/phinx seed:run