2014-07-18 5 views
4

우리는 모든 참조 값을 사용하여 데이터베이스를 시드하기 위해 일련의 데이터 픽스처를 구축했습니다. 스키마 업데이트를 관리하기 위해 DoctrineMigrationsBundle도 사용하고 있습니다. 초기 스키마 마이그레이션 클래스 내에서 조명기로드를 트리거하여 추가 스키마 업데이트를 실행하기 전에 시스템이 채워지도록하려고합니다.Symfony2 조명기를 마이그레이션 클래스에서로드하는 방법은 무엇입니까?

migration classes container aware을 만들 수 있다는 문서에서 발견되었지만 데이터 고정 장치를 호출/실행하는 방법을 생각해 낼 수는 없습니다. Stackoverflow 또는 Google을 통해 좋은 답변을 찾지 못했습니다. 누구든지이 일을하고 올바른 방향으로 나를 가리킬 수 있습니까? (또는 스키마 마이그레이션과 함께 시드 데이터를 관리하는 더 좋은 방법에 대한 제안 사항이 있음). 감사.

이것은 Symfony를 사용하고 있습니다. 버전 : 2.4

답변

5

이것은 흥미로운 질문입니다. "더러운"해결책을 찾았지만 잘 작동합니다.


namespace Application\Migrations; 

use Doctrine\DBAL\Migrations\AbstractMigration; 
use Doctrine\DBAL\Schema\Schema; 
use Symfony\Component\DependencyInjection\ContainerInterface; 
use Symfony\Component\DependencyInjection\ContainerAwareInterface; 

class Version20140811164659 extends AbstractMigration implements ContainerAwareInterface 
{ 
    private $container; 

    public function setContainer(ContainerInterface $container = null) 
    { 
     $this->container = $container; 
    } 

    public function up(Schema $schema) 
    { 
     // ... your code here 
    } 

    public function postUp(Schema $schema) 
    { 
     // here you have to define fixtures dir 
     $this->loadFixtures('src/Acme/BlogBundle/DataFixtures/ORM'); 
    } 

    public function down(Schema $schema) 
    { 
     // ... your code here 
    } 

    public function loadFixtures($dir, $append = true) 
    { 
     $kernel = $this->container->get('kernel'); 
     $application = new \Symfony\Bundle\FrameworkBundle\Console\Application($kernel); 
     $application->setAutoExit(false); 

     //Loading Fixtures 
     $options = array('command' => 'doctrine:fixtures:load', "--fixtures" => $dir, "--append" => (boolean) $append); 
     $application->run(new \Symfony\Component\Console\Input\ArrayInput($options)); 
    } 
} 

이 해결 방법은 "up"마이그레이션 후 콘솔 명령 php app/console doctrine:fixtures:load --fixtures=src/Acme/BlogBundle/DataFixtures/ORM --append을 실행하는 것입니다. 영어로는 유감스럽게 생각합니다. 명확한 해결책을 찾을 수 있다면 공유하십시오.)

1

저는이 문제를 해결하기 위해 마이그레이션 클래스를 만들었습니다. 이 코드는 본질적으로 교리에서 영감을 얻습니다 : fixtures : load 명령.

<?php 

namespace AppBundle\Migrations; 

use Doctrine\Common\DataFixtures\Executor\ORMExecutor; 
use Doctrine\Common\DataFixtures\FixtureInterface; 
use Doctrine\Common\DataFixtures\Purger\ORMPurger; 
use Doctrine\DBAL\Migrations\AbstractMigration; 
use Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader; 
use Symfony\Component\DependencyInjection\ContainerAwareInterface; 
use Symfony\Component\DependencyInjection\ContainerInterface; 

abstract class AbstractFixturesAwareMigration extends AbstractMigration implements ContainerAwareInterface 
{ 
    private $container; 
    private $fixtures; 

    public function setContainer(ContainerInterface $container = null) 
    { 
     $this->container = $container; 
    } 

    protected function getContainer() 
    { 
     return $this->container; 
    } 

    protected function addFixture(FixtureInterface $fixture) 
    { 
     if(null === $this->fixtures) { 
      $this->fixtures = new ContainerAwareLoader($this->getContainer()); 
     } 

     $this->fixtures->addFixture($fixture); 

     return $this; 
    } 

    protected function executeFixtures($em = null, $append = true, $purgeMode = ORMPurger::PURGE_MODE_DELETE) 
    { 
     $em = $this->getContainer()->get('doctrine')->getManager($em); 
     $purger = new ORMPurger($em); 
     $purger->setPurgeMode($purgeMode); 
     $executor = new ORMExecutor($em, $purger); 
     $executor->execute($this->fixtures->getFixtures(), $append); 
     $this->fixtures = null; 

     return $this; 
    } 
} 

사용법은 매우 간단합니다 :

<?php 

namespace Application\Migrations; 

use AppBundle\Migrations\AbstractFixturesAwareMigration 
use Doctrine\DBAL\Schema\Schema; 
use Symfony\Component\DependencyInjection\ContainerAwareInterface; 
use Symfony\Component\DependencyInjection\ContainerInterface; 

/** 
* Auto-generated Migration: Please modify to your needs! 
*/ 
class Version20170726102103 extends AbstractFixturesAwareMigration 
{   
    /** 
    * @param Schema $schema 
    */ 
    public function up(Schema $schema) 
    { 
     // this up() migration is auto-generated, please modify it to your needs 
     // [...] 
    } 

    public function postUp(Schema $schema) 
    { 
     // LoadMyData can be any fixture class 
     $this->addFixture(new LoadMyData()); 
     $this->executeFixtures(); 
    }   

    /** 
    * @param Schema $schema 
    */ 
    public function down(Schema $schema) 
    { 
     // this down() migration is auto-generated, please modify it to your needs 
     // [...] 
    } 
}