PostRepository getPostWithComments() method added. PostController#showAction() now uses it.

This commit is contained in:
Alexey Skobkin 2016-03-29 07:11:12 +03:00
parent cf08978c58
commit 0ceaf43ff3
2 changed files with 15 additions and 3 deletions

View file

@ -20,8 +20,7 @@ class PostController extends Controller
$userApi = $this->container->get('skobkin_point_tools.api_user'); $userApi = $this->container->get('skobkin_point_tools.api_user');
return $this->render('SkobkinPointToolsBundle:Post:show.html.twig', [ return $this->render('SkobkinPointToolsBundle:Post:show.html.twig', [
'post' => $post, 'post' => $this->getDoctrine()->getRepository('SkobkinPointToolsBundle:Blogs\Post')->getPostWithComments($post->getId()),
'avatar_url' => $userApi->getAvatarUrl($post->getAuthor(), UserApi::AVATAR_SIZE_LARGE),
]); ]);
} }

View file

@ -3,8 +3,21 @@
namespace Skobkin\Bundle\PointToolsBundle\Repository\Blogs; namespace Skobkin\Bundle\PointToolsBundle\Repository\Blogs;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
class PostRepository extends EntityRepository 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()
;
}
} }