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?

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! But if i will use some database only in read mode, setter methods in entity classes are necessary to use?
Need I to specify field types in entity classes for existing database (which in read only mode)?
Yes because Doctrine need this informations to correctly hydrate your entities...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.