Faery
Faery

Reputation: 4650

Can I set more that one database in Symfony2?

I was wondering if I can set more than one database in Symfony2 (I am using it with Doctrine2) because when I want to see another application it says that there is an error and such table doesn't exist.

My question is whether every time when I want to view different applications, I have to change my database (I do it in the parameters.ini file) or there is another way?

Upvotes: 0

Views: 99

Answers (2)

AlterPHP
AlterPHP

Reputation: 12717

Basically, you have to define (at least) 1 EntityManager per database AND (at least) 1 connection per database. This is achieved by configuration in app/config/config.yml :

# Doctrine Configuration
doctrine:
    dbal:
        default_connection: conn1
        connections:
            conn1:
                driver:   %database_driver%
                host:     %database_host%
                port:     %database_port%
                dbname:   %database_name%
                user:     %database_user%
                password: %database_password%
                charset:  UTF8
            conn2:
                driver:   %database2_driver%
                host:     %database2_host%
                port:     %database2_port%
                dbname:   %database2_name%
                user:     %database2_user%
                password: %database2_password%
                charset:  UTF8

    orm:
        auto_generate_proxy_classes: %kernel.debug%

        default_entity_manager:   em1
        entity_managers:
            em1:
                connection:       conn1
                mappings:
                    XxxBundle1: ~
            em2:
                connection:       conn2
                mappings:
                    XxxBundle2: ~

As you must tell which bundle contains mapped Entities for a given EntityManager, you have to create (at least) 1 bundle per database. As I wrote it here, don't forget to erase auto_mapping parameter in orm part !

Official documentation documentation on this subject

Upvotes: 4

Luke Adamczewski
Luke Adamczewski

Reputation: 395

This can be useful advice how to achieve this http://www.theodo.fr/blog/2011/09/symfony2-working-with-multiple-databases/

Upvotes: 2

Related Questions