-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommentRepository.php
93 lines (78 loc) 路 2.25 KB
/
CommentRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace App\Repository;
use App\Entity\User;
use App\Entity\Comment;
use Doctrine\ORM\Query;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
/**
* @method Comment|null find($id, $lockMode = null, $lockVersion = null)
* @method Comment|null findOneBy(array $criteria, array $orderBy = null)
* @method Comment[] findAll()
* @method Comment[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class CommentRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Comment::class);
}
public function resetIndex()
{
$connection = $this->getEntityManager()->getConnection();
$connection->exec("ALTER TABLE comment AUTO_INCREMENT = 1;");
}
/**
* @return Comment[]
*/
public function findItems(int $index = 1, $idFigure): array
{
return $this->getQueryDesc($index, $idFigure)
->getQuery()
->getResult();
}
public function findMoreItems(int $index, $idFigure): array
{
if ($index !== 1) {
return $this->getQueryDesc($index, $idFigure)
->getQuery()
->getResult(Query::HYDRATE_ARRAY);
}
}
public function countAll($idFigure)
{
return intval($this->createQueryBuilder('p')
->select('COUNT(p)')
->where('p.figure = :id_figure')
->setParameter('id_figure', $idFigure)
->getQuery()->getSingleScalarResult());
}
private function getQueryDesc(int $index, $idFigure)
{
$total = $this->countAll($idFigure);
$nbResultsPerPage = 10;
$nbGroups = round($total / $nbResultsPerPage);
$start = 0;
$intervals = [];
if (!$total || !$nbGroups) {
return $this->createQueryBuilder('p')
->orderBy('p.created_at', 'DESC')
->where('p.figure = :id_figure')
->setParameter('id_figure', $idFigure)
->setMaxResults($nbResultsPerPage);
}
for ($i = 1; $i <= $nbGroups; $i++) {
$intervals[$i] = [
'start' => $start
];
$start += $nbResultsPerPage;
}
$interval = $intervals[$index];
return $this->createQueryBuilder('p')
->orderBy('p.created_at', 'DESC')
->where('p.figure = :id_figure')
->setParameter('id_figure', $idFigure)
->setFirstResult($interval['start'])
->setMaxResults($nbResultsPerPage);
}
}