TenantUserProjectMember
两者VendorUserProjectMember
都有一个$user
属性,但它们有不同的 targetEntities。这两个类都扩展了 MappedSuperclass AbstractUserProjectMember
。虽然未显示,但此$user
属性具有其他属性,我想在其中定义一次AbstractUserProjectMember
并覆盖具体类中的关联。
我尝试了以下方法,但得到了错误关联TenantUserProjectMember#user
并VendorUserProjectMember#user
引用了AbstractUser#projectMembers
不存在的反向字段。请注意,我不能添加 a $projectMembers
,AbstractUser
因为并非所有扩展类都具有这样的属性。
是否可以在扩展的具体类中指定父级的 targetEntity,如果可以,如何指定?
#[ORM\MappedSuperclass]
abstract class AbstractUserProjectMember implements UserCreateActionInterface
{
#[ORM\ManyToOne(targetEntity: AbstractUser::class)] //Override join table and add inversedBy in child class
// More attributes but just not shown in this example
protected TenantUser|VendorUser|null $user = null;
// Typical properties, getters, and setters...
}
#[ORM\Entity]
#[ORM\AssociationOverrides([
new ORM\AssociationOverride(
name: "user",
joinTable: new ORM\JoinTable(name: "tenant_user"),
inversedBy: "projectMembers",
),
])]
class TenantUserProjectMember extends AbstractUserProjectMember
{
// Typical properties, getters, and setters...
}
#[ORM\Entity]
#[ORM\AssociationOverrides([
new ORM\AssociationOverride(
name: "user",
joinTable: new ORM\JoinTable(name: "vendor_user"),
inversedBy: "projectMembers",
),
])]
class VendorUserProjectMember extends AbstractUserProjectMember
{
// Typical properties, getters, and setters...
}
#[ORM\Entity]
class TenantUser extends AbstractUser
{
#[ORM\OneToMany(mappedBy: 'user', targetEntity: TenantUserProjectMember::class)]
private ?Collection $projectMembers = null;
// Typical properties, getters, and setters...
}
#[ORM\Entity]
class VendorUser extends AbstractUser
{
#[ORM\OneToMany(mappedBy: 'user', targetEntity: VendorUserProjectMember::class)]
private ?Collection $projectMembers = null;
// Typical properties, getters, and setters...
}
#[ORM\Entity]
#[ORM\InheritanceType(value: 'JOINED')]
#[ORM\DiscriminatorColumn(name: 'type', type: 'string')]
#[ORM\DiscriminatorMap(value: ['tenant' => TenantUser::class, 'system' => SystemUser::class, 'vendor' => VendorUser::class])]
#[ORM\Table(name: '`user`')]
abstract class AbstractUser
{
// Note that only child class TenantUser and VendorUser have a $projectMembers property and SystemUser does not.
// Typical properties, getters, and setters...
}