-1

TenantUserProjectMember and VendorUserProjectMember both have a $user property but they have different targetEntities. Both of these classes extend the MappedSuperclass AbstractUserProjectMember. While not shown, this $user property has additional attributes, and I would like to define it once in AbstractUserProjectMember and override the associations in the concrete classes.

I have tried the following but get errors association TenantUserProjectMember#user and VendorUserProjectMember#user refer to the inverse side field of AbstractUser#projectMembers which does not exist. Note that I cannot add a $projectMembers to AbstractUser as not all extended classes have such a property.

Is it possible to specify parent's targetEntity in the extended concrete class, and if so how?

#[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...
}
4

0 に答える 0