我正在使用phinx来管理数据库,我需要从数据库中收集数据并将其插入到另一个数据库中。
我已经在配置文件中定义了两个环境,如下所示:
'environments' => [
'default_database' => 'current',
'current' => [
'adapter' => 'mysql',
'host' => '127.0.0.1',
'name' => 'old',
'user' => 'root',
'pass' => '*****',
'port' => '3306',
'charset' => 'utf8',
],
'new' => [
'adapter' => 'mysql',
'host' => '127.0.0.1',
'name' => 'new',
'user' => 'root',
'pass' => '*****',
'port' => '3306',
'charset' => 'utf8',
]
],
我想要达到的目标是这样的:
public function up () {
// The environment is 'current' by default
$data = $this->fetchAll("SELECT * FROM old_table WHERE x");
// Change environment somehow
$this->environment('new')
$this->table('new_table')->insert($data);
}
这可能吗?我在official documentation上找不到任何内容。
答案 0 :(得分:0)
他们在执行迁移时会查找phinx代码
public function executeMigration(MigrationInterface $migration, $direction = MigrationInterface::UP, $fake = false)
{
$direction = ($direction === MigrationInterface::UP) ? MigrationInterface::UP : MigrationInterface::DOWN;
$migration->setMigratingUp($direction === MigrationInterface::UP);
$startTime = time();
$migration->setAdapter($this->getAdapter());
您已在迁移中使用了setAdaptor,因此您可以使用它。本来宁愿在评论中写这个,因为它并不是真正的答案,但我没有足够的字符