Use Query Builder
Summary
$qb->expr()->isMemberOf(':user', 'a.authors')
Solution
src/Repository/ArticleRepository.php
/**
* @param User $author
* @return Article[] List of articles filtered by $author
*/
public function findByAuthor(User $author): array
{
$qb = $this->createQueryBuilder('a');
$qb->setParameter('user', $author);
$qb->where($qb->expr()->isMemberOf(':user', 'a.authors'));
return $qb->getQuery()->getResult();
}
Example use
src/Controller/ArticleController.php
/**
* @Route("/article/{id<\d+>}", name="show-articles-by-author")
* @param ArticleRepository $articleRepository
* @param User $author
*/
public function showArticlesFromAuthor(ArticleRepository $articleRepository, User $author)
{
$articles = $articleRepository->findByAuthor($author);
return $this->render('articles.html.twig', [
'articles' => $articles,
'author' => $author->getName()
]);
}
templates/articles.html.twig
<h1>Articles from {{ author }}</h1>
<li>
{% for article in articles %}
<ul>
{{ article.title }}
</ul>
{% endfor %}
</li>