You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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.
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 :
I don't know if this is possible through DQL/QueryBuilder
Thanks in advance for your help
The text was updated successfully, but these errors were encountered: