I’m using an analogy to make my point more understandable.
Let’s imagine a library. We have Books, Collections of books, and Users.
Books, Collections and Users are my entities.
- A Collection has many Books (OnetoMany, bidirectionnal)
- A Book may have been read by several Users (ManytoMany, bidirectionnal)
I have built these relations between the three entities, and so far, it seems fine.
Question is : how to display a list of Books for a given Collection, AND if the current User has read those Books?
I’m starting with this request for the list of books; I don’t know how to add a sub-request or something similar for the users:
class CollectionRepository extends EntityRepository {
public function getCollectionWithBooks($collectionName) {
$qb = $this->createQueryBuilder('c')
->leftJoin('c.books', 'b')
->where('c.urlname = :collectionName')
->setParameter('collectionName', $collectionName)
->addSelect('b');
return $qb->getQuery()
->getSingleResult();
}
}
Thank you very much!
Found the answer thanks to Coussinsky.
It was finally much less complicated than I expected 🙂
First, we need the getter/setter of the users linked to the books.
This can be done by updating the book entity with the console, such as :
We’ll have a getUsers() method returning an arrayCollection.
In the twig display, we can do something like this :