zend-blog-3-backend

Форк
0
/
PostRepository.php 
93 строки · 2.1 Кб
1
<?php
2

3
namespace App\Repository;
4

5
use App\Entity\Post;
6
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
7
use Doctrine\ORM\Query;
8
use Doctrine\Persistence\ManagerRegistry;
9

10
/**
11
 * PostRepository
12
 *
13
 * This class was generated by the Doctrine ORM. Add your own custom
14
 * repository methods below.
15
 *
16
 * @method Post|null find($id, $lockMode = null, $lockVersion = null)
17
 */
18
class PostRepository extends ServiceEntityRepository
19
{
20
    const ITERATION_STEP = 15;
21

22
    /**
23
     * @param ManagerRegistry $registry
24
     */
25
    public function __construct(ManagerRegistry $registry)
26
    {
27
        parent::__construct($registry, Post::class);
28
    }
29

30
    /**
31
     * @return Query
32
     */
33
    public function getListQuery(): Query
34
    {
35
        $qb = $this->createQueryBuilder('e');
36
        $qb
37
            ->select('e', 'c', 't')
38
            ->innerJoin('e.category', 'c')
39
            ->leftJoin('e.tags', 't')
40
            ->orderBy('e.id', 'DESC')
41
        ;
42

43
        return $qb->getQuery();
44
    }
45

46
    /**
47
     * @param int $i
48
     *
49
     * @return Post[]
50
     */
51
    public function getPostsForIteration($i): array
52
    {
53
        $qb = $this->createQueryBuilder('p');
54

55
        $qb
56
            ->orderBy('p.id')
57
            ->setFirstResult($i * self::ITERATION_STEP)
58
            ->setMaxResults(self::ITERATION_STEP)
59
        ;
60

61
        return $qb->getQuery()->getResult();
62
    }
63

64
    public function increaseViewCounter(int $articleId, int $cnt)
65
    {
66
        $qb = $this->createQueryBuilder('p');
67
        $qb
68
            ->update()
69
            ->set('p.viewsCount', '(p.viewsCount + :cnt)')
70
            ->where($qb->expr()->eq('p.id', ':id'))
71
            ->setParameter('id', $articleId)
72
            ->setParameter('cnt', $cnt)
73
        ;
74

75
        $qb->getQuery()->execute();
76
    }
77

78
    /**
79
     * @param int $codeId
80
     *
81
     * @return Post[]
82
     */
83
    public function getPostsByCodeSnippet(int $codeId): array
84
    {
85
        $qb = $this->createQueryBuilder('p');
86
        $qb
87
            ->where($qb->expr()->like('p.rawText', ':code'))
88
            ->setParameter('code', sprintf('%%!<code>%d!%%', $codeId))
89
        ;
90

91
        return $qb->getQuery()->getResult();
92
    }
93
}
94

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.