0

Related to this previous topic : Database : Account table with Joined Tables Inheritance

I've created a joined tables inheritance in my database (MySQL) : enter image description here

client_id attributes are both PK and FK.

Now I need to represent it with Doctrine ORM but I can't find a solution of how to make that.

I created, with Symfony commands, the Client entity and for the ClientCompany entity I don't know which relationship to use nor how to use the Client ID as primary key of the CompanyClient entity.

Does anyone know how to do it?

Stov
  • 29
  • 5

1 Answers1

1

First of all, be really carefull with inheritance, this is a really good feature of doctrine but need to be used with a lot of cautious because of it's counterpart.

For your case, i would advise to not try to put "person" and "company" under the same abstract "client" class for conception reason i already explain in this answer since a company and a person are totally different things: Symfony 6 inheritance Mapping : How to submit a form depends on a clicked radio button?

But i will still answer on how to properly do a join table inheritance :

AbstractClient.php

#[Entity]
#[InheritanceType('JOIN_TABLE')] 
#[DiscriminatorColumn(name: 'discr', type: 'string')]
#[DiscriminatorMap(['person' => Person::class, 'company' => Company::class])]
abstract class Client
{
    // you do not need clientType since it is hold by the "discr" column
    // And if you want to know what type your client is you can do it using   
    // if($client instanceof Person::class) { do something}
}

Person.php

#[Entity]
class Person extends Client
{
    // ...
}

Company.php

#[Entity]
class Company extends Client
{
    // ...
}

Take a look at #[InheritanceType('JOIN_TABLE')] It will create one table for each entity and they will share ids. If you create a company with id 2, there will be a client with id 2. So a Person with id 2 will never be possible.

But if you use 'SINGLE_TABLE' it will create only one table with all the field of all the entity which will be empty depending on which child you inserted inside.

But again i strongly advise you to not use join table inheritance for your usecase

ThomasL
  • 749
  • 4
  • 12
  • Thanks for your response. I see what you mean in that a person and a company are two different things. But since, in my case, both people and companies can be clients, I think it's better to put these two tables together as a client (also since this project is the creation of a client area). – Stov Jan 26 '23 at 10:41
  • As you wish :) Just be carefull because i think your under estimate the amount of code you will have to do to manage this duality for forms / views / condition. – ThomasL Jan 26 '23 at 10:55
  • To provide a great exemple of inheritance. A Vehicle can be a truck or a car, the truck will have "space of cargo" has additionnal attribute. But they will still share "number of wheels" , "power", "number of seats". In your case, Company and Person will share almost nothing. And Inheritance are not made for such usecase. Anyway everyone have to experience those issue themselves. the only reason that make me insist is because i already made that mistake in the past. In fact i had in the past, the inhritance discovery syndrom which made me put it everywhere haha – ThomasL Jan 26 '23 at 10:57
  • I understand, but in my case people and companies will share several attributes (address, postal code/city, status (active or terminated client)) and have some different ones (companies: name and number - People: first/last names and birth date). – Stov Jan 26 '23 at 12:32
  • Sorry, I must have made a mistake because the Client class cannot be abstracted since it is the one I will use. I think that the inheritance must be reversed and therefore it would be equivalent to a multiple inheritance – Stov Jan 26 '23 at 13:01
  • Since a Company and a Person are a Client by definition, why would use Client directly ? You wouldn't know if it is a Person or a Company otherwise – ThomasL Jan 26 '23 at 13:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/251408/discussion-between-thomasl-and-stov). – ThomasL Jan 26 '23 at 13:16