Calamity Jane
Calamity Jane

Reputation: 2686

How can I setup a second db connection with doctrine2 and symfony?

I use doctrine ORM in a Symfony 2.8 project.

My project contains several Bundles. For one Bundle, which generates Reports I want to use the db server with the slave replication as not to stress the master db server. How to set this up?

What I tried so far:

In the config.yml

Doctrine Configuration

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


            slave:
                driver:   pdo_mysql
                host:     '%database_host_slave%'
                port:     '%database_port_slave%'
                dbname:   '%database_name_slave%'
                user:     '%database_user_slave%'
                password: '%database_password_slave%'
                charset:  UTF8

Here I created my second db connection with the values stored in my parameters.yml.

I seem to get the orm configuration I tried to setup in the same file not correctly. Let my first explain what I need: I have a "ReportingBundle" which runs a console command. The entity manager is only needed to provide the proper authorization to the needed db server. The queries itself are oure SQL and I don't use the entities.

my service.yml for this bundle:

services:
    myproject.reporting.service.csv_report_attachment:
        class: Myproject\ReportingBundle\Service\DefaultCsvReportAttachmentService
        arguments: ['@doctrine.orm.slave_entity_manager', '@logger', '@myproject.reporting.service.php_template_engine', 'reportingHtmlMailTemplate.php']

Now my non functioning orm setup in the config.yml:

ORM configuration

    orm:
        auto_generate_proxy_classes: '%kernel.debug%'
        # naming_strategy: doctrine.orm.naming_strategy.underscore
        default_entity_manager: default
        entity_managers:

            slave:
                connection: slave
                mappings:
                    MyprojectReportingBundle: ~

            default:
                connection: default
                auto_mapping : true
                metadata_cache_driver: redis
                query_cache_driver: redis
                result_cache_driver: redis

This results in

    [Doctrine\ORM\ORMException]                             
    Unknown Entity namespace alias 'MyprojectReportingBundle'. 

I tried to follow the documentation here: http://symfony.com/doc/current/reference/configuration/doctrine.html#custom-mapping-entities-in-a-bundle

Question: What is the correct syntax, so that my query is run on the slave server instead of the default server?

Upvotes: 0

Views: 1363

Answers (1)

Jake Litwicki
Jake Litwicki

Reputation: 232

I believe you have everything configured correctly. However, you need to make sure the Entity classes are defined in the Bundle namespace. So for example you'd want to have all the entities defined within Myproject\ReportingBundle\Entity in your example.

Upvotes: 0

Related Questions