将实体管理器绑定到 Symfony2 中的实体?

发布于 2024-12-23 08:50:18 字数 1974 浏览 1 评论 0原文

我的 Symfony2 框架中有 2 个捆绑包: 一个包含用户实体的“MainBundle”,以及一个包含我用于问题跟踪系统的所有内容的“TicketBundle”。在这种情况下,只有实体“Pool”和“Mappingpooluser”很重要。 他们都使用不同的实体管理器,因为我必须使用两个不同的数据库。 (请参阅下面的 config.yml)

doctrine:
  dbal:
    default_connection: default
    connections:
        default:
               [driver, host etc.]
               dbname:   m_symfony
        ticket:
               [driver, host etc.]
               dbname:   m_ticketbundle


orm:
  default_entity_manager: default
  entity_managers:
    default:
      connection: default
      mappings:
        XXXMainBundle: ~
    ticket:
      connection: ticket
      mappings:
        XXXTicketBundle: ~
        XXXMainBundle: ~

现在我有一个实体(Mappingpooluser),它需要 MainBundle(用户)的一个实体和 TicketBundle 的一个实体(池)(以及一些不重要的东西):

/**
 * XXX\TicketBundle\Entity\Mappingpooluser
 *
 * @ORM\Table(name="MappingPoolUser")
 * @ORM\Entity(repositoryClass="XXX\TicketBundle\Repository\MappingPoolUserRepository")
*/
class Mappingpooluser
{

/**
 * @var integer $poolid
 *
 * @ORM\OneToOne(targetEntity="Pool")
 * @ORM\JoinColumn(name="id", referencedColumnName="id")
 */
private $pool;

/**
 * @var integer $userid
 *
 * @ORM\OneToOne(targetEntity="XXX\MainBundle\Entity\User")
 * @ORM\JoinColumn(name="id", referencedColumnName="id")
 */
private $user;

[getter/setter and this stuff]

到目前为止,一切正常:) 我可以通过以下方式获取控制器中的 Mappingpooluser 实体

 $em = $this->getDoctrine()->getEntityManager("ticket");
 $entities = $em->getRepository('XXXTicketBundle:Mappingpooluser')->findAll();

如果我调用 $entities[0]->getPool()->getId() 我将获得池的正确 ID(如上所述:池位于同一个 Bundle 中,例如Mappingpooluser),但如果我调用 $entities[0]->getUser()->getId() 我会得到一个错误:

 SQLSTATE[42S02]: Base table or view not found: 1146 Table 'm_ticketbundle.User' doesn't exist 

那是正确的退出,因为用户表位于另一个数据库中(m_symfony)。

长话短说问题:如何让 Symfony2 使用池的票证实体管理器和用户的默认实体管理器?

I've got 2 Bundles in my Symfony2 Framework:
A "MainBundle" with a User Entity, and a "TicketBundle" with all my Stuff for the Issue Tracking System. In this Case only the Entities "Pool" and "Mappingpooluser" are important.
They both use different entity mangers, because i have to use 2 different databases. (see my config.yml below)

doctrine:
  dbal:
    default_connection: default
    connections:
        default:
               [driver, host etc.]
               dbname:   m_symfony
        ticket:
               [driver, host etc.]
               dbname:   m_ticketbundle


orm:
  default_entity_manager: default
  entity_managers:
    default:
      connection: default
      mappings:
        XXXMainBundle: ~
    ticket:
      connection: ticket
      mappings:
        XXXTicketBundle: ~
        XXXMainBundle: ~

Now I have an Entity (Mappingpooluser) which needs one Entity of the MainBundle (User) and one Entity (Pool) of the TicketBundle (and some unimportant stuff):

/**
 * XXX\TicketBundle\Entity\Mappingpooluser
 *
 * @ORM\Table(name="MappingPoolUser")
 * @ORM\Entity(repositoryClass="XXX\TicketBundle\Repository\MappingPoolUserRepository")
*/
class Mappingpooluser
{

/**
 * @var integer $poolid
 *
 * @ORM\OneToOne(targetEntity="Pool")
 * @ORM\JoinColumn(name="id", referencedColumnName="id")
 */
private $pool;

/**
 * @var integer $userid
 *
 * @ORM\OneToOne(targetEntity="XXX\MainBundle\Entity\User")
 * @ORM\JoinColumn(name="id", referencedColumnName="id")
 */
private $user;

[getter/setter and this stuff]

Till this point everything works fine :)
I can fetch the Mappingpooluser Entity in the Controller via

 $em = $this->getDoctrine()->getEntityManager("ticket");
 $entities = $em->getRepository('XXXTicketBundle:Mappingpooluser')->findAll();

If I call $entities[0]->getPool()->getId() I will get the correct ID of the Pool (as mentioned: Pool is in the same Bundle like Mappingpooluser), but if I call $entities[0]->getUser()->getId() I will get an Error:

 SQLSTATE[42S02]: Base table or view not found: 1146 Table 'm_ticketbundle.User' doesn't exist 

Thats quit correct, because the User table is in the other DB (m_symfony).

Long Story, short Question: How do I get Symfony2 to use the ticket entity-manger for the Pool and the default entity-manger for the User??

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

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

发布评论

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

评论(1

浅浅 2024-12-30 08:50:18

您的场景的实际问题是您需要将相同的实体(池)映射到两个不同的实体管理器(默认和票证)。在这种情况下,我建议在您的实体层中为此实体创建一个层次结构:

  • 基础池,包含所有公共字段
  • 池“默认”,包含默认 em
  • 池“票证”的专用字段,包含票证 em 的专用字段

然后您可以将它们映射到不同的实体管理器中。如果您对默认实体和票证实体使用两个不同的命名空间,则可以在 em 配置中指定应从中加载映射的文件夹,如下所示:

mappings:   
  MyBundle:
    type: annotation
    dir: Path/To/Entities

该路径相对于捆绑包的根目录。例如,您可以拥有 Entity/Default 和 Entity/Ticket 命名空间,并独立映射它们,同时在实体命名空间中的未映射类中拥有公共字段。

The actual problem with your scenario is that you need to map the same entity (pool) to two different entity managers (default and ticket). What I would recommend in this scenario is creating a hierarchy in your entity layer for this entity:

  • base pool, contains all common fields
  • pool "default", contains specialized fields for default em
  • pool "ticket", contains specialized fields for ticket em

Then you can map them in different entity managers. If you use two different namespaces for default and ticket entities you can specify the folder from which mappings should be loaded in your em configuration like this:

mappings:   
  MyBundle:
    type: annotation
    dir: Path/To/Entities

The path is relative to the Bundles's root directory. So for example you could have Entity/Default and Entity/Ticket namespaces and map them independently while having common fields in an unmapped class in Entity namespace.

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