在 Symfony2 中使用同一个包中的两个实体管理器 [英] Working with two entity managers in the same bundle in Symfony2

查看:23
本文介绍了在 Symfony2 中使用同一个包中的两个实体管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用两个实体管理器来处理同一个包.我的配置是这样的:

orm:default_entity_manager:默认实体经理:电子:连接:电子映射:XXDemoBundle:~默认:连接:默认映射:XXDemoBundle:~

有什么办法可以知道哪些实体属于哪个实体管理器?如果我想使用不属于默认实体管理器的表,它现在会崩溃.

  • 更新

这是我的连接配置:

学说:数据库:default_connection: 默认连接:默认:数据库名称:old_project用户:root密码:123123主机:1.1.1.1端口:1电子:数据库名称:电子用户:root密码:123123主机:2.2.2.2端口:2形式:default_entity_manager:electra实体经理:电子:连接:电子映射:XXDemoBundle:~默认:连接:默认映射:XXDemoBundle:~

解决方案

要在同一个包中使用多个 entitymanager,您必须为每个 entitymanager 配置映射选项.

http://symfony.com/doc/current/reference/configuration/doctrine.html

配置文件示例

<前>教义:数据库:default_connection: 默认连接:默认:驱动程序:%database_driver%主机:%database_host%端口:%database_port%数据库名称:%database_name%用户:%database_user%密码:%database_password%字符集:UTF8第二:驱动程序:%database_sqlite_driver%主持人:~端口:~数据库名称:%database_sqlite_shop_name%路径:%database_sqlite_shop_name%用户:~密码:~字符集:UTF8形式:auto_generate_proxy_classes: %kernel.debug%default_entity_manager:默认实体经理:默认:连接:默认映射:你的捆绑:# 你必须指定类型类型:注释"#实体的目录(相对于bundle路径)目录:实体/FirstDb"#前缀前缀:YourBundleEntityFirstDb"店铺:连接:第二映射:你的捆绑:类型:注释"#这里是连接实体所在的第二条路径目录:实体/SecondDb"#前缀前缀:YourBundleEntitySecondDb"

您现在可以使用控制台通过 --em 参数管理您的数据库

例如:更新商店实体管理器的数据库

php app/console 原则:schema:update --em=shop

从 YourBundleEntitySecondDb 中读取映射信息

例如:更新默认实体管理器的数据库

php app/console 原则:模式:更新

从 YourBundleEntityFirstDb 读取映射信息

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

解决方案

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

这篇关于在 Symfony2 中使用同一个包中的两个实体管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆