1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Repository; |
4
|
|
|
use AppBundle\Model\PaginatorWithPages; |
5
|
|
|
use Doctrine\ORM\EntityRepository; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* ArticleRepository |
9
|
|
|
* |
10
|
|
|
* This class was generated by the Doctrine ORM. Add your own custom |
11
|
|
|
* repository methods below. |
12
|
|
|
*/ |
13
|
|
|
class ArticleRepository extends EntityRepository |
14
|
|
|
{ |
15
|
1 |
|
public function getArticlesWithCountComment($page = 1, $max = 10) |
16
|
|
|
{ |
17
|
1 |
|
$first = $max * ($page - 1); |
18
|
1 |
|
$query = $this->createQueryBuilder('a') |
19
|
1 |
|
->select('a, c, t, u, count(cm.id) as countComments') |
20
|
1 |
|
->leftJoin('a.categories', 'c') |
21
|
1 |
|
->leftJoin('a.tags', 't') |
22
|
1 |
|
->leftJoin('a.comments', 'cm') |
23
|
1 |
|
->join('a.user', 'u') |
24
|
1 |
|
->groupBy('a, c, t, u') |
25
|
1 |
|
->orderBy('a.createdAt', 'DESC') |
26
|
1 |
|
->setFirstResult($first) |
27
|
1 |
|
->setMaxResults($max) |
28
|
1 |
|
->getQuery(); |
29
|
|
|
// ->getResult(); |
|
|
|
|
30
|
|
|
|
31
|
1 |
|
return $articles = new PaginatorWithPages($query, $fetchJoinCollection = true); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
1 |
|
public function getArticleWithDep($slug) |
35
|
|
|
{ |
36
|
1 |
|
return $this->createQueryBuilder('a') |
37
|
1 |
|
->select('a, c, t, u') |
38
|
1 |
|
->leftJoin('a.categories', 'c') |
39
|
1 |
|
->leftJoin('a.tags', 't') |
40
|
1 |
|
->join('a.user', 'u') |
41
|
1 |
|
->where('a.slug = ?1') |
42
|
1 |
|
->setParameter(1, $slug) |
43
|
1 |
|
->getQuery() |
44
|
1 |
|
->getSingleResult(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
public function getArticleWithCountComment($slug) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
return $this->createQueryBuilder('a') |
50
|
|
|
->select('a, c, t, u, count(cm.id) as countComments') |
51
|
|
|
->leftJoin('a.categories', 'c') |
52
|
|
|
->leftJoin('a.tags', 't') |
53
|
|
|
->leftJoin('a.comments', 'cm') |
54
|
|
|
->join('a.user', 'u') |
55
|
|
|
->where('a.slug = ?1') |
56
|
|
|
->groupBy('a, c, t, u') |
57
|
|
|
->setParameter(1, $slug) |
58
|
|
|
->getQuery() |
59
|
|
|
->getSingleResult(); |
60
|
|
|
} |
61
|
|
|
|
62
|
3 |
|
public function getArticlesSorted($sortBy, $param, $page = 1, $max = 10) |
63
|
|
|
{ |
64
|
3 |
|
$first = $max * ($page - 1); |
65
|
3 |
|
$query = $this->createQueryBuilder('a') |
66
|
3 |
|
->select('a, c, t, u, count(cm.id) as countComments') |
67
|
3 |
|
->leftJoin('a.categories', 'c') |
68
|
3 |
|
->leftJoin('a.tags', 't') |
69
|
3 |
|
->leftJoin('a.comments', 'cm') |
70
|
3 |
|
->join('a.user', 'u') |
71
|
3 |
|
->groupBy('a, c, t, u') |
72
|
3 |
|
->orderBy('a.createdAt', 'DESC'); |
73
|
|
|
|
74
|
|
|
switch ($sortBy) { |
75
|
3 |
|
case 'category': |
76
|
|
|
$query |
77
|
1 |
|
->where('c.slug = ?1') |
78
|
1 |
|
->setParameter(1, $param); |
79
|
1 |
|
break; |
80
|
2 |
|
case 'tag': |
81
|
|
|
$query |
82
|
1 |
|
->where('t.slug = ?1') |
83
|
1 |
|
->setParameter(1, $param); |
84
|
1 |
|
break; |
85
|
1 |
|
case 'author': |
86
|
|
|
$query |
87
|
1 |
|
->where('u.slug = ?1') |
88
|
1 |
|
->setParameter(1, $param); |
89
|
1 |
|
break; |
90
|
|
|
case 'date': |
91
|
|
|
$query |
92
|
|
|
->where('a.createdAt >= ?1') |
93
|
|
|
->andWhere('a.createdAt <= ?2') |
94
|
|
|
->setParameter(1, $param." 00:00:00") |
95
|
|
|
->setParameter(2, $param." 23:59:59"); |
96
|
|
|
break; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$query |
100
|
3 |
|
->setFirstResult($first) |
101
|
3 |
|
->setMaxResults($max) |
102
|
3 |
|
->getQuery(); |
103
|
|
|
// ->getResult(); |
|
|
|
|
104
|
|
|
|
105
|
3 |
|
return $articles = new PaginatorWithPages($query, $fetchJoinCollection = true); |
|
|
|
|
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
5 |
View Code Duplication |
public function getPopularArticles($max = 5) |
|
|
|
|
110
|
|
|
{ |
111
|
5 |
|
return $this->createQueryBuilder('a') |
112
|
5 |
|
->select('a, u, avg(c.rating) as rating') |
113
|
5 |
|
->leftJoin('a.comments', 'c') |
114
|
5 |
|
->join('a.user', 'u') |
115
|
5 |
|
->groupBy('a, u') |
116
|
5 |
|
->orderBy('rating', 'DESC') |
117
|
5 |
|
->setFirstResult(0) |
118
|
5 |
|
->setMaxResults($max) |
119
|
5 |
|
->getQuery() |
120
|
5 |
|
->getResult(); |
121
|
|
|
} |
122
|
|
|
|
123
|
5 |
|
public function getRecentArticles($max = 5) |
124
|
|
|
{ |
125
|
5 |
|
return $this->createQueryBuilder('a') |
126
|
5 |
|
->select('a, u') |
127
|
5 |
|
->join('a.user', 'u') |
128
|
5 |
|
->orderBy('a.createdAt', 'DESC') |
129
|
5 |
|
->setFirstResult(0) |
130
|
5 |
|
->setMaxResults($max) |
131
|
5 |
|
->getQuery() |
132
|
5 |
|
->getResult(); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.