0

I am asking for your help because today I am having a legacy problem.

I have a Holder class which inherits from the User class. I would like to associate a user with several holders with inheritance and without creating a ManyToOne relationship, but the inheritance creates a key duplication problem on Holder.

The idea is to have a Business, Merchant, personal, etc. Holder associated with a single user.

Thanks for your help.

User class :

#[ORM\Entity(repositoryClass: UserRepository::class)]
//#[UniqueEntity('phoneNumber', message: "This phone number is already used")]
#[ORM\InheritanceType("JOINED")]
#[ORM\DiscriminatorColumn("user_type")]
#[ORM\DiscriminatorMap(["holder" => "Holder", "employee" => "Employee"])]
abstract class User implements UserInterface, PasswordAuthenticatedUserInterface
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;
}

Holder class :

class Holder extends User
{
    ....
}

1 Answers1

0

What you're trying to achieve is not possible with inheritance. And for a good reason, it is not made for such use case.

And inheritance always make id shared within parent and child entity. So
if you create a Business id 1, it create a Holder id 1
if you create a Merchant id 2, it create a Holder id 2

There will be no possibility to have a Business id 2 since Holder already exist with id 2 shared with Merchant.
It will be auto incremented to 3 then.

I advise you to stick to many to one/many relationship to handle those case. Also, look at my answer on Doctrine ORM : Joined table inheritance And the chat in the comment to get detail about mindset to handle such case.

ThomasL
  • 749
  • 4
  • 12