简体   繁体   English

Doctrine2 OneToMany有多个实体

[英]Doctrine2 OneToMany with multiple Entities

I have three kinds of Users, every user has some FiscalData and is linked to a UserCredential entry. 我有三种用户,每个用户都有一些FiscalData并链接到UserCredential条目。

Which translates in: 其中翻译为:

UserX (X=1,2,3) has two FKs referring to FiscalData and UserCredential tables. UserX(X = 1,2,3)有两个FK引用FiscalDataUserCredential表。

Using Doctrine2, reading the docs http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/association-mapping.html , I believe to need the MappedSuperClass pattern. 使用Doctrine2,阅读文档http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/association-mapping.html ,我认为需要MappedSuperClass模式。

I have also read the following questions: 我还阅读了以下问题:

Doctrine 2 - One-To-Many with multiple Entities 学说2 - 具有多个实体的一对多

Many-To-One with multiple target entities 具有多个目标实体的多对一

Doctrine2, Symfony2 - oneToOne with multiple entities? Doctrine2,Symfony2 - oneToOne有多个实体?

But in the docs is clearly stated that 但在文档中明确指出

A mapped superclass cannot be an entity, it is not query-able and persistent relationships defined by a mapped superclass must be unidirectional (with an owning side only). 映射的超类不能是实体,它不是可查询的,并且由映射的超类定义的持久关系必须是单向的(仅具有拥有方)。 This means that One-To-Many associations are not possible on a mapped superclass at all. 这意味着根本不可能在映射的超类上进行一对多关联。 Furthermore Many-To-Many associations are only possible if the mapped superclass is only used in exactly one entity at the moment. 此外,只有当映射的超类目前仅在一个实体中使用时,才能实现多对多关联。 For further support of inheritance, the single or joined table inheritance features have to be used. 为了进一步支持继承,必须使用单个或连接表继承功能。

So, how to achieve what I'm trying to achieve, which is a bidirectional relationship between UserX and FiscalData / UserCredential ? 那么,如何实现我想要实现的目标,这是UserX和FiscalData / UserCredential之间的双向关系 (so that fe via Doctrine I can get a UserCredential and check what kind of profiles it has associated) (通过Doctrine我可以获得UserCredential并检查它与哪个配置文件关联)

Any complete minimal code example showing how to enforce the relationship I'm looking for (and not just the MappedSuperClass inheritance shown in the docs will be highly appreciated. 任何完整的最小代码示例显示如何强制执行我正在寻找的关系 (而不仅仅是文档中显示的MappedSuperClass继承将受到高度赞赏。

Use an abstract entity instead of MappedSuperClass. 使用抽象实体而不是MappedSuperClass。 Single-table is usually the way to go unless you're sure you want class/table. 单表通常是要走的路,除非你确定你想要类/表。

<?php
/**
 * @ORM\Entity
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({
 *     "mentor" = "Mentor",
 *     "protege" = "Protege"
 * })
 */
abstract class User { ... }

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

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