Database Migrations
Database migrations are handled by Doctrine Migrations. When writing your migrations it is strongly recommended to not use the entity manager to persist records. If you need to change schema or structure this can make managing older migrations much more difficult.
However: if you do need the entity manager you must add a factory override in the doctrine_migrations.yaml
file and have the following:
<?php declare(strict_types=1);
namespace App\Resources\Factories;
use Doctrine\Migrations\AbstractMigration;
use Doctrine\Migrations\Version\DbalMigrationFactory;
use Doctrine\Migrations\Version\MigrationFactory;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class MigrationFactoryDecorator
*
* From: https://symfony.com/doc/master/bundles/DoctrineMigrationsBundle/index.html#migration-dependencies
*
* @package App\Resources\Factories
* @subpackage App\Resources\Factories\MigrationFactoryDecorator
*/
class MigrationFactoryDecorator implements MigrationFactory
{
private MigrationFactory $factory;
private ContainerInterface $container;
public function __construct(DbalMigrationFactory $migrationFactory, ContainerInterface $container)
{
$this->factory = $migrationFactory;
$this->container = $container;
}
public function createVersion(string $migrationClassName): AbstractMigration
{
$instance = $this->factory->createVersion($migrationClassName);
if ($instance instanceof ContainerAwareInterface) {
$instance->setContainer($this->container);
}
return $instance;
}
}
The same goes for any other services you would like to inject into the migrations.