src/Repository/Publication/PublicationRepository.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Repository\Publication;
  3. use App\Entity\Publication\Publication;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. /**
  7.  * @extends ServiceEntityRepository<Publication>
  8.  *
  9.  * @method Publication|null find($id, $lockMode = null, $lockVersion = null)
  10.  * @method Publication|null findOneBy(array $criteria, array $orderBy = null)
  11.  * @method Publication[]    findAll()
  12.  * @method Publication[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  13.  */
  14. class PublicationRepository extends ServiceEntityRepository
  15. {
  16.     public function __construct(ManagerRegistry $registry)
  17.     {
  18.         parent::__construct($registryPublication::class);
  19.     }
  20.     public function add(Publication $entitybool $flush false): void
  21.     {
  22.         $this->getEntityManager()->persist($entity);
  23.         if ($flush) {
  24.             $this->getEntityManager()->flush();
  25.         }
  26.     }
  27.     public function remove(Publication $entitybool $flush false): void
  28.     {
  29.         $this->getEntityManager()->remove($entity);
  30.         if ($flush) {
  31.             $this->getEntityManager()->flush();
  32.         }
  33.     }
  34.     /**
  35.      * @param string $localeCode: The request's locale
  36.      * @return Publication[] Returns an array of publication objects
  37.      */
  38.     public function findPublicationsQuery($keyword)
  39.     {
  40.         $qb $this->createQueryBuilder('p')
  41.             ->select('p.id''p.author''p.date''p.image.name as image''p.title''p.link''p.fileDetails.name as pdf''p.description''p.createdAt')
  42.             ->orderBy('p.createdAt''DESC')
  43.         ;
  44.         if ($keyword) {
  45.             $qb->andWhere('(p.title LIKE :keyword OR p.author LIKE :keyword OR p.description LIKE :keyword)')
  46.                 ->setParameter('keyword''%' $keyword '%');
  47.         }
  48.         return $qb->getQuery();
  49.     }
  50.     public function findRecent($limit 10)
  51.     {
  52.         return $this->createQueryBuilder('p')
  53.             ->orderBy('p.createdAt''DESC')
  54.             ->setMaxResults($limit)
  55.             ->getQuery()
  56.             ->getResult()
  57.         ;
  58.     }
  59. }