Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine2 Many To Many Polymorphic Relations

I try to create a Many To Many Polymorphic Relations with Doctrine2 in Symfony2.

I would like a single entity that is associate dynamically with multiples entities.

I want to get this following schema:

  1. posts
    • id: integer,
    • name: string

======

  1. videos
    • id - integer
    • name - string

======

  1. tags
    • id - integer
    • name - string

======

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

In taggables entity :

  • tag_id is the of the associate tag
  • taggable_id is the id of associate post
  • taggable_type is the type of associate entity ie 'Posts"

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

  • tag_id represents the id of associate tag
  • taggable_id is the id of associate videos
  • taggable_type is the name of associate entity ie 'Videos'

and all this without duplicating table.

i'ved test multiple solutions but i never got this result :/

Thank you in advance for your help.

like image 491
deezmeen Avatar asked Nov 01 '22 14:11

deezmeen


1 Answers

You could solve this with OOP, using inheritance.

Define an abstract class Taggable, and make Post and Video extend that class. Then create a OneToMany from Tag to Taggable.

Doctrine will take care of everithing, supposed that you choose between Single Table Inheritance or Class Table Inheritance.

I would choose Class Table, though.

More on this subject here.

like image 77
Alessandro Lai Avatar answered Jan 04 '23 13:01

Alessandro Lai