Symfony MappedSuperclass store Entity reference

120 Views Asked by At

I'm currently writing a MappedSuperclass to store entity reference on an entity object. For example to store favorites for a user, it can be any kind of model (product, category of product or whatever).

The class contains deux fields :

  • entityType : the class of the entity reference
  • entityId : the id of the entity reference

Favorite entity class will extends this Superclass, and that's work.

I'm would like to know, if there is another (cleaner?) way to do this ?

Thanks for your inputs.

1

There are 1 best solutions below

0
On

As we work a lot currently with Doctrine mapping, this all depends of your buisness logic and what you want your database looks like.

If using a mappedSuperclass, basically, this entity will not appear in your database. Only entities that inherits from that mappedSuperClass will appear with its own properties and the superClass properties.

Another approach could be to use inheritance joined type that will generates this time the class in your database, event it is abstract.

Usability of your schema, even in mappedSuperclass or JOINED type, do not changes a lot of things, it's all about your preferences and your logic.

I personaly prefer the second method, as it permit to structure more your data.

I hope this helps.

Best Regards.