2016-03-17 54 views
4

我有使用我的附加包的項目。這個包連接到其他數據庫,我需要配置另一個數據庫。Symfony2 - 包中的學說連接配置

我想在2個配置文件中有這個連接。

主要配置:

# ROOT/app/config/config.yml: 
doctrine: 
    dbal: 
     default_connection: default 
     connections: 
      default: 
       driver: "%database_driver%" 
       host:  "%database_host%" 
       port:  "%database_port%" 
       dbname: "%database_name%" 
       user:  "%database_user%" 
       password: "%database_password%" 
       charset: UTF8 

捆綁配置:

# src/SecondBundle/Resources/config/config.yml 
doctrine: 
    dbal: 
     connections: 
      secondBundle: 
       driver: "%secondBundle.database_driver%" 
       host:  "%secondBundle.database_host%" 
       port:  "%secondBundle.database_port%" 
       dbname: "%secondBundle.database_name%" 
       user:  "%secondBundle.database_user%" 
       password: "%secondBundle.database_password%" 
       charset: UTF8 

捆綁擴展文件:

class SecondBundleExtension extends Extension 
{ 
    /** 
    * {@inheritdoc} 
    */ 
    public function load(array $configs, ContainerBuilder $container) 
    { 
     $configuration = new Configuration(); 
     $config = $this->processConfiguration($configuration, $configs); 

     $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); 
     $loader->load('config.yml'); 
    } 
} 

在我看來一切正常,但是當我試圖運行這我有溝通:

There is no extension able to load the configuration for "doctrine"

+0

看來您的包的配置正在讀取並在配置Doctrine之前運行。我建議你看看[CompilerPasses](http://symfony.com/doc/current/cookbook/service_container/compiler_passes.html)來相應地設置容器和配置優先級。 – hasumedic

回答

2

您可以將您的額外配置添加到app/config/config.yml中的導入中,以便將它合併到完整config中。

應用程序/配置/ config.yml

imports: 
    - { resource: parameters.yml } 
    - { resource: security.yml } 
    - { resource: '@SecondBundle/Resources/config/config.yml' } 

與報價更新由於事實a non-quoted string cannot start with @ or ` (reserved) nor with a scalar indicator (| or >)自從3.0版本。

+0

好的導入配置,但如何使用第二個數據庫沒有第二個實體管理器? – Nicolas

+0

實體管理器與顯示的錯誤無關。該錯誤是由於在配置文件被加載到擴展中時「DoctrineBundle」尚未加載。由於實體經理在這個問題中沒有提到,所以這可能意味着連接直接與實體經理相對使用。 – qooplmao

+0

我明白了你的意思,但是我認爲面向學習食譜的迴應是一個很好的幫助他的方法,就是兩個數據庫 – Nicolas

1

可以聲明第二個驅動程序,具體到你的包(使用PrependExtensionInterface

重命名首先你config.yml文件SecondBundledoctrine.yml(或任何其他名稱不是config.yml)命名在你的榜樣SecondBundle

變化現在您SecondBundleExtension類是這樣的:

use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; 
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; 
use Symfony\Component\Yaml\Exception\ParseException; 
use Symfony\Component\Yaml\Parser as YamlParser; 
// ... 

class SecondBundleExtension extends Extension implements PrependExtensionInterface 
{ 
    public function load(array $configs, ContainerBuilder $container) 
    { 
     // ... 
    } 

    public function prepend(ContainerBuilder $container) 
    { 
     $extensions = $container->getExtensions(); 

     $yamlParser = new YamlParser(); 

     try { 
      $doctrineConfig = $yamlParser->parse(
       file_get_contents(__DIR__.'/../Resources/config/doctrine.yml') 
      ); 
     } catch (ParseException $e) { 
      throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e); 
     } 

     $doctrineExtension = $extensions['doctrine']; 
     $container->prependExtensionConfig('doctrine', $doctrineConfig['doctrine']); 
    } 
} 

你當您啓用捆綁時,連接現在將自動註冊。