Answer the question
In order to leave comments, you need to log in
How to transfer users from one database to another using migration?
Hello everyone,
Tell me how can I use 2 connections correctly, and using a simple example, please explain how to transfer users from one database to another?
Here's what I achieved. Created a second array [db2] => connection to another database
Created a migration
class m151223_062347_data_transfer extends Migration
{
public function setDb($db)
{
$this->db = $db;
}
public function safeUp()
{
$this->db = $this->setDb('db');
$this->db2 = $this->setDb('db2');
$shema = $this->db2->schema->getTableSchema('users');
Answer the question
In order to leave comments, you need to log in
For these purposes, there is a batchInsert
As an example:
public function up()
{
Yii::$app->db->createCommand()->batchInsert('users',
['id', 'name', 'email', 'role'], [
[1, 'Vanya', '[email protected]', 10],
[2, 'Kolya', '[email protected]', 1],
[3, 'Dima', '[email protected]', 1],
])->execute();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question