Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"

like image 366
Kamil P Avatar asked Mar 17 '16 10:03

Kamil P


2 Answers

You can declare the second driver that is specific to your bundle (named SecondBundle in your example using the PrependExtensionInterface.

Rename first your config.yml file in SecondBundle to doctrine.yml (or any other name that is not config.yml).

Change now your SecondBundleExtension class like this:

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.

like image 53
Michaël Perrin Avatar answered Nov 12 '22 10:11

Michaël Perrin


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.

like image 45
qooplmao Avatar answered Nov 12 '22 09:11

qooplmao