Doctrine

Schema Migration

Data Migration

After creating your migration (this could be an empty migration if required) you can migrate data using the postUp() function. It's important to note that within postUp() you can't use $this->addSql() and must use $this->connection->exec() directly.

This is the best post I found covering this: post

A short example would be:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    // ...

    private const ARTICLE_INSERT = "INSERT INTO article (timestamp, content) VALUES ('%s', '%s')";

    private const ARTICLE_DATA = [
        [
            'timestamp' => '2001-01-01 00:00:00',
            'content'   => 'First post'
        ],
        [
            'timestamp' => '2001-01-02 00:00:00',
            'content'   => 'Second post'
        ]
    ];

    // ...

    public function postUp(Schema $schema)
    {
        foreach (self::ARTICLE_DATA as $data) {
            $query = sprintf(
                self::ARTICLE_INSERT,
                $data['timestamp'],
                $data['content']
            );

            $this->connection->exec($query);
        }
    }

    // ...