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

Accessing Many-To-Many Relation in child entity through parent using CTI #8262

Closed
InjustFr opened this issue Sep 7, 2020 · 1 comment
Closed

Comments

@InjustFr
Copy link

InjustFr commented Sep 7, 2020

Hi,

I'm trying to load ManyToMany associations on child entities using the parent entity. Here is an example to try to make this clearer:

I have a parent abstract Entity. Let's call it Animal. Then I have two entities inheriting Animal called Dog and Cat. Dog has a ManyToMany relation with DogHouse and Cat a ManyToMany relation with LitterBox. The inheritance type is a CTI

Now when I load all the Animal I want to fetch at the same time DogHouse and LitterBox to avoid several queries when accessing those entities

I've tried a bunch of ways and it either throws errors as DogHouse and LitterBox do not have a relation with Animal or the hydration breaks and instead of a collection of Animal entities I end up with a collection of Animal, DogHouse and LitterBox

I've searched for ages on the Web and haven't found my answer, sadly

Here are examples of the requests I tried :

//In a repository
// Doesn't work as animal doesn't have a relation with doghouse
$this->createQueryBuilder("a")
    ->leftJoin("a.doghouses", "dh")
    ->addSelect("dh");


//In a repository
//Returns a collection of Animals and DogHouses instead of just Animals
$this->createQueryBuilder("a")
    ->leftJoin(DogHouse::class, "dh", "WITH", "a MEMBER OF dg.dogs")
    ->addSelect("dh");

//In a repository
//Doesn't work either as you now have duplicates in your collection between Animal and Dog
$this->createQueryBuilder("a")
    ->leftJoin(Dog::class, "d", "WITH", "d.id = a.id")
    ->addSelect('d')
    ->leftJoin("d.doghouses", "dh")
    ->addSelect("dh");

I don't know if this is possible through DQL/QueryBuilder

Thanks in advance for your help

@beberlei
Copy link
Member

This is not possible, because inheritance rules are enforced in DQL as they would be in PHP. You can convert it to a native SQL query or rethink your use of inheritance, it looks like it might not be a good fit.

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

2 participants