4

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?

Neka
  • 1,574
  • 4
  • 22
  • 36

1 Answers1

8

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();
Herzult
  • 3,429
  • 22
  • 15
  • Thanks! But if i will use some database only in read mode, setter methods in entity classes are necessary to use? – Neka Dec 13 '11 at 03:21
  • Need I to specify field types in entity classes for existing database (which in read only mode)? – Neka Dec 13 '11 at 03:30
  • Yes because Doctrine need this informations to correctly hydrate your entities... – Herzult Dec 18 '11 at 09:59