Kamil P Kamil P - 3 years ago 206
PHP Question

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.

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"

Answer Source

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

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download