diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Repository')
-rw-r--r-- | src/Wallabag/CoreBundle/Repository/EntryRepository.php | 53 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Repository/TagRepository.php | 18 |
2 files changed, 60 insertions, 11 deletions
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 4f03ae0f..b9532fa2 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php | |||
@@ -20,8 +20,7 @@ class EntryRepository extends EntityRepository | |||
20 | private function getBuilderByUser($userId) | 20 | private function getBuilderByUser($userId) |
21 | { | 21 | { |
22 | return $this->createQueryBuilder('e') | 22 | return $this->createQueryBuilder('e') |
23 | ->leftJoin('e.user', 'u') | 23 | ->andWhere('e.user = :userId')->setParameter('userId', $userId) |
24 | ->andWhere('u.id = :userId')->setParameter('userId', $userId) | ||
25 | ->orderBy('e.createdAt', 'desc') | 24 | ->orderBy('e.createdAt', 'desc') |
26 | ; | 25 | ; |
27 | } | 26 | } |
@@ -86,6 +85,36 @@ class EntryRepository extends EntityRepository | |||
86 | } | 85 | } |
87 | 86 | ||
88 | /** | 87 | /** |
88 | * Retrieves entries filtered with a search term for a user. | ||
89 | * | ||
90 | * @param int $userId | ||
91 | * @param string $term | ||
92 | * @param strint $currentRoute | ||
93 | * | ||
94 | * @return QueryBuilder | ||
95 | */ | ||
96 | public function getBuilderForSearchByUser($userId, $term, $currentRoute) | ||
97 | { | ||
98 | $qb = $this | ||
99 | ->getBuilderByUser($userId); | ||
100 | |||
101 | if ('starred' === $currentRoute) { | ||
102 | $qb->andWhere('e.isStarred = true'); | ||
103 | } elseif ('unread' === $currentRoute) { | ||
104 | $qb->andWhere('e.isArchived = false'); | ||
105 | } elseif ('archive' === $currentRoute) { | ||
106 | $qb->andWhere('e.isArchived = true'); | ||
107 | } | ||
108 | |||
109 | $qb | ||
110 | ->andWhere('e.content LIKE :term OR e.title LIKE :term')->setParameter('term', '%'.$term.'%') | ||
111 | ->leftJoin('e.tags', 't') | ||
112 | ->groupBy('e.id'); | ||
113 | |||
114 | return $qb; | ||
115 | } | ||
116 | |||
117 | /** | ||
89 | * Retrieves untagged entries for a user. | 118 | * Retrieves untagged entries for a user. |
90 | * | 119 | * |
91 | * @param int $userId | 120 | * @param int $userId |
@@ -96,9 +125,7 @@ class EntryRepository extends EntityRepository | |||
96 | { | 125 | { |
97 | return $this | 126 | return $this |
98 | ->getBuilderByUser($userId) | 127 | ->getBuilderByUser($userId) |
99 | ->leftJoin('e.tags', 't') | 128 | ->andWhere('size(e.tags) = 0'); |
100 | ->groupBy('e.id') | ||
101 | ->having('count(t.id) = 0'); | ||
102 | } | 129 | } |
103 | 130 | ||
104 | /** | 131 | /** |
@@ -144,7 +171,7 @@ class EntryRepository extends EntityRepository | |||
144 | $qb->orderBy('e.updatedAt', $order); | 171 | $qb->orderBy('e.updatedAt', $order); |
145 | } | 172 | } |
146 | 173 | ||
147 | $pagerAdapter = new DoctrineORMAdapter($qb); | 174 | $pagerAdapter = new DoctrineORMAdapter($qb, true, false); |
148 | 175 | ||
149 | return new Pagerfanta($pagerAdapter); | 176 | return new Pagerfanta($pagerAdapter); |
150 | } | 177 | } |
@@ -329,4 +356,18 @@ class EntryRepository extends EntityRepository | |||
329 | 356 | ||
330 | return $qb->getQuery()->getSingleScalarResult(); | 357 | return $qb->getQuery()->getSingleScalarResult(); |
331 | } | 358 | } |
359 | |||
360 | /** | ||
361 | * Remove all entries for a user id. | ||
362 | * Used when a user want to reset all informations. | ||
363 | * | ||
364 | * @param int $userId | ||
365 | */ | ||
366 | public function removeAllByUserId($userId) | ||
367 | { | ||
368 | $this->getEntityManager() | ||
369 | ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId') | ||
370 | ->setParameter('userId', $userId) | ||
371 | ->execute(); | ||
372 | } | ||
332 | } | 373 | } |
diff --git a/src/Wallabag/CoreBundle/Repository/TagRepository.php b/src/Wallabag/CoreBundle/Repository/TagRepository.php index e76878d4..81445989 100644 --- a/src/Wallabag/CoreBundle/Repository/TagRepository.php +++ b/src/Wallabag/CoreBundle/Repository/TagRepository.php | |||
@@ -34,6 +34,9 @@ class TagRepository extends EntityRepository | |||
34 | 34 | ||
35 | /** | 35 | /** |
36 | * Find all tags per user. | 36 | * Find all tags per user. |
37 | * Instead of just left joined on the Entry table, we select only id and group by id to avoid tag multiplication in results. | ||
38 | * Once we have all tags id, we can safely request them one by one. | ||
39 | * This'll still be fastest than the previous query. | ||
37 | * | 40 | * |
38 | * @param int $userId | 41 | * @param int $userId |
39 | * | 42 | * |
@@ -41,15 +44,20 @@ class TagRepository extends EntityRepository | |||
41 | */ | 44 | */ |
42 | public function findAllTags($userId) | 45 | public function findAllTags($userId) |
43 | { | 46 | { |
44 | return $this->createQueryBuilder('t') | 47 | $ids = $this->createQueryBuilder('t') |
45 | ->select('t.slug', 't.label', 't.id') | 48 | ->select('t.id') |
46 | ->leftJoin('t.entries', 'e') | 49 | ->leftJoin('t.entries', 'e') |
47 | ->where('e.user = :userId')->setParameter('userId', $userId) | 50 | ->where('e.user = :userId')->setParameter('userId', $userId) |
48 | ->groupBy('t.slug') | 51 | ->groupBy('t.id') |
49 | ->addGroupBy('t.label') | ||
50 | ->addGroupBy('t.id') | ||
51 | ->getQuery() | 52 | ->getQuery() |
52 | ->getArrayResult(); | 53 | ->getArrayResult(); |
54 | |||
55 | $tags = []; | ||
56 | foreach ($ids as $id) { | ||
57 | $tags[] = $this->find($id); | ||
58 | } | ||
59 | |||
60 | return $tags; | ||
53 | } | 61 | } |
54 | 62 | ||
55 | /** | 63 | /** |