Skip to content
Advertisement

Working with two entity managers in the same bundle in Symfony2

I’m trying to work with two entity managers for the same bundle. My configuration is like this:

orm:

    default_entity_manager:   default
    entity_managers:
        electra:
            connection:       electra
            mappings:
                XXDemoBundle: ~
        default:
            connection:       default
            mappings:
                XXDemoBundle: ~

Is there any way to tell which entities belong to which entity manager? It crashes now if I want to work with a table which doesn’t belong to the default entity manager.

  • UPDATE

here is my configuration for the connection:

doctrine:
    dbal:
        default_connection:       default
        connections:
            default:
                dbname:           old_project
                user:             root
                password:         123123
                host:             1.1.1.1
                port:             1
            electra:
                dbname:           electra
                user:             root
                password:         123123
                host:             2.2.2.2
                port:             2

orm:
    default_entity_manager:   electra
    entity_managers:
        electra:
            connection:       electra
            mappings:
                XXDemoBundle: ~


        default:
            connection:       default
            mappings:
                XXDemoBundle: ~

Advertisement

Answer

For using multiple entitymanager in same bundle you have to config mapping options for each entitymanager.

http://symfony.com/doc/current/reference/configuration/doctrine.html

Exemple off config file

doctrine:
    dbal:
        default_connection:   default
        connections:
            default:
                driver:   %database_driver%
                host:     %database_host%
                port:     %database_port%
                dbname:   %database_name%
                user:     %database_user%
                password: %database_password%
                charset:  UTF8
            second:
                driver:   %database_sqlite_driver%
                host:     ~
                port:     ~
                dbname:   %database_sqlite_shop_name%
                path:     %database_sqlite_shop_name%
                user:     ~
                password: ~
                charset:  UTF8

    orm:
        auto_generate_proxy_classes: %kernel.debug%
        default_entity_manager:   default
        entity_managers:
            default:
                connection:       default
                mappings:
                    YourBundle:
                      # you must specify the type
                      type:     "annotation"    
                      # The directory for entity (relative to bundle path)
                      dir:      "Entity/FirstDb"        
                      #the prefix 
                      prefix:   "YourBundleEntityFirstDb" 
            shop:
                connection:       second
                mappings:
                    YourBundle:
                      type: "annotation"
                      #here the second path where entity for the connection stand
                      dir: "Entity/SecondDb" 
                      #the prefix
                      prefix: "YourBundleEntitySecondDb" 

You can now use console for managing your db with the –em parameter

Ex : update database for shop entitymanager

php app/console doctrine:schema:update --em=shop

Read mapping information from YourBundleEntitySecondDb

Ex : update database for default entitymanager

php app/console doctrine:schema:update   

Read mapping information from YourBundleEntityFirstDb

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement