在 Symfony2 中使用同一包中的两个实体管理器

发布于 2025-01-06 00:29:36 字数 1284 浏览 3 评论 0原文

我正在尝试与同一个捆绑包的两个实体管理器合作。我的配置是这样的:

orm:

    default_entity_manager:   default
    entity_managers:
        electra:
            connection:       electra
            mappings:
                XXDemoBundle: ~
        default:
            connection:       default
            mappings:
                XXDemoBundle: ~

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

  • 这里更新

是我的连接配置:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜中书 2025-01-13 00:29:36

要在同一包中使用多个实体管理器,您必须为每个实体管理器配置映射选项。

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

示例 您现在可以使用控制台

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" 

通过 --em 参数管理

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

php app/console doctrine:schema:update --em=shop

从 Your\Bundle\Entity\SecondDb 读取映射信息

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

php app/console doctrine:schema:update   

从以下位置读取映射信息你的\Bundle\Entity\FirstDb

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

隔纱相望 2025-01-13 00:29:36

好的。尝试编辑您的原始帖子,但正在等待同行评审。不确定这需要多长时间。尝试将您的配置更改为:

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

现在完全清除您的缓存,以确保然后运行:

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

您应该得到相同的结果。我在我的系统上测试了这个,所以我相当确定如果你不这样做,那么你在某个地方有一些拼写错误。

所以地图信息正在发挥作用。下一步是验证两个数据库是否与您的实体架构匹配。所以这样做:

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

两者都不应该产生任何输出。如果出现这种情况,则意味着您的数据库与您的实体不匹配,需要在查询起作用之前解决该问题(可能使用 --force 选项)。

一旦数据库同步,那么您可能应该使用doctrine:query:dql并对两个管理器进行测试查询。然后回到你的代码。

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

现在已经明白真正的目标是让两个实体管理器指向同一组实体,但以某种方式指示每个实体管理器应该将自己限制为这些实体的某个集合。 S2 并不支持这种功能。

您可以查看 Doctrine 手册,看看它如何处理实体元数据,也许可以用它做一些事情,但这可能会变得复杂。

S2 真正提供的唯一一件事是能够使用映射属性将实体管理器绑定到一个或多个捆绑包中的所有实体。如果您想与另一个捆绑包共享一个捆绑包中的七个实体中的三个,那么您只需在第二个捆绑包中重新创建这些实体即可。可能通过扩展类来避免代码重复。

我想你可能想稍微改变一下你的方法。如果您有一组与多个捆绑包共享的核心实体,请将它们放入自己的捆绑包中。每个后续捆绑包都可以添加其他实体。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文