2

Introduction

I'm working on a symfony3 project and I have a lot of different "types" for certain entities. What I mean by this is that, I have a TypeUser, TypeEvent, TypeMeeting tables.

These tables will only have an id and a label.

Problem:

When using doctrine, I can link 1 entity to 1 table using the anotations, like so:

/**
 * @ORM\Entity
 * @ORM\Table(name="TypeUser")
 */

However, I would like this part to be completely generic. How can I specify the Table name depending on the type I need?

Is there another alternative when using Doctrine other than the annotations to make this possible?

I would really like to avoid making n entities for n tables, when they are very similar in name and in structure.

Question:

Is it possible to make one generic entity to match a specific TypeXXXX table, to reduce redundancy? If so how would I go about doing it?


Similar Doctrine 2.1 - Map entity to multiple tables


Symfony book on doctrine

Community
  • 1
  • 1
kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131
  • 2
    Inheritance is probably your best bet: http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html. Of course I would sort of wonder about your design but that is a different issue. – Cerad Feb 19 '16 at 16:16
  • @Cerad thanks for the comment. I did think about that, but I wanted to avoid inheritance as well. Why would you wonder about the design? Bad practice? – kemicofa ghost Feb 22 '16 at 10:20
  • Depends on the app of course. Duplicate types can be a bit of a red flag. But maybe not. – Cerad Feb 22 '16 at 14:10
  • @Cerad that's a really good point. I could eventually make a third column in the type and just specify "typeName" and have that set as "user", "meeting", etc – kemicofa ghost Feb 22 '16 at 14:20
  • Speaking of design, could you not have solved your problem by doing entity relationships between your main table and the several possible types, instead of repeating tables? – Joe Yahchouchi Sep 26 '16 at 08:47

0 Answers0