繁体   English   中英

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

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

这就是我想要做的:

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();

    }

}

…因此,上面的代码在Bundle 2中,它使用Bundle 1中的Entity-但它需要使用DB connection2。

我可以使用此http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html设置两个数据库连接,我可以看到如何定义捆绑使用哪个EM,但是我想要2一捆的连接。

我认为上面的代码可以工作,但是当我运行app / console doctrine:schema:update --em = dbcon2时,它不会在数据库中填充任何内容,也不会检测到任何实体。 我想我需要以某种方式使其意识到BundleOne \\ Entity \\ TestEntity也被Bundle 2和DB 2使用。

如果我可以将设置放在BundleTwo内,该设置可以设置用于BundleOne \\ Entity \\ TestEntity的数据库连接,则可以这样做。

您可以指定哪些捆绑包包含具有mapping属性的实体元数据

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

如您所猜测的,您必须告诉每个实体管理器在寻找实体时要使用哪个捆绑软件。

就像是:

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

因此,您的dbcon2将列出捆绑1和捆绑2。 如果此答案无济于事,请更新您的问题并显示您的主义orm配置。

暂无
暂无

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

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