<?php
namespace App\Repository\Publication;
use App\Entity\Publication\Publication;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Publication>
*
* @method Publication|null find($id, $lockMode = null, $lockVersion = null)
* @method Publication|null findOneBy(array $criteria, array $orderBy = null)
* @method Publication[] findAll()
* @method Publication[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class PublicationRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Publication::class);
}
public function add(Publication $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function remove(Publication $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
/**
* @param string $localeCode: The request's locale
* @return Publication[] Returns an array of publication objects
*/
public function findPublicationsQuery($keyword)
{
$qb = $this->createQueryBuilder('p')
->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')
->orderBy('p.createdAt', 'DESC')
;
if ($keyword) {
$qb->andWhere('(p.title LIKE :keyword OR p.author LIKE :keyword OR p.description LIKE :keyword)')
->setParameter('keyword', '%' . $keyword . '%');
}
return $qb->getQuery();
}
public function findRecent($limit = 10)
{
return $this->createQueryBuilder('p')
->orderBy('p.createdAt', 'DESC')
->setMaxResults($limit)
->getQuery()
->getResult()
;
}
}