|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use App\Constrain\CommentPublic; |
|
6
|
|
|
use App\CycleDataPaginator; |
|
7
|
|
|
use App\DataPaginatorInterface; |
|
8
|
|
|
use App\Entity\Post; |
|
9
|
|
|
use Cycle\ORM\Select; |
|
10
|
|
|
use Spiral\Database\DatabaseInterface; |
|
11
|
|
|
use Spiral\Database\Driver\DriverInterface; |
|
12
|
|
|
use Spiral\Database\Injection\Fragment; |
|
13
|
|
|
|
|
14
|
|
|
class PostRepository extends Select\Repository |
|
15
|
|
|
{ |
|
16
|
|
|
public function findLastPublic(): DataPaginatorInterface |
|
17
|
|
|
{ |
|
18
|
|
|
$query = $this->select() |
|
19
|
|
|
->load(['user', 'tags']) |
|
20
|
|
|
->orderBy('published_at', 'DESC'); |
|
21
|
|
|
return new CycleDataPaginator($query); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function findArchivedPublic(int $year, int $month): DataPaginatorInterface |
|
25
|
|
|
{ |
|
26
|
|
|
$begin = (new \DateTimeImmutable())->setDate($year, $month, 1)->setTime(0, 0, 0); |
|
27
|
|
|
$end = $begin->setDate($year, $month + 1, 1)->setTime(0, 0, -1); |
|
28
|
|
|
|
|
29
|
|
|
$query = $this->select() |
|
30
|
|
|
->andWhere('published_at', 'between', $begin, $end) |
|
31
|
|
|
->orderBy('published_at', 'DESC') |
|
32
|
|
|
->load(['user', 'tags']); |
|
33
|
|
|
return new CycleDataPaginator($query); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function findByTag($tagId): DataPaginatorInterface |
|
37
|
|
|
{ |
|
38
|
|
|
$query = $this->select() |
|
39
|
|
|
->where(['tags.id' => $tagId]) |
|
40
|
|
|
->orderBy('published_at', 'DESC') |
|
41
|
|
|
->load(['user']); |
|
42
|
|
|
return new CycleDataPaginator($query); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function fullPostPage(string $slug, ?string $userId = null): ?Post |
|
46
|
|
|
{ |
|
47
|
|
|
$query = $this->select() |
|
48
|
|
|
->where(['slug' => $slug]) |
|
49
|
|
|
->load('user', [ |
|
50
|
|
|
'method' => Select::SINGLE_QUERY, |
|
51
|
|
|
]) |
|
52
|
|
|
->load('tags', [ |
|
53
|
|
|
'method' => Select::OUTER_QUERY, |
|
54
|
|
|
]) |
|
55
|
|
|
// force loading in single query with comments |
|
56
|
|
|
->load('comments.user', ['method' => Select::SINGLE_QUERY]) |
|
57
|
|
|
->load('comments', [ |
|
58
|
|
|
'method' => Select::OUTER_QUERY, |
|
59
|
|
|
// not works (default Constrain would not be replaced): |
|
60
|
|
|
'load' => new CommentPublic($userId === null ? null : ['user_id' => $userId]), |
|
61
|
|
|
]); |
|
62
|
|
|
/** @var null|Post $post */ |
|
63
|
|
|
$post = $query->fetchOne(); |
|
64
|
|
|
// /** @var Select\Repository $commentRepo */ |
|
65
|
|
|
// $commentRepo = $this->orm->getRepository(Comment::class); |
|
66
|
|
|
// $commentRepo->select()->load('user')->where('post_id', $post->getId())->fetchAll(); |
|
67
|
|
|
return $post; |
|
68
|
|
|
} |
|
69
|
|
|
/** |
|
70
|
|
|
* @return array Array of Array('Count' => '123', 'Month' => '8', 'Year' => '2019') |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getArchive(): array |
|
73
|
|
|
{ |
|
74
|
|
|
try { |
|
75
|
|
|
if ($this->getDriver() instanceof \Spiral\Database\Driver\SQLite\SQLiteDriver) { |
|
76
|
|
|
return $this->select() |
|
77
|
|
|
->buildQuery() |
|
78
|
|
|
->columns( |
|
79
|
|
|
[ |
|
80
|
|
|
'count(post.id) count', |
|
81
|
|
|
new Fragment('strftime(\'%m\', post.published_at) month'), |
|
82
|
|
|
new Fragment('strftime(\'%Y\', post.published_at) year'), |
|
83
|
|
|
] |
|
84
|
|
|
) |
|
85
|
|
|
->orderBy('year', 'DESC') |
|
86
|
|
|
->orderBy('month', 'DESC') |
|
87
|
|
|
->groupBy('year, month') |
|
88
|
|
|
->fetchAll(); |
|
89
|
|
|
} |
|
90
|
|
|
return $this->select() |
|
91
|
|
|
->buildQuery() |
|
92
|
|
|
->columns( |
|
93
|
|
|
[ |
|
94
|
|
|
'count(post.id) count', |
|
95
|
|
|
new Fragment('extract(month from post.published_at) month'), |
|
96
|
|
|
new Fragment('extract(year from post.published_at) year'), |
|
97
|
|
|
] |
|
98
|
|
|
) |
|
99
|
|
|
->orderBy('year', 'DESC') |
|
100
|
|
|
->orderBy('month', 'DESC') |
|
101
|
|
|
->groupBy('year, month') |
|
102
|
|
|
->fetchAll(); |
|
103
|
|
|
} catch (\Spiral\Database\Exception\StatementException $d) { |
|
104
|
|
|
return []; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
private function getDriver(): DriverInterface |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->select() |
|
111
|
|
|
->getBuilder() |
|
112
|
|
|
->getLoader() |
|
113
|
|
|
->getSource() |
|
114
|
|
|
->getDatabase() |
|
115
|
|
|
->getDriver(DatabaseInterface::READ); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|