3
votes

I'm working with a Symfony3 app and I want to set up multiple connections to different databases.

I've been looking around and I found out the documentation about entityManagers and DB connections. My config.yml is configured as follows:

config.yml

doctrine:
dbal:
    default_connection: default
    connections:
            default:
                    driver:   pdo_mysql
                    host:     "%database_host%"
                    port:     "%database_port%"
                    dbname:   "%database_name%"
                    user:     "%database_user%"
                    password: "%database_password%"
                    charset:  UTF8
                    mapping_types:
                      enum: string
            other:
                    driver:   pdo_mysql
                    host:     "%database_host2%"
                    port:     "%database_port2%"
                    dbname:   "%database_name2%"
                    user:     "%database_user2%"
                    password: "%database_password2%"
                    charset:  UTF8
                    mapping_types:
                      enum: string
orm:
    dql:
         string_functions:
                DAY:   DoctrineExtensions\Query\Mysql\Day
                MONTH: DoctrineExtensions\Query\Mysql\Month
                YEAR:  DoctrineExtensions\Query\Mysql\Year
    auto_generate_proxy_classes: "%kernel.debug%"
    naming_strategy: doctrine.orm.naming_strategy.underscore
    auto_mapping: true

So now I do can access to my database like this:

$con2 = $this->get('doctrine.dbal.other_connection');
$orders = $con2->fetchAll('SELECT * FROM orders');

But what I really need is to configure a second orm-mapping connection which will allow me to interact with entities instead of dealing with the second database directly. So again as the documentation says I added under the doctrine orm label:

orm:
    dql:
         string_functions:
                DAY:   DoctrineExtensions\Query\Mysql\Day
                MONTH: DoctrineExtensions\Query\Mysql\Month
                YEAR:  DoctrineExtensions\Query\Mysql\Year
    auto_generate_proxy_classes: "%kernel.debug%"
    naming_strategy: doctrine.orm.naming_strategy.underscore
    auto_mapping: true

    default_entity_manager: default
    entity_managers:
        default:
            connection: default
            mappings:
                AppBundle:  ~      
        other:
            connection: other
            mappings:
                OtherBundle: ~

This throws an exception:

ParseException in Parser.php line 296: Unable to parse at line 78 (near " entity_managers:").

How should I configure my config.yml to allow orm-mapping for my second database connection? Should I delete the dql label and use it only under a certain entity manager label?

3
The error is related to a YAML syntax error. However, this is not reflected by the example config you are showing here. You may want to show your complete config file. - xabbuh

3 Answers

3
votes

check the doc here for full reference of the configuration about doctrine.

Check the indentation of the dql function etc.

Probably us are using the Shortened Configuration Syntax where all options available can be placed directly under doctrine.orm config level.

Hope this help

2
votes

Try this one:

doctrine:
    orm:
        auto_generate_proxy_classes: true
        entity_managers:
            default:
                mappings:
                    AppBundle: ~
                naming_strategy: doctrine.orm.naming_strategy.underscore
                dql:
                   string_functions:
                       DAY: DoctrineExtensions\Query\Mysql\Day
            other:
                mappings:
                    OtherBundle: ~
0
votes

I solved the problem. Thanks for the answers because they were pretty much the solution. I just had to use the labels in their correct levels.

Just in case it can be useful for someone, I'll post what I've done:

config.yml

 orm:

    entity_managers:
        default:
            mappings:
                AppBundle: ~
            naming_strategy: doctrine.orm.naming_strategy.underscore
            auto_mapping: true
            dql:
                         string_functions:
                                DAY:   DoctrineExtensions\Query\Mysql\Day
                                MONTH: DoctrineExtensions\Query\Mysql\Month
                                YEAR:  DoctrineExtensions\Query\Mysql\Year
        other:
            mappings:
                OtherBundle: ~
            naming_strategy: doctrine.orm.naming_strategy.underscore

    auto_generate_proxy_classes: "%kernel.debug%"

Once the (other) entity manager is created you just can use it within a controller as you'd do with the default em just by specifying the entity manager name you are using.

$orders = $this->get('doctrine')
        ->getRepository('OtherBundle:Orders', 'other')
        ->findAll();

Now you're ready to go.

Thanks for your help.