Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

php - 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: ~
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...