简体   繁体   English

Symfony2-捆绑中的两个数据库连接-似乎无法设置-请参阅我的代码

[英]Symfony2 - Two database connections in one bundle - can't seem to set it up - see my code

This is what I am trying to do: 这就是我想要做的:

namespace BundleTwo\Controller;

use BundleOne\Entity\TestEntity;

class TestController
{

    public function pageAction()
    {

        $insert = new TestEntity();
        $insert->fieldName('test');
        $em = $this->getDoctrine()->getManager('dbcon2');   
        $em->persist($insert);
        $em->flush();

    }

}

…so the code above is in Bundle 2 which uses an Entity from Bundle 1 - but it needs to use DB connection2. …因此,上面的代码在Bundle 2中,它使用Bundle 1中的Entity-但它需要使用DB connection2。

I can use this http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html to set up the two DB connections and I can see how you can define which EM is used by a bundle, but I want 2 connections for one bundle. 我可以使用此http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html设置两个数据库连接,我可以看到如何定义捆绑使用哪个EM,但是我想要2一捆的连接。

I think the above code would work, but when I run app/console doctrine:schema:update --em=dbcon2 it doesn't populate anything in the DB and doesn't detect any Entities. 我认为上面的代码可以工作,但是当我运行app / console doctrine:schema:update --em = dbcon2时,它不会在数据库中填充任何内容,也不会检测到任何实体。 I think I need to somehow get it to realise that BundleOne\\Entity\\TestEntity is also used by Bundle 2 and DB 2. 我想我需要以某种方式使其意识到BundleOne \\ Entity \\ TestEntity也被Bundle 2和DB 2使用。

If I could put settings inside BundleTwo that set the DB connection used for BundleOne\\Entity\\TestEntity then that might do it. 如果我可以将设置放在BundleTwo内,该设置可以设置用于BundleOne \\ Entity \\ TestEntity的数据库连接,则可以这样做。

You can specify which bundles contains entity metadata with mapping property 您可以指定哪些捆绑包包含具有mapping属性的实体元数据

doctrine:
    orm:
        default_entity_manager:   dbcon1
        entity_managers:
            dbcon1:
                connection: dbcon1
                mappings:
                    BundleOne: ~
            dbcon2:
                connection: dbcon2
                mappings:
                    BundleOne: ~

As you surmised, you must tell each entity manager which bundles to use when looking for entities. 如您所猜测的,您必须告诉每个实体管理器在寻找实体时要使用哪个捆绑软件。

Something like: 就像是:

orm:
    default_entity_manager:       default
    auto_generate_proxy_classes: %kernel.debug%
   #auto_mapping: true

    entity_managers:

        default:
            connection: default
            mappings:

        games:
           connection: games
           mappings:
                CeradGameBundle: ~

        accounts:
           connection: accounts
           mappings:
                FOSUserBundle: ~
                CeradAccountBundle: ~

So your dbcon2 would have bundle 1 and bundle 2 listed. 因此,您的dbcon2将列出捆绑1和捆绑2。 If this answer does not help then please update your question and show your doctrine orm config. 如果此答案无济于事,请更新您的问题并显示您的主义orm配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM