orm
orm copied to clipboard
[QUESTION] manyToMany polymorphic
I know this place is for bugs, but i don't know any other place where to get a clear response about this.
I'm mantaining a huge app that uses Doctrine with LaravelDoctrine and Fluent.
I already used Mapping inheritance a couple of times but I dont know how to achieve a manyToMany polymorphic relationship and could't find anything at the docs.
Let's do this with Tags:
I have an:
<?php
abstract class Tag
{
/** @var string $name */
protected $name;
/** @var string $color */
protected $color;
public function __construct(string $name, string $color)
{
$this->name = $name;
$this->color = $color;
}
then:
<?php
class PostTag extends Tag
and:
<?php
class VideoTag extends Tag
What i've tried so far is a mix between a normal belongsToMany relationship at the child mappings with the singleTableInheritance method at the parent's mapping.
<?php
class TagMapping extends EntityMapping
{
public function map(Fluent $builder)
{
$builder->bigIncrements('id');
$builder->string('name');
$builder->string('color');
$builder->singleTableInheritance()->column('type');
}
<?php
class PostTagMapping extends EntityMapping
{
public function map(Fluent $builder)
{
$builder->belongsToMany(Post::class, 'posts')->inversedBy('tags');
}
<?php
class PostMapping extends EntityMapping
{
public function map(Fluent $builder)
{
$builder->belongsToMany(PostTag::class, 'tags')->mappedBy('posts');
}
and is saving the tags at the tags table but not the relationship at the pivot. I don't even know if it should create one pivot for every child or not.
In short, i need help, thanks a lot and excuse me if this is not the place
Guys come on, let me know please if polymorphic relationships are possible with Fluent
I have not used this part of laravel-doctrine, sorry :/
@marcosdipaolo You don't need the PostTag mapping. Remove it and then link Post to AbstractTag directly. You would then need to implement entity level validation to keep only PostTags within a Post. The relationship is then a straight forward many-to-many Post to AbstractTag with the type on the Tags table.
As an aside, to the best of my knowledge Doctrine does not use or support "pivot" tables on many-to-many relationships. A pivot created in this way is really a missing object in your object graph. To do this in Doctrine you have to define that object and map it accordingly.
The route you should take largely depends on where you want that tag type to be set: either a single object "Tag" that is mapped through a polymorphic TagRelationship object (PostTag, VideoTag etc etc) which would then give you an intermediary object and a link table with the type on it. Tag would become a first class object (not abstract) and the link table would become a first class object in between the Post and Tag.
Or: as originally stated, Tag has the type and Post has a collection of AbstractTag objects that you would need to enforce the type of via application logic. There would be no type field on the link table between tag and post.
Thanks @dave-redfern
I did more that last thing you mentioned.
But a bit different, Post and Video has manyToMany relationships with PostTag and VideoTag separately, each with the auto generated pivot post_post_tag and video_video_tag. Both child Tag entities extending the Tag mother entitiy