Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony2-捆绑包中的条令连接配置_Php_Symfony_Doctrine - Fatal编程技术网

Php Symfony2-捆绑包中的条令连接配置

Php Symfony2-捆绑包中的条令连接配置,php,symfony,doctrine,Php,Symfony,Doctrine,我有一个使用我的附加包的项目。这个包连接到其他数据库,我需要另一个数据库的配置 我想在2个配置文件中有这个连接 主配置: # ROOT/app/config/config.yml: doctrine: dbal: default_connection: default connections: default: driver: "%database_driver%"

我有一个使用我的附加包的项目。这个包连接到其他数据库,我需要另一个数据库的配置

我想在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');
    }
}
在我看来,一切看起来都不错,但当我尝试运行此功能时,我必须:

没有能够加载“条令”配置的扩展


您可以将额外的配置添加到
app/config/config.yml
中的导入中,以便将其合并到完整的
config

app/config/config.yml


由于
自3.0版以来,非引号字符串不能以@或`(保留)开头,也不能以标量指示符(|或>)开头,因此使用引号进行更新。

您可以使用
PrependExtensionInterface
声明特定于捆绑包的第二个驱动程序(在示例中名为
SecondBundle

首先将
SecondBundle
中的
config.yml
文件重命名为
doctor.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
连接现在将在您启用捆绑包时自动注册。

看起来您的捆绑包的配置在配置条令之前正在读取和运行。我建议您查看相应的容器和配置优先级设置。是否要导入con图,但如何在没有第二个实体管理器的情况下使用第二个数据库?实体管理器与显示的错误无关。错误是由于在扩展名中加载配置文件时未加载
DoctrineBundle
。因为问题中根本没有提到实体管理器,这可能意味着连接是直接使用的,而不是实体管理器。我明白你的意思,但我认为使用条令烹饪书的响应是一个很好的方法,可以帮助他使用两个数据库。关系管理器是另一回事。我将第二个参数放在
\doctor\Bundle\DoctrineBundle\Regis中的
getRepository
函数上试试
doctrine类。@Nicolas,我觉得没有必要,因为OP清楚地知道如何创建配置,因为它在问题中是正确的。问题是如何导入该配置以便可以使用。使用该配置创建另一个实体管理器除了可能会导致更多错误外,什么都没有做它所依赖的连接将不存在。您的答案比所选答案好得多。我使用了它!需要注意的是:您不需要调用$container->getExtensions(),也不需要使用$doctrineExtension=$extensions['doctrine'].回答得好though@Jaimz谢谢你的评论,很高兴它对我有所帮助。我将根据你的评论删除未使用的变量:)
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']);
    }
}