具有多个实体的 Doctrine2 OneToMany

Doctrine2 OneToMany with multiple Entities

我有三种用户,每个用户都有一些 FiscalData 并链接到 UserCredential 条目。

翻译成:

UserX (X=1,2,3) has two FKs referring to FiscalData and UserCredential tables.

使用 Doctrine2,阅读文档 http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/association-mapping.html,我认为需要 MappedSuperClass 模式。

我还阅读了以下问题:

Doctrine 2 - One-To-Many with multiple Entities

Many-To-One with multiple target entities

Doctrine2, Symfony2 - oneToOne with multiple entities?

但是在文档中明确指出

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.

那么,如何实现我想要实现的目标,即 UserX 和 FiscalData/UserCredential 之间的 双向关系 ? (这样 f.e。通过 Doctrine 我可以获得一个 UserCredential 并检查它关联的配置文件类型)

任何完整的最小代码示例展示了如何强制执行关系我正在寻找(而不仅仅是文档中显示的 MappedSuperClass 继承)将受到高度赞赏。

使用抽象实体代替 MappedSuperClass。单 table 通常是要走的路,除非你确定你想要 class/table.

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