2

我有app/config.yml这个:

# Doctrine Configuration
doctrine:
    dbal:
        driver:   %database_driver%
        host:     %database_host%
        port:     %database_port%
        dbname:   %database_name%
        user:     %database_user%
        password: %database_password%

    orm:
        auto_generate_proxy_classes: %kernel.debug%
        auto_mapping: true

现在我创建了我的包,我希望它覆盖其中的一些配置:

doctrine:
    orm:
        resolve_target_entities:
              Acme\UserBundle\Interfaces\User: Acme\MyBundle\Entity\User

我想在不更改的情况下添加此配置app/config.yml

4

2 回答 2

3

我通过添加编译器通道找到了解决方案:

class OrmResolverCompilerPass implements CompilerPassInterface
{

    public function process(ContainerBuilder $container)
    {
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../../Resources/config'));
        $loader->load('orm.yml');

        $def = $container->findDefinition('doctrine.orm.listeners.resolve_target_entity');
        foreach ($container->getParameter('resolve_target_entities') as $name => $implementation) {
            $def->addMethodCall('addResolveTargetEntity', array( $name, $implementation, array() ));
        }
        $def->addTag('doctrine.event_listener', array('event' => 'loadClassMetadata'));
    }

}

我们还需要在Bundle 内orm.yml的目录中创建。Resources/config

最后在 controllerBundle 类中注册编译器传递:

class MyAcmeBundle extends Bundle
{

    public function build(ContainerBuilder $container)
    {
        parent::build($container);
        $container->addCompilerPass(new OrmResolverCompilerPass());
    }

}
于 2013-08-05T14:06:15.237 回答
2

您现在可以PrependExtensionInterface为此使用:

class ThirisCartLogicCatalogExtension extends Extension implements PrependExtensionInterface
{    
    public function prepend(ContainerBuilder $container)
    {
        $config = Yaml::parse(file_get_contents(__DIR__.'/../Resources/config/config.yml'));

        foreach ($config as $key => $configuration) {
            $container->prependExtensionConfig($key, $configuration);
        }
    }
}

使用此代码,您的包中的 config.yml 将无条件地合并到全局配置中。

请注意,您加载的配置文件不会以这种方式验证语法。

于 2015-11-08T17:49:38.117 回答