Created
June 9, 2016 13:34
-
-
Save chalasr/dc5cd628c9d16e255d4e2ea7b2f36b72 to your computer and use it in GitHub Desktop.
DoctrineMigration - Container-aware migration with EntityManager example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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