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