Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create 2 connections (mysql and postgresql) with Doctrine2 in Symfony2

I'm new to Symfony2. And my question quite simple. I would use 2 connections to DB at different host and driver in one bundle.

Could you help me with this?

like image 840
Neka Avatar asked Feb 23 '23 04:02

Neka


1 Answers

You can do something like:

doctrine:
    dbal:
        default_connection: alpha
        connections:
            alpha:
                driver:     pdo_mysql
                host:       localhost
                dbname:     alpha
                user:       root
                charset:    UTF8
            beta:
                driver:     pdo_pgsql
                host:       localhost
                dbname:     beta
                user:       root
                charset:    UTF8
    orm:
        auto_generate_proxy_classes: %kernel.debug%
        entity_managers:
            alpha:
                connection: alpha
            beta:
                connection: beta

You see, we declare two connections in the dbal section and two entity managers in the orm one.

After that, you can use both:

$emAlpha = $this->getDoctrine()->getEntityManager('alpha');
$emBeta  = $this->getDoctrine()->getEntityManager('beta');

As the alpha one was defined as the default one, you can access it without specifying its name:

$emAlpha = $this->getDoctrine()->getEntityManager();
like image 129
Herzult Avatar answered May 02 '23 18:05

Herzult