diff options
author | Jérémy Benoist <j0k3r@users.noreply.github.com> | 2017-08-08 19:48:47 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-08 19:48:47 +0200 |
commit | 86ecd2b543e0f63609213ff45665fef94e38c681 (patch) | |
tree | bbd2709f61167882ca2b023ade4f037d249fd2b8 /src/Wallabag | |
parent | aff1dd4ff13f541001832cfb009d6f8854ff978d (diff) | |
parent | 935e9fffb4083f83e86c4e97a32bc2d9bccd677f (diff) | |
download | wallabag-86ecd2b543e0f63609213ff45665fef94e38c681.tar.gz wallabag-86ecd2b543e0f63609213ff45665fef94e38c681.tar.zst wallabag-86ecd2b543e0f63609213ff45665fef94e38c681.zip |
Merge pull request #3314 from nclsHart/fix-3313
Reduce number of queries on tag list
Diffstat (limited to 'src/Wallabag')
-rw-r--r-- | src/Wallabag/CoreBundle/Controller/TagController.php | 21 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Repository/EntryRepository.php | 20 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Repository/TagRepository.php | 21 |
3 files changed, 23 insertions, 39 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/TagController.php b/src/Wallabag/CoreBundle/Controller/TagController.php index f2ca58c6..be2dff98 100644 --- a/src/Wallabag/CoreBundle/Controller/TagController.php +++ b/src/Wallabag/CoreBundle/Controller/TagController.php | |||
@@ -84,28 +84,11 @@ class TagController extends Controller | |||
84 | */ | 84 | */ |
85 | public function showTagAction() | 85 | public function showTagAction() |
86 | { | 86 | { |
87 | $repository = $this->get('wallabag_core.entry_repository'); | ||
88 | $tags = $this->get('wallabag_core.tag_repository') | 87 | $tags = $this->get('wallabag_core.tag_repository') |
89 | ->findAllTags($this->getUser()->getId()); | 88 | ->findAllFlatTagsWithNbEntries($this->getUser()->getId()); |
90 | |||
91 | $flatTags = []; | ||
92 | |||
93 | foreach ($tags as $tag) { | ||
94 | $nbEntries = $repository->countAllEntriesByUserIdAndTagId( | ||
95 | $this->getUser()->getId(), | ||
96 | $tag->getId() | ||
97 | ); | ||
98 | |||
99 | $flatTags[] = [ | ||
100 | 'id' => $tag->getId(), | ||
101 | 'label' => $tag->getLabel(), | ||
102 | 'slug' => $tag->getSlug(), | ||
103 | 'nbEntries' => $nbEntries, | ||
104 | ]; | ||
105 | } | ||
106 | 89 | ||
107 | return $this->render('WallabagCoreBundle:Tag:tags.html.twig', [ | 90 | return $this->render('WallabagCoreBundle:Tag:tags.html.twig', [ |
108 | 'tags' => $flatTags, | 91 | 'tags' => $tags, |
109 | ]); | 92 | ]); |
110 | } | 93 | } |
111 | 94 | ||
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 7f35bb9a..d70d6ca6 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php | |||
@@ -330,26 +330,6 @@ class EntryRepository extends EntityRepository | |||
330 | } | 330 | } |
331 | 331 | ||
332 | /** | 332 | /** |
333 | * Count all entries for a tag and a user. | ||
334 | * | ||
335 | * @param int $userId | ||
336 | * @param int $tagId | ||
337 | * | ||
338 | * @return int | ||
339 | */ | ||
340 | public function countAllEntriesByUserIdAndTagId($userId, $tagId) | ||
341 | { | ||
342 | $qb = $this->createQueryBuilder('e') | ||
343 | ->select('count(e.id)') | ||
344 | ->leftJoin('e.tags', 't') | ||
345 | ->where('e.user=:userId')->setParameter('userId', $userId) | ||
346 | ->andWhere('t.id=:tagId')->setParameter('tagId', $tagId) | ||
347 | ; | ||
348 | |||
349 | return (int) $qb->getQuery()->getSingleScalarResult(); | ||
350 | } | ||
351 | |||
352 | /** | ||
353 | * Remove all entries for a user id. | 333 | * Remove all entries for a user id. |
354 | * Used when a user want to reset all informations. | 334 | * Used when a user want to reset all informations. |
355 | * | 335 | * |
diff --git a/src/Wallabag/CoreBundle/Repository/TagRepository.php b/src/Wallabag/CoreBundle/Repository/TagRepository.php index 213283e5..5c45211f 100644 --- a/src/Wallabag/CoreBundle/Repository/TagRepository.php +++ b/src/Wallabag/CoreBundle/Repository/TagRepository.php | |||
@@ -63,6 +63,27 @@ class TagRepository extends EntityRepository | |||
63 | } | 63 | } |
64 | 64 | ||
65 | /** | 65 | /** |
66 | * Find all tags (flat) per user with nb entries. | ||
67 | * | ||
68 | * @param int $userId | ||
69 | * | ||
70 | * @return array | ||
71 | */ | ||
72 | public function findAllFlatTagsWithNbEntries($userId) | ||
73 | { | ||
74 | return $this->createQueryBuilder('t') | ||
75 | ->select('t.id, t.label, t.slug, count(e.id) as nbEntries') | ||
76 | ->distinct(true) | ||
77 | ->leftJoin('t.entries', 'e') | ||
78 | ->where('e.user = :userId') | ||
79 | ->groupBy('t.id') | ||
80 | ->orderBy('t.slug') | ||
81 | ->setParameter('userId', $userId) | ||
82 | ->getQuery() | ||
83 | ->getArrayResult(); | ||
84 | } | ||
85 | |||
86 | /** | ||
66 | * Used only in test case to get a tag for our entry. | 87 | * Used only in test case to get a tag for our entry. |
67 | * | 88 | * |
68 | * @return Tag | 89 | * @return Tag |