1

I'm working on a project and I've come out with this doubt. I've got an IS-A relationship which looks like this:

Users as a parent table, then Companies, Admins and Customers as child tables. Since all of them have different attributes I've decided to create 4 tables and link them to the Users table which will hold the login information, which is the common information between the childs.

My struggle is that I can't create a child if the parent doesn't exist obviusly, but I'd like to let the customers and companies register themselves, so the solution I've come out with is inserting a new user, then get the max id from users and insert a customer. But I'm not sure if is a good approach, what happens if multiple people try to register at once?

Could I control the parent insertion from the database instead of my code? Maybe with a trigger?

That would make more sense to me, mainly because an user itself doesn't make any sense in my context, it's either a customer, an admin or a company.

ruuux93
  • 122
  • 2
  • 3
  • 9

1 Answers1

1

Given that, like you say, a user itself does not make any sense, I think your user class should be made abstract. This way you don't have to create any User, the information is directlu inherited from the User class in your children entities.

Detailed edit : From a doctrine point of view, simply use the @ORM\MappedSuperclass annotation on you parent entity (User). There is nothing to do in your children entities. This way, every child entity will inherit all the properties and methods of the parent entity, which in your case is what I understood you wanted to do. I let you read the specific part of the doctrine documentation about this point :

Doctrine inheritance mapped superclass

Hope this helps.

Guillaume Fache
  • 813
  • 10
  • 21
  • Could you explain further how to manage this at database level? Because you can't create a child if the parent doesn't exist. You mean merging some tables? – ruuux93 May 13 '15 at 08:14