duannan4486 2015-05-13 06:39
浏览 38
已采纳

使用Symfony中的非默认实体管理器进行自动映射

Using the doctrine-bundle, I'm creating a bundle that requires a different database connection than the default one.

The bundle creates a new entity_manager, that uses the new connection through configuration:

doctrine:
  orm:
    entity_managers:
       default:....
       my_custom_em:
          connection: my_custom_connection
          mappings:
            MyBundle: ~

When trying to access the repository with $this->getContainer()->get('doctrine')->getRepository('MyBundle:AnEntity'); I have the error:

  Unknown Entity namespace alias 'MyBundle'.

But when using the name of the custom entity manager it works : $this->getContainer()->get('doctrine')->getRepository('MyBundle:AnEntity', 'my_custom_em');

There is no way for doctrine to detect that MyBundle should be mapped to this entity manager?

  • 写回答

1条回答 默认 最新

  • dqdmvg7332 2015-05-13 14:33
    关注

    Unfortunately auto_mapping can only be used when one connection is configured, so there is no simple one-line configuration which solve the problem.

    Anyway there is a simple solution.

    Use the RegistryManager

    Anyway, when you call $this->getContainer()->get('doctrine') you get an instance of ManagerRegistry (see the docs), which have a method called getManagerForClass. With this method you can write code that don't rely on the connection configuration.

    Your example will become:

    $class = 'MyBundle:AnEntity';
    $manager = $this->getContainer()->get('doctrine')->getManagerForClass($class);
    $manager->getRepository($className)
    

    Define the ObjectManager as a service

    If the special handling is required only for one bundle, you can take an even better solution from the previous approach.

    You can define a service which will call the RegistryManager for you in a transparent way.

    In service.xml you could write:

    <parameters>
        <parameter key="my.entity.an_entity.class">Acme\MyBundle\Entity\AnEntity</parameter>
    </parameters>
    
    <services>
        <service id="my.manager"
                 class="Doctrine\Common\Persistence\ObjectManager"
                 factory-service="doctrine" factory-method="getManagerForClass">
             <argument>%my.entity.an_entity.class%</argument>
        </service>
    
        <service id="my.an_entity_repository"
                 class="Doctrine\Common\Persistence\ObjectRepository"
                 factory-service="my.manager" factory-method="getRepository">
             <argument>%my.entity.an_entity.class%</argument>
        </service>
    </services>
    

    Your example will become:

    $class = 'MyBundle:AnEntity';
    
    $repository = $this->getContainer()->get('my.manager')->getRepository($class);
    // or
    $repository = $this->getContainer()->get('my.an_entity_repository')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 哪个tomcat中startup一直一闪而过 找不出问题
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成