简体   繁体   English

Symfony2 - bundle中的doctrine连接配置

[英]Symfony2 - doctrine connection configuration in bundle

I have project which uses my additional bundle. 我有一个使用我的附加包的项目。 This bundle connects to other database and I need configuration for another database. 此捆绑包连接到其他数据库,我需要配置另一个数据库。

I want to have this connections in 2 config files. 我希望在2个配置文件中建立此连接。

main config: 主配置:

# 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

bundle config: 捆绑配置:

# 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

Bundle Extension file: 捆绑扩展文件:

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');
    }
}

In my opinion everything looks OK, but when I'm trying to run this I have communicate: 在我看来,一切看起来都不错,但是当我试图运行时,我已经沟通:

There is no extension able to load the configuration for "doctrine" 没有扩展能够加载“doctrine”的配置

You can declare the second driver that is specific to your bundle (named SecondBundle in your example using the PrependExtensionInterface . 你可以声明第二个驱动程序,具体到你的包(称为SecondBundle在使用你的例子PrependExtensionInterface

Rename first your config.yml file in SecondBundle to doctrine.yml (or any other name that is not config.yml ). 第一重命名config.yml文件SecondBundledoctrine.yml (或未被任何其他名称config.yml )。

Change now your SecondBundleExtension class like this: 现在更改你的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']);
    }
}

Your secondBundle connection will now be automatically registered when you enable your bundle. secondBundle连接将现在当您启用束被自动注册。

You can add your extra config to the imports in your app/config/config.yml so that it is merged into the full config . 您可以将额外的配置添加到app/config/config.yml以便将其合并到完整config

app/config/config.yml 应用程序/配置/ config.yml

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

Updated with quotes due to the fact that a non-quoted string cannot start with @ or ` (reserved) nor with a scalar indicator (| or >) since version 3.0. 更新了引号,因为从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