Symfony5 Doctrine2 Migration Cheatsheet
Command line options
-
Run with debug info (SQL Queries)
-
php bin/console doctrine:migrations:migrate -vv
-
-
Run without asking
-
php bin/console doctrine:migrations:migrate --no-interaction
-
-
Return to the previous state
-
php bin/console doctrine:migrations:migrate prev --no-interaction
-
Use PDO in Migration
-
public function up(Schema $schema): void { $pdo = $this->connection->getNativeConnection(); $q = "SELECT foo FROM bar"; $pdo->query($q); }
-
@see https://www.ullright.org/ullWiki/show/php-pdo-cheat-sheet-for-symfony1
Use Entity Manager in Migration
- config/services.yaml
-
# Make Doctrine entity manager service public Doctrine\ORM\EntityManagerInterface: public: true alias: 'doctrine.orm.default_entity_manager'
-
-
final class Version20230510101711 extends AbstractMigration implements ContainerAwareInterface { use ContainerAwareTrait; public function getDescription(): string { return 'Describe things...'; } public function up(Schema $schema): void { // this up() migration is auto-generated, please modify it to your needs } public function postUp(Schema $schema): void { $em = $this->container->get(EntityManagerInterface::class); ... } }
Use repository
- As above, and then:
-
public function postUp(Schema $schema): void { $em = $this->container->get(EntityManagerInterface::class); $fooRepository = $em->getRepository(FooRepository::class); }
-