php - Doctrine - ManyToMany Join on a LeftJoin alias
one text
I am confused.
I have 4 tables as follows:
Grocery and GroceryTranslation have a OneToMany
Grocery and Tag have a ManyToMany.
Grocery and UserGrocery have a OneToMany
Now a User may ask "Hey what are my Groceries?"
So to find the answer I select from
UserGrocery, join with Grocery (to get info about the actual groceries), then join Grocery with GroceryTranslation to get the display-names of the Groceries, and then join Grocery with Tag to get the corresponding tags.
I tried to say that exact same sentence in Doctrine with this:
# create the queryBuilder from UserGrocery-Repo
$qb = $em->getRepository(UserGrocery::class)->createQueryBuilder('ug');
$result = $qb
->leftJoin('ug.grocery', 'g') # Join on Grocery
->leftJoin('g.translation', 'gt') # Join on GroceryTranslation
->innerJoin('g.tags', 't') # Join on Tags with Grocery-Alias !!Fails!!
->where(
$qb->expr()->eq('ug.owner', $user->getId()),
)
->getQuery()
->execute();
This Query results in a Semantical Error:
Error: Class App\Entity\Grocery has no field or association named "tags"
Which kind of makes sense - but not to me?
The Grocery-Entity has the properties translation, populated with GroceryTranslation-Items, and tags populated with Tag-Items - both are properties of type Collection.
#[ORM\OneToMany(mappedBy: 'grocery', targetEntity: GroceryTranslation::class, orphanRemoval: true)]
private Collection $translation;
#[ORM\ManyToMany(targetEntity: Tag::class, mappedBy: 'grocery')]
private Collection $tags;
If I remove the ->innerJoin() statement everything works, but obviously no Tags exist in the result.
Please help me?
Source