Doctrine2 Many polymorphic relationships

I am trying to create many many polymorphic relationships with Doctrine2 in Symfony2.

I would like one entity to be dynamically associated with multiplet objects.

I want to get the following diagram:

  • posts
    • id: integer,
    • name: string

======

  1. video
    • id - integer
    • name - string

======

  1. tags
    • id - integer
    • name - string

======

  1. taggables
    • tag_id - integer
    • taggable_id - integer
    • taggable_type - string

In the taggables object:

  • tag_id is an associated tag
  • taggable_id is the post id
  • taggable_type - type of the associated object, that is, "Messages"

And I would like it to be the same with the "video", where:

  • tag_id represents the identifier of the associated tag
  • taggable_id is the identifier of the associated videos
  • taggable_type - name of the associated object, i.e. "Video"

And all this without duplication of the table.

I tested several solutions, but I never got this result: /

Thank you in advance for your help.

+5
source share
1 answer

You can solve this with OOP using inheritance.

Define an abstract class Taggable and make Post and Video continue this class. Then create OneToMany from Tag to Taggable .

Doctrine takes care of eternity, suppose you choose one-dimensional inheritance or class table inheritance.

I would choose a class table.

Read more about this topic here .

+2
source

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


All Articles