diff options
Diffstat (limited to 'src/Wallabag')
-rw-r--r-- | src/Wallabag/CoreBundle/Repository/EntryRepository.php | 50 |
1 files changed, 38 insertions, 12 deletions
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 38a3026d..749d2338 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php | |||
@@ -21,7 +21,7 @@ class EntryRepository extends EntityRepository | |||
21 | public function getBuilderForAllByUser($userId) | 21 | public function getBuilderForAllByUser($userId) |
22 | { | 22 | { |
23 | return $this | 23 | return $this |
24 | ->getBuilderByUser($userId) | 24 | ->getSortedQueryBuilderByUser($userId) |
25 | ; | 25 | ; |
26 | } | 26 | } |
27 | 27 | ||
@@ -35,7 +35,7 @@ class EntryRepository extends EntityRepository | |||
35 | public function getBuilderForUnreadByUser($userId) | 35 | public function getBuilderForUnreadByUser($userId) |
36 | { | 36 | { |
37 | return $this | 37 | return $this |
38 | ->getBuilderByUser($userId) | 38 | ->getSortedQueryBuilderByUser($userId) |
39 | ->andWhere('e.isArchived = false') | 39 | ->andWhere('e.isArchived = false') |
40 | ; | 40 | ; |
41 | } | 41 | } |
@@ -50,7 +50,7 @@ class EntryRepository extends EntityRepository | |||
50 | public function getBuilderForArchiveByUser($userId) | 50 | public function getBuilderForArchiveByUser($userId) |
51 | { | 51 | { |
52 | return $this | 52 | return $this |
53 | ->getBuilderByUser($userId) | 53 | ->getSortedQueryBuilderByUser($userId) |
54 | ->andWhere('e.isArchived = true') | 54 | ->andWhere('e.isArchived = true') |
55 | ; | 55 | ; |
56 | } | 56 | } |
@@ -65,7 +65,7 @@ class EntryRepository extends EntityRepository | |||
65 | public function getBuilderForStarredByUser($userId) | 65 | public function getBuilderForStarredByUser($userId) |
66 | { | 66 | { |
67 | return $this | 67 | return $this |
68 | ->getBuilderByUser($userId, 'starredAt', 'desc') | 68 | ->getSortedQueryBuilderByUser($userId, 'starredAt', 'desc') |
69 | ->andWhere('e.isStarred = true') | 69 | ->andWhere('e.isStarred = true') |
70 | ; | 70 | ; |
71 | } | 71 | } |
@@ -82,7 +82,7 @@ class EntryRepository extends EntityRepository | |||
82 | public function getBuilderForSearchByUser($userId, $term, $currentRoute) | 82 | public function getBuilderForSearchByUser($userId, $term, $currentRoute) |
83 | { | 83 | { |
84 | $qb = $this | 84 | $qb = $this |
85 | ->getBuilderByUser($userId); | 85 | ->getSortedQueryBuilderByUser($userId); |
86 | 86 | ||
87 | if ('starred' === $currentRoute) { | 87 | if ('starred' === $currentRoute) { |
88 | $qb->andWhere('e.isStarred = true'); | 88 | $qb->andWhere('e.isStarred = true'); |
@@ -111,7 +111,7 @@ class EntryRepository extends EntityRepository | |||
111 | public function getBuilderForUntaggedByUser($userId) | 111 | public function getBuilderForUntaggedByUser($userId) |
112 | { | 112 | { |
113 | return $this | 113 | return $this |
114 | ->getBuilderByUser($userId) | 114 | ->getSortedQueryBuilderByUser($userId) |
115 | ->andWhere('size(e.tags) = 0'); | 115 | ->andWhere('size(e.tags) = 0'); |
116 | } | 116 | } |
117 | 117 | ||
@@ -260,7 +260,7 @@ class EntryRepository extends EntityRepository | |||
260 | */ | 260 | */ |
261 | public function removeTag($userId, Tag $tag) | 261 | public function removeTag($userId, Tag $tag) |
262 | { | 262 | { |
263 | $entries = $this->getBuilderByUser($userId) | 263 | $entries = $this->getSortedQueryBuilderByUser($userId) |
264 | ->innerJoin('e.tags', 't') | 264 | ->innerJoin('e.tags', 't') |
265 | ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId()) | 265 | ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId()) |
266 | ->getQuery() | 266 | ->getQuery() |
@@ -296,7 +296,7 @@ class EntryRepository extends EntityRepository | |||
296 | */ | 296 | */ |
297 | public function findAllByTagId($userId, $tagId) | 297 | public function findAllByTagId($userId, $tagId) |
298 | { | 298 | { |
299 | return $this->getBuilderByUser($userId) | 299 | return $this->getSortedQueryBuilderByUser($userId) |
300 | ->innerJoin('e.tags', 't') | 300 | ->innerJoin('e.tags', 't') |
301 | ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId) | 301 | ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId) |
302 | ->getQuery() | 302 | ->getQuery() |
@@ -414,7 +414,20 @@ class EntryRepository extends EntityRepository | |||
414 | } | 414 | } |
415 | 415 | ||
416 | /** | 416 | /** |
417 | * Return a query builder to used by other getBuilderFor* method. | 417 | * Return a query builder to be used by other getBuilderFor* method. |
418 | * | ||
419 | * @param int $userId | ||
420 | * | ||
421 | * @return QueryBuilder | ||
422 | */ | ||
423 | private function getQueryBuilderByUser($userId) | ||
424 | { | ||
425 | return $this->createQueryBuilder('e') | ||
426 | ->andWhere('e.user = :userId')->setParameter('userId', $userId); | ||
427 | } | ||
428 | |||
429 | /** | ||
430 | * Return a sorted query builder to be used by other getBuilderFor* method. | ||
418 | * | 431 | * |
419 | * @param int $userId | 432 | * @param int $userId |
420 | * @param string $sortBy | 433 | * @param string $sortBy |
@@ -422,10 +435,23 @@ class EntryRepository extends EntityRepository | |||
422 | * | 435 | * |
423 | * @return QueryBuilder | 436 | * @return QueryBuilder |
424 | */ | 437 | */ |
425 | private function getBuilderByUser($userId, $sortBy = 'createdAt', $direction = 'desc') | 438 | private function getSortedQueryBuilderByUser($userId, $sortBy = 'createdAt', $direction = 'desc') |
426 | { | 439 | { |
427 | return $this->createQueryBuilder('e') | 440 | return $this->sortQueryBuilder($this->getQueryBuilderByUser($userId)); |
428 | ->andWhere('e.user = :userId')->setParameter('userId', $userId) | 441 | } |
442 | |||
443 | /** | ||
444 | * Return the given QueryBuilder with an orderBy() call | ||
445 | * | ||
446 | * @param QueryBuilder $qb | ||
447 | * @param string $sortBy | ||
448 | * @param string $direction | ||
449 | * | ||
450 | * @return QueryBuilder | ||
451 | */ | ||
452 | private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc') | ||
453 | { | ||
454 | return $qb | ||
429 | ->orderBy(sprintf('e.%s', $sortBy), $direction); | 455 | ->orderBy(sprintf('e.%s', $sortBy), $direction); |
430 | } | 456 | } |
431 | } | 457 | } |