<?php
namespace App\Repository\Law;
use App\Entity\Law\Law;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method Law|null find($id, $lockMode = null, $lockVersion = null)
* @method Law|null findOneBy(array $criteria, array $orderBy = null)
* @method Law[] findAll()
* @method Law[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class LawRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Law::class);
}
/**
* @param string $localeCode: The request's locale
* @return Law[] Returns an array of Law objects
*/
public function findLawsQuery($section, $keyword)
{
$qb = $this->createQueryBuilder('l')
->select('l.id', 'l.author', 'l.date', 'l.image.name as image', 'l.title', 'l.link', 'l.fileDetails.name as pdf', 'l.description', 'l.createdAt', 's.name as section', 's.nameEng as sectionEng')
->innerJoin('l.section', 's')
->orderBy('l.createdAt', 'DESC')
;
if ($keyword) {
$qb->andWhere('(l.title LIKE :keyword OR l.author LIKE :keyword OR l.description LIKE :keyword OR s.name LIKE :keyword)')
->setParameter('keyword', '%' . $keyword . '%');
} elseif ($section) {
$qb->andWhere('s.id = :id')->setParameter('id', $section->getId());
}
return $qb->getQuery();
}
public function findRecent($limit = 10)
{
return $this->createQueryBuilder('l')
->orderBy('l.createdAt', 'DESC')
->setMaxResults($limit)
->getQuery()
->getResult()
;
}
}