Working with Two Entity Managers in the Same Bundle in Symfony2

Working with two entity managers in the same bundle in Symfony2

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: "Your\Bundle\Entity\FirstDb"
shop:
connection: second
mappings:
YourBundle:
type: "annotation"
#here the second path where entity for the connection stand
dir: "Entity/SecondDb"
#the prefix
prefix: "Your\Bundle\Entity\SecondDb"

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 Your\Bundle\Entity\SecondDb

Ex : update database for default entitymanager

php app/console doctrine:schema:update   

Read mapping information from Your\Bundle\Entity\FirstDb

Configure Second Entity Manager for Symfony5 Bundle Entities

I have not configured multiple entity managers for the latest and greatest Symfony versions so I setup a little test case and came up with this for a configuration:

    orm:
default_entity_manager: default
auto_generate_proxy_classes: true
entity_managers:
default:
connection: default
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
geonames:
connection: geonames
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
mappings:
BordeuxGeoNameBundle:
is_bundle: true
type: annotation
dir: 'Entity'
prefix: 'Bordeux\Bundle\GeoNameBundle\Entity'
alias: GeoNames

I tested it using:

bin/console doctrine:mapping:info --em=geonames

And confirmed the entities were being mapped. I did not install EasyAdmin and test it but I don't see any reason why it would not work.

The main difference was using the entity namespace for the prefix attribute.

And just for my own future reference, I committed the test project to github.

Using Relationships with Multiple Entity Managers

Using different object managers (entity managers) doesn't allow the object graphs to intersect. That case is too complex and isn't managed by Doctrine ORM.

If you need such a case, keep the object graphs disconnected by saving the identifiers of the related objects (old style) instead of a reference to them, then manually get the objects through services. You can find a fairly good example of how this would work in an example of connection between Doctrine2 ORM and Doctrine2 MongoDB ODM. Alternatively, you could also use a @PostLoad event listener that populates data in your entities by creating the link through the repositories I've linked in the example. Same for @PostPersist (which should instead extract the identifiers for the related objects), but beware that this technique can become really messy.

Also, if your RDBMS supports cross-database operations on a single host, you can just use a single EntityManager and reference the other table with @ORM\Table(name="schemaname.tablename").

Symfony2 Relation between entities cross Bundles and multiple entity manager

Make sure that you have set this in the config file:

assetic:
...
bundles: [ DashboardProjectBundle, SportOfferBundle]

UPDATE:

Also, try to set orm config like this:

orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
DashboardProjectBundle: ~
SpotOfferBundle: ~
spot:
connection: spot
mappings:
SpotOfferBundle: ~

AliceBundle with multiple entity managers?

You must tell the bundle what Entity Manager to use manually by providing the --manager option.

Working with 2 databases and just one entity manager in Symfony2

This isn't possible, each Entity Manager can only use one DB connection, the docs seem quite clear about it.

So I think you'll be stuck with using two EMs. Each will be configured with a duplicate set of your mappings. The detail of how you use them is up to you though:

  • You could just manually choose one or the other as required in your app
  • You could somehow abstract it away into a class of your own which has both EMs, and then when you run queries etc it will be worrying about where to get the data from (and possibly how to combine data from both EMs)
  • If the only activity which really needs both EMs is the archive process itself, that's an obvious thing to hide away in a class nicely

I suppose it also depends on what the point of the archive DB is. If it's some architectural thing like it needs to be on a different server or whatever, then you're stuck as above. On the other hand, if you really just want old data not to show up in day-to-day queries (without specifically asking for it), then it might be better to implement some kind of "archived" flag and a Doctrine Extension which magically hides archived items away until you ask for them, very similar to SoftDeleteable



Related Topics



Leave a reply



Submit