Working with two entity managers in the same bundl

2019-01-13 16:39发布

问题:

I try 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 wich entites belong to which entity manager? It crashes now if I want to work with a table which doens't belong to the default entity manager.

Thanks

  • 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: ~

回答1:

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



回答2:

Ok. Tried to edit your original post but it's waiting for peer review. Not sure how long that takes. Try changing your config to:

doctrine:
    dbal:
        default_connection:       default
        connections:

        default:
            dbname:           old_project
            user:             root
            password:         123123
            host:             1.1.1.1
            port:             1

        # Make an explicit connection just for clarity
        old_project:
            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:
        # Humor me and add these
        auto_generate_proxy_classes: %kernel.debug%
    #   auto_mapping: true

    default_entity_manager:   electra
    entity_managers:

    # Make an explicit old_project em so default does not confuse us
    old_project:
        connection:       old_project
        mappings:
            XXDemoBundle: ~

    electra:
        connection:       electra
        mappings:
            XXDemoBundle: ~


    default:
        connection:       default
        mappings:
            XXDemoBundle: ~

Now completely blow away your cache just to be sure then run:

php app/console doctrine:mapping:info --em electra
php app/console doctrine:mapping:info --em old_project

You should get identical results. I tested this on my system so I'm fairly certain that if you don't then you have some typo somewhere.

So mapping info is working. Next step is to verify that both databases match your entity schema. So do this:

php app/console doctrine:schema:update --em electra --dump-sql
php app/console doctrine:schema:update --em old_project --dump-sql

Neither should produce any output. If one does then it means your database does not match your entities and that needs to be resolved (possibly using the --force option) before queries will work.

Once the databases are in sync then you should probably use doctrine:query:dql and do a test query against both managers. Then go back into your code.

=========================================

It has now been understood that the real goal is to have two entity managers point to the same set of entities but somehow indicate that each entity manager should limit itself to a certain set of those entities. And that is not something the S2 supports out of the box.

You could look through the Doctrine manual and see how it handles the entity metadata and maybe do something with that but that could get complicated.

The only thing that S2 really offers is the ability to bind an entity manager to all the entities in one or more bundles using the mapping attribute. If you wanted to share say three of seven entities from one bundle with another bundle then you would simply recreate those entities in the second bundle. Possibly by extending the class so as to avoid code duplication.

I think you might want to alter your approach a bit. If you have a set of core entities shared with multiple bundles then put those in their own bundle. Each follow on bundle can then add additional entities.