2014-04-23 1 views
4

기본 제공되는 명령 행에서 기본 Doctrine을 사용하는 경우 Symfony와 Doctrine 및 app/console을 사용하여 사용할 수없는 두 가지 명령을 사용할 수 있습니다.Symfony app/console에서 Doctrine DBAL 명령 사용 가능

dbal 
    dbal:import Import SQL file(s) directly to Database. 
    dbal:run-sql Executes arbitrary SQL directly from the command line. 

Symfony의 app/console에서 이러한 명령을 사용할 수있는 방법이 있습니까?

+0

이 명령은'bin/doctrine'을 통해 사용할 수 있어야합니다. – kix

+0

@kix; 나는 이해. bin/doctrine의 문제는'--env' 매개 변수를 이해하지 못한다는 것입니다. 이 명령을'app/console'에서 사용할 수있게하고 싶습니다. – Luke

답변

7

이 문제를 해결할 수있는 방법을 찾았습니다. 아니면 명령을 사용하는 방법 일 수도 있습니다.

자신의 번들 중 하나 (또는 ​​전적으로 귀하에게 달려 있음)에 Command을 추가하면 Doctrine 명령을 하위 클래스로 간단하게 지정할 수 있습니다. 예 : dbal:import 명령은 다음과 같은 사용 가능합니다 : 당신이 볼 수 있듯이

namespace Acme\Bundle\AcmeBundle\Command\Doctrine; 

use Symfony\Component\Console\Input\InputInterface; 
use Symfony\Component\Console\Output\OutputInterface; 

use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper; 
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper; 

class ImportCommand extends \Doctrine\DBAL\Tools\Console\Command\ImportCommand { 

    protected function execute(InputInterface $input, OutputInterface $output) 
    { 
     $container = $this->getApplication()->getKernel()->getContainer(); 

     $doctrine = $container->get('doctrine'); 

     $em = $doctrine->getEntityManager(); 
     $db = $em->getConnection(); 

     $helperSet = $this->getHelperSet(); 
     $helperSet->set(new ConnectionHelper($db), 'db'); 
     $helperSet->set(new EntityManagerHelper($em), 'em'); 

     parent::execute($input, $output); 
    } 

} 

, 우리는 단순히 원래의 명령을 서브 클래스입니다. 데이터베이스 구성이 Symfony에 의해 관리되기 때문에 컨테이너를 통해 엔티티 관리자를 가져와야합니다. HelperSet을 업데이트하면 실행을 상위 클래스로 다시 전달합니다.

3

cli-config.php을 프로젝트의 루트 디렉토리에 넣습니다. 그러면 php vendor/bin/doctrine--env 매개 변수를 사용할 수 있습니다. 그것은 Symfony의 app/console 파일에 의해 "영감을받은"것입니다.

Doctrine < = 2.3에 대한 참고입니다. 최신 버전의 경우 the docs을 참조하십시오.

<?php 
require_once __DIR__.'/app/bootstrap.php.cache'; 
require_once __DIR__.'/app/AppKernel.php'; 

use Symfony\Component\Console\Input\ArgvInput; 

$input = new ArgvInput(); 
$env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev'); 
$debug = getenv('SYMFONY_DEBUG') !== '0' && !$input->hasParameterOption(array('--no-debug', '')) && $env !== 'prod'; 

$kernel = new AppKernel($env, $debug); 
$kernel->boot(); 

$em = $kernel->getContainer()->get('doctrine.orm.entity_manager'); 

$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
    'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()), 
    'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em) 
)); 

return $helperSet; 
1

교리 명령 (버전 1.6.4 이후)에 DoctrineBundle 통해 심포니 프로젝트에서 사용할 수 있습니다.

이제 php bin/console doctrine:database:import [files]을 실행할 수 있습니다.

관련 문제