Political Association Doctrine

I have a table called forums with the following field structure:

  • object_type → [Group | Page | Tournament | etc.] (Used by values. Each element has its own table)

  • object_id → [group id | page id | tournament id | etc.] (id object_type)

  • id_forum, 4.name, etc.

Then I have the following tables: Group , Page , Tournament , etc.

Is it possible to realize this with the doctrine?

+4
source share
3 answers

I think what you are looking for can be done with inheritance.

Learn more about http://www.doctrine-project.org/docs/orm/2.0/en/reference/inheritance-mapping.html .

+3
source

It should have been a comment, but my reputation is too low, sorry.

Unfortunately, polymorphic association is not quite the same as CTI (Inheritance of table classes): CTI requires a parent table with the same identifier for all subclasses.

CTI can work in most cases, but it can be a problem if you want to create a polymorphic relationship between existing objects that already have their own identifier and / or may have a different type of identifier.

In addition, CTI requires creating a record in the parent table for each subclass that is useless for polymorphic association. A polymorphic association should not require any table, it should just join existing tables with conditions (id, type). I believe Doctrine requires a parent table for convenience / performance.

When CTI is not a solution, I propose to emulate a polymorphic association at the repository level, i.e. create an abstract repository with the $ type attribute and implement the polyorphicJoin method, which automatically joins the current query to the target table using the id / type condition. It then extends the abstract repository with your Repositories subclasses and calls the polymorphicJoin method when necessary in your search / select methods.

It would be great if such an association were implemented in the Doctrine.

+7
source

I had a similar problem. I published a complete example of how to implement polymorphic associations in Doctrine 2.2 in this post .

0
source

Source: https://habr.com/ru/post/1337818/


All Articles