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:
// ...
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);
}
}
// ...