|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Albert221\Blog\Repository\Database; |
|
4
|
|
|
|
|
5
|
|
|
use Albert221\Blog\Entity\Post; |
|
6
|
|
|
use Albert221\Blog\Repository\PostRepositoryInterface; |
|
7
|
|
|
use Doctrine\ORM\EntityRepository; |
|
8
|
|
|
|
|
9
|
|
|
class PostRepository extends EntityRepository implements PostRepositoryInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* {@inheritdoc} |
|
13
|
|
|
*/ |
|
14
|
|
|
public function count() |
|
15
|
|
|
{ |
|
16
|
|
|
$query = $this->createQueryBuilder('p') |
|
17
|
|
|
->select('count(p.id)') |
|
18
|
|
|
->getQuery(); |
|
19
|
|
|
|
|
20
|
|
|
return $query->getSingleScalarResult(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* {@inheritdoc} |
|
25
|
|
|
*/ |
|
26
|
|
|
public function paginated($page, $perPage) |
|
27
|
|
|
{ |
|
28
|
|
|
$first = ($page - 1) * $perPage; |
|
29
|
|
|
|
|
30
|
|
|
$query = $this->createQueryBuilder('p') |
|
31
|
|
|
->setFirstResult($first) |
|
32
|
|
|
->setMaxResults($perPage) |
|
33
|
|
|
->getQuery(); |
|
34
|
|
|
|
|
35
|
|
|
return $query->getResult(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
|
|
public function bySlug($slug) |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->findOneBy(['slug' => $slug]); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
|
|
public function byCategoryCount($slug) |
|
50
|
|
|
{ |
|
51
|
|
|
$qb = $this->createQueryBuilder('p'); |
|
52
|
|
|
|
|
53
|
|
|
$query = $qb->select('count(p.id)') |
|
54
|
|
|
->join('p.category', 'c') |
|
55
|
|
|
->where($qb->expr()->eq('c.slug', ':category')) |
|
56
|
|
|
->setParameter(':category', $slug) |
|
57
|
|
|
->getQuery(); |
|
58
|
|
|
|
|
59
|
|
|
return $query->getSingleScalarResult(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
public function byCategory($slug, $page, $perPage) |
|
66
|
|
|
{ |
|
67
|
|
|
$first = ($page - 1) * $perPage; |
|
68
|
|
|
|
|
69
|
|
|
$qb = $this->createQueryBuilder('p'); |
|
70
|
|
|
|
|
71
|
|
|
$query = $qb->join('p.category', 'c') |
|
72
|
|
|
->where($qb->expr()->eq('c.slug', ':category')) |
|
73
|
|
|
->setParameter(':category', $slug) |
|
74
|
|
|
->setFirstResult($first) |
|
75
|
|
|
->setMaxResults($perPage) |
|
76
|
|
|
->getQuery(); |
|
77
|
|
|
|
|
78
|
|
|
return $query->getResult(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function byTagCount($slug) |
|
82
|
|
|
{ |
|
83
|
|
|
$qb = $this->createQueryBuilder('p'); |
|
84
|
|
|
|
|
85
|
|
|
$query = $qb->select('count(p.id)') |
|
86
|
|
|
->join('p.tags', 't') |
|
87
|
|
|
->where($qb->expr()->eq('t.slug', ':tag')) |
|
88
|
|
|
->setParameter(':tag', $slug) |
|
89
|
|
|
->getQuery(); |
|
90
|
|
|
|
|
91
|
|
|
return $query->getSingleScalarResult(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* {@inheritdoc} |
|
96
|
|
|
*/ |
|
97
|
|
|
public function byTag($slug, $page, $perPage) |
|
98
|
|
|
{ |
|
99
|
|
|
$first = ($page - 1) * $perPage; |
|
100
|
|
|
|
|
101
|
|
|
$qb = $this->createQueryBuilder('p'); |
|
102
|
|
|
|
|
103
|
|
|
$query = $qb->join('p.tags', 't') |
|
104
|
|
|
->where($qb->expr()->eq('t.slug', ':tag')) |
|
105
|
|
|
->setParameter(':tag', $slug) |
|
106
|
|
|
->setFirstResult($first) |
|
107
|
|
|
->setMaxResults($perPage) |
|
108
|
|
|
->getQuery(); |
|
109
|
|
|
|
|
110
|
|
|
return $query->getResult(); |
|
111
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* {@inheritdoc} |
|
116
|
|
|
*/ |
|
117
|
|
|
public function save(Post $post) |
|
118
|
|
|
{ |
|
119
|
|
|
$this->getEntityManager()->persist($post); |
|
120
|
|
|
$this->getEntityManager()->flush(); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|