zend-blog-3-backend
93 строки · 2.1 Кб
1<?php
2
3namespace App\Repository;4
5use App\Entity\Post;6use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;7use Doctrine\ORM\Query;8use 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*/
18class PostRepository extends ServiceEntityRepository19{
20const ITERATION_STEP = 15;21
22/**23* @param ManagerRegistry $registry
24*/
25public function __construct(ManagerRegistry $registry)26{27parent::__construct($registry, Post::class);28}29
30/**31* @return Query
32*/
33public function getListQuery(): Query34{35$qb = $this->createQueryBuilder('e');36$qb37->select('e', 'c', 't')38->innerJoin('e.category', 'c')39->leftJoin('e.tags', 't')40->orderBy('e.id', 'DESC')41;42
43return $qb->getQuery();44}45
46/**47* @param int $i
48*
49* @return Post[]
50*/
51public function getPostsForIteration($i): array52{53$qb = $this->createQueryBuilder('p');54
55$qb56->orderBy('p.id')57->setFirstResult($i * self::ITERATION_STEP)58->setMaxResults(self::ITERATION_STEP)59;60
61return $qb->getQuery()->getResult();62}63
64public function increaseViewCounter(int $articleId, int $cnt)65{66$qb = $this->createQueryBuilder('p');67$qb68->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*/
83public function getPostsByCodeSnippet(int $codeId): array84{85$qb = $this->createQueryBuilder('p');86$qb87->where($qb->expr()->like('p.rawText', ':code'))88->setParameter('code', sprintf('%%!<code>%d!%%', $codeId))89;90
91return $qb->getQuery()->getResult();92}93}
94