簡體   English   中英

Symfony2 - bundle中的doctrine連接配置

[英]Symfony2 - doctrine connection configuration in bundle

我有一個使用我的附加包的項目。 此捆綁包連接到其他數據庫,我需要配置另一個數據庫。

我希望在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');
    }
}

在我看來,一切看起來都不錯,但是當我試圖運行時,我已經溝通:

沒有擴展能夠加載“doctrine”的配置

你可以聲明第二個驅動程序,具體到你的包(稱為SecondBundle在使用你的例子PrependExtensionInterface

第一重命名config.yml文件SecondBundledoctrine.yml (或未被任何其他名稱config.yml )。

現在更改你的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)
    {    
        $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);
        }

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

secondBundle連接將現在當您啟用束被自動注冊。

您可以將額外的配置添加到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 a non-quoted string cannot start with @ or ` (reserved) nor with a scalar indicator (| or >)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM