Skip to content

Commit 62d652d

Browse files
committed
bug #460 Reduce DB query number in post index action (yceruto)
This PR was squashed before being merged into the master branch (closes #460). Discussion ---------- Reduce DB query number in post index action **Performance Issue** After tags feature the blog index page performs one extra query for each post to fetches its related tags. This PR avoids that. Other changes: * Refactoring the Post repository methods to reuse paginator creation in the future. Commits ------- 0c169ca Reduce DB query number in post index action
2 parents 14e6114 + 0c169ca commit 62d652d

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/AppBundle/Repository/PostRepository.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,34 @@
2525
*
2626
* @author Ryan Weaver <[email protected]>
2727
* @author Javier Eguiluz <[email protected]>
28+
* @author Yonel Ceruto <[email protected]>
2829
*/
2930
class PostRepository extends EntityRepository
3031
{
3132
/**
32-
* @return Query
33+
* @param int $page
34+
*
35+
* @return Pagerfanta
3336
*/
34-
public function queryLatest()
37+
public function findLatest($page = 1)
3538
{
36-
return $this->getEntityManager()
39+
$query = $this->getEntityManager()
3740
->createQuery('
38-
SELECT p
41+
SELECT p, t
3942
FROM AppBundle:Post p
43+
LEFT JOIN p.tags t
4044
WHERE p.publishedAt <= :now
4145
ORDER BY p.publishedAt DESC
4246
')
4347
->setParameter('now', new \DateTime())
4448
;
49+
50+
return $this->createPaginator($query, $page);
4551
}
4652

47-
/**
48-
* @param int $page
49-
*
50-
* @return Pagerfanta
51-
*/
52-
public function findLatest($page = 1)
53+
private function createPaginator(Query $query, $page)
5354
{
54-
$paginator = new Pagerfanta(new DoctrineORMAdapter($this->queryLatest(), false));
55+
$paginator = new Pagerfanta(new DoctrineORMAdapter($query));
5556
$paginator->setMaxPerPage(Post::NUM_ITEMS);
5657
$paginator->setCurrentPage($page);
5758

0 commit comments

Comments
 (0)