From c2177838c8ea837b41a52cfff6771e020f09ee44 Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Sun, 12 Feb 2017 11:05:28 -0500 Subject: [PATCH 1/3] Reduce DB query number --- src/AppBundle/Repository/PostRepository.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/AppBundle/Repository/PostRepository.php b/src/AppBundle/Repository/PostRepository.php index 99db04bc1..5468af6bd 100644 --- a/src/AppBundle/Repository/PostRepository.php +++ b/src/AppBundle/Repository/PostRepository.php @@ -35,8 +35,9 @@ public function queryLatest() { return $this->getEntityManager() ->createQuery(' - SELECT p + SELECT p, t FROM AppBundle:Post p + LEFT JOIN p.tags t WHERE p.publishedAt <= :now ORDER BY p.publishedAt DESC ') From 01bc5b45d6966107a992e282d4889e11cacee05e Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Sun, 12 Feb 2017 11:10:26 -0500 Subject: [PATCH 2/3] Refactor paginator creation for reusability --- src/AppBundle/Repository/PostRepository.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/AppBundle/Repository/PostRepository.php b/src/AppBundle/Repository/PostRepository.php index 5468af6bd..87a4e168e 100644 --- a/src/AppBundle/Repository/PostRepository.php +++ b/src/AppBundle/Repository/PostRepository.php @@ -25,15 +25,18 @@ * * @author Ryan Weaver * @author Javier Eguiluz + * @author Yonel Ceruto */ class PostRepository extends EntityRepository { /** - * @return Query + * @param int $page + * + * @return Pagerfanta */ - public function queryLatest() + public function findLatest($page = 1) { - return $this->getEntityManager() + $query = $this->getEntityManager() ->createQuery(' SELECT p, t FROM AppBundle:Post p @@ -43,16 +46,13 @@ public function queryLatest() ') ->setParameter('now', new \DateTime()) ; + + return $this->createPaginator($query, $page); } - /** - * @param int $page - * - * @return Pagerfanta - */ - public function findLatest($page = 1) + private function createPaginator(Query $query, $page) { - $paginator = new Pagerfanta(new DoctrineORMAdapter($this->queryLatest(), false)); + $paginator = new Pagerfanta(new DoctrineORMAdapter($query, false)); $paginator->setMaxPerPage(Post::NUM_ITEMS); $paginator->setCurrentPage($page); From 37a37d0ecf1cd08adac242e25b5917a8475913e6 Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Sun, 12 Feb 2017 11:50:02 -0500 Subject: [PATCH 3/3] Fix tests --- src/AppBundle/Repository/PostRepository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AppBundle/Repository/PostRepository.php b/src/AppBundle/Repository/PostRepository.php index 87a4e168e..ba8cb0ebd 100644 --- a/src/AppBundle/Repository/PostRepository.php +++ b/src/AppBundle/Repository/PostRepository.php @@ -52,7 +52,7 @@ public function findLatest($page = 1) private function createPaginator(Query $query, $page) { - $paginator = new Pagerfanta(new DoctrineORMAdapter($query, false)); + $paginator = new Pagerfanta(new DoctrineORMAdapter($query)); $paginator->setMaxPerPage(Post::NUM_ITEMS); $paginator->setCurrentPage($page);