From 0ceaf43ff3359ed2ed19d61697092c8ad8c2d0eb Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Tue, 29 Mar 2016 07:11:12 +0300 Subject: [PATCH] PostRepository getPostWithComments() method added. PostController#showAction() now uses it. --- .../Controller/PostController.php | 3 +-- .../Repository/Blogs/PostRepository.php | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Skobkin/Bundle/PointToolsBundle/Controller/PostController.php b/src/Skobkin/Bundle/PointToolsBundle/Controller/PostController.php index 4ce4266..97816f8 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Controller/PostController.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Controller/PostController.php @@ -20,8 +20,7 @@ class PostController extends Controller $userApi = $this->container->get('skobkin_point_tools.api_user'); return $this->render('SkobkinPointToolsBundle:Post:show.html.twig', [ - 'post' => $post, - 'avatar_url' => $userApi->getAvatarUrl($post->getAuthor(), UserApi::AVATAR_SIZE_LARGE), + 'post' => $this->getDoctrine()->getRepository('SkobkinPointToolsBundle:Blogs\Post')->getPostWithComments($post->getId()), ]); } diff --git a/src/Skobkin/Bundle/PointToolsBundle/Repository/Blogs/PostRepository.php b/src/Skobkin/Bundle/PointToolsBundle/Repository/Blogs/PostRepository.php index 83ca10a..7a2a069 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Repository/Blogs/PostRepository.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Repository/Blogs/PostRepository.php @@ -3,8 +3,21 @@ namespace Skobkin\Bundle\PointToolsBundle\Repository\Blogs; use Doctrine\ORM\EntityRepository; +use Doctrine\ORM\QueryBuilder; class PostRepository extends EntityRepository { - + public function getPostWithComments($postId) + { + /** @var QueryBuilder $qb */ + $qb = $this->createQueryBuilder('p'); + return $qb + ->select(['p', 'c', 'a']) + ->leftJoin('p.comments', 'c') + ->leftJoin('c.author', 'a') + ->where($qb->expr()->eq('p.id', ':post_id')) + ->setParameter('post_id', $postId) + ->getQuery()->getOneOrNullResult() + ; + } } \ No newline at end of file