src/Repository/BlogRepository.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Blog;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\ORM\OptimisticLockException;
  6. use Doctrine\ORM\ORMException;
  7. use Doctrine\ORM\Tools\Pagination\Paginator;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. /**
  10.  * @extends ServiceEntityRepository<Blog>
  11.  *
  12.  * @method Blog|null find($id, $lockMode = null, $lockVersion = null)
  13.  * @method Blog|null findOneBy(array $criteria, array $orderBy = null)
  14.  * @method Blog[]    findAll()
  15.  * @method Blog[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  16.  */
  17. class BlogRepository extends ServiceEntityRepository {
  18.     public const PAGINATOR_PER_PAGE = 10;
  19.     public function __construct(ManagerRegistry $registry) {
  20.         parent::__construct($registry, Blog::class);
  21.     }
  22.     /**
  23.      * @throws ORMException
  24.      * @throws OptimisticLockException
  25.      */
  26.     public function add(Blog $entity, bool $flush = true): void {
  27.         $this->_em->persist($entity);
  28.         if ($flush) {
  29.             $this->_em->flush();
  30.         }
  31.     }
  32.     /**
  33.      * @throws ORMException
  34.      * @throws OptimisticLockException
  35.      */
  36.     public function remove(Blog $entity, bool $flush = true): void {
  37.         $this->_em->remove($entity);
  38.         if ($flush) {
  39.             $this->_em->flush();
  40.         }
  41.     }
  42.     public function getBlogPaginator(Blog $blog, int $offset): Paginator {
  43.         $query = $this->createQueryBuilder('c')
  44.                 ->andWhere('c.conference = :conference')
  45.                 ->setParameter('conference', $blog)
  46.                 ->orderBy('c.createdAt', 'DESC')
  47.                 ->setMaxResults(self::PAGINATOR_PER_PAGE)
  48.                 ->setFirstResult($offset)
  49.                 ->getQuery()
  50.         ;
  51.         return new Paginator($query);
  52.     }
  53. // /**
  54. //  * @return Blog[] Returns an array of Blog objects
  55. //  */
  56.     /*
  57.       public function findByExampleField($value)
  58.       {
  59.       return $this->createQueryBuilder('b')
  60.       ->andWhere('b.exampleField = :val')
  61.       ->setParameter('val', $value)
  62.       ->orderBy('b.id', 'ASC')
  63.       ->setMaxResults(10)
  64.       ->getQuery()
  65.       ->getResult()
  66.       ;
  67.       }
  68.      */
  69.     /*
  70.       public function findOneBySomeField($value): ?Blog
  71.       {
  72.       return $this->createQueryBuilder('b')
  73.       ->andWhere('b.exampleField = :val')
  74.       ->setParameter('val', $value)
  75.       ->getQuery()
  76.       ->getOneOrNullResult()
  77.       ;
  78.       }
  79.      */
  80. }