<?php
namespace App\Repository;
use App\Entity\Post;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use DateTime;
/**
* @method Post|null find($id, $lockMode = null, $lockVersion = null)
* @method Post|null findOneBy(array $criteria, array $orderBy = null)
* @method Post[] findAll()
* @method Post[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class PostRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Post::class);
}
public function search(int $page = 1, int $limit = 3, ?string $keywords, ?DateTime $date)
{
$query = $this->createQueryBuilder('s');
$query->where('s.type = :type')->setParameter('type', 'post');
$query->andWhere('s.status = :status')->setParameter('status', 'published');
if($date)
$query->andWhere('s.publishAt = :date')->setParameter('date', $date);
if($keywords && $keywords != '') {
$query->andWhere('LOWER(s.title) LIKE :keywords OR LOWER(s.content) LIKE :keywords')->setParameter('keywords', '%'.strtolower($keywords).'%');
}
$query->orderBy('s.id', 'DESC');
$query->setMaxResults($limit)->setFirstResult((($page-1)*$limit));
return $query->getQuery()->getResult();
}
}