Tuesday, October 4, 2011

Configuration trick for multiple entity managers in Symfony2

First time I tried to define multiple entity managers in config.yml, i got some errors...


I didn't strictly followed symfony cookbook but based my configuration on the reference about Doctrine configuration. So I let "auto_mapping" parameter with false value, and I got following error :

InvalidConfigurationException: Unrecognized options "auto_mapping" under "doctrine.orm"

So if you want to implement many entity managers, don't forget to erase "auto_mapping" param in your config files (config.yml and derived ones if needed).
doctrine: 
    orm: 
        auto_mapping: false 
        default_entity_manager:   em1 
        entity_managers: 
            em1: 
                connection:       conn1 
                mappings: 
                    XxxYourBundle: ~ 
            em2: 
                connection:       conn2 
                mappings: 
                    XxxYourBundle2: ~

4 comments :

naitsirch said...

Hi.
Thank you for this post. It helped me to understand how it works when you want to use multiple Entity Manager or define further configurations for the default Entity Manager.

I had the same exception like you, but I found a more general solution.

You can add the 'mapping_auto' option to the doctrine.orm.entity_managers.[manager] directive.

Like this:
doctrine.orm.entity_managers.[manager].auto_mapping: true

The advantag is, that you do not need to explicitly define the bundles where you want to use the Manager.

PéCé said...

The property doctrine.orm.entity_managers.[manager].mappings doesn't define where you can use the entity_manager but in which bundle the entity_manager will find the entities it's managing.

Nicolas Schurmann said...

Hey dude!, amazing!, how can i make relations between the entities of different bundles??

pece said...

Making relations between entities of different bundles does not matter IF THEY ARE MAPPED BY THE SAME ENTITY MANAGER. So it"s not possible with different databases, that require 2 connections and so 2 entity managers...

But ... there is a workaround, working for MySQL at least. It's not natively supported by Doctrine, but it works if your RDBMS handles cross-database joins. Everything explained at following address : http://www.doctrine-project.org/blog/cross-database-joins.html

Post a Comment

Comments are moderated before being published.