Working with two entity managers in the same bundle in Symfony2

前端 未结 2 1620
孤城傲影
孤城傲影 2020-12-05 03:15

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

orm:

    default_entity_manager:   default
    entity_manage         


        
2条回答
  •  死守一世寂寞
    2020-12-05 04:19

    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

提交回复
热议问题