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

Closed
opened 2026-01-22 15:34:44 +01:00 by admin · 1 comment
Owner

Originally created by @InjustFr on GitHub (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

Originally created by @InjustFr on GitHub (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
admin closed this issue 2026-01-22 15:34:44 +01:00
Author
Owner

@beberlei commented on GitHub (Sep 13, 2020):

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.

@beberlei commented on GitHub (Sep 13, 2020): 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 in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6537