M
M
Mikha Pankratov2015-12-23 10:16:41
Yii
Mikha Pankratov, 2015-12-23 10:16:41

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

1 answer(s)
V
Vladimir Korovin, 2015-12-23
@frmax

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();
    }

Before that, you first get an array of all users by specifying asArray() in the request, and pass this array to batchInsert. And of course, it remains for you to track the order of the fields.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question