Mi solucion al reto
En el controlador
#[Route('/blog/{slug}', name: 'app_post')]
public function post($slug, PostRepository $postRepository): Response
{
return $this->render('page/post.html.twig', [
'post' => $postRepository->findOneBySlug($slug),
'form' => $this->createForm(CommentType::class),
]);
}
En el repositorio
public function findOneBySlug($slug): ?Post
{
return $this->createQueryBuilder('post')
->andWhere('post.slug = :slug')
->setParameter('slug', $slug)
->addSelect(['comments', 'category', 'user'])
->leftJoin('post.comments', 'comments')
->leftJoin('comments.user', 'user')
->leftJoin('post.category', 'category')
->getQuery()
->getOneOrNullResult();
}
¿Quieres ver más aportes, preguntas y respuestas de la comunidad?