Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] manyToMany polymorphic #434

Open
marcosdipaolo opened this issue Feb 20, 2020 · 4 comments
Open

[QUESTION] manyToMany polymorphic #434

marcosdipaolo opened this issue Feb 20, 2020 · 4 comments

Comments

@marcosdipaolo
Copy link

marcosdipaolo commented Feb 20, 2020

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

@marcosdipaolo
Copy link
Author

marcosdipaolo commented Feb 22, 2020

Guys come on, let me know please if polymorphic relationships are possible with Fluent

@eigan
Copy link
Member

eigan commented Feb 22, 2020

I have not used this part of laravel-doctrine, sorry :/

@marcosdipaolo marcosdipaolo changed the title manyToMany polymorphic [QUESTION] manyToMany polymorphic Feb 22, 2020
@dave-redfern
Copy link

@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.

@marcosdipaolo
Copy link
Author

marcosdipaolo commented Feb 28, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants