Skip to content

Instantly share code, notes, and snippets.

@chalasr
Created June 9, 2016 13:34
Show Gist options
  • Save chalasr/dc5cd628c9d16e255d4e2ea7b2f36b72 to your computer and use it in GitHub Desktop.
Save chalasr/dc5cd628c9d16e255d4e2ea7b2f36b72 to your computer and use it in GitHub Desktop.
DoctrineMigration - Container-aware migration with EntityManager example
<?php
namespace Application\Migrations;
use AppBundle\Entity\Foo;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Auto-generated Migration: Please modify to your needs!
*/
class Version20160609093209 extends AbstractMigration implements ContainerAwareInterface
{
/** @var ContainerInterface */
private $container;
/**
* @param ContainerInterface|null $container
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
public function preUp(Schema $schema)
{
$em = $this->container->get('doctrine.orm.entity_manager');
$fooRepository = $em->getRepository(Foo::class);
// Enjoy
$em->flush();
}
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');
// ...
}
/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment