<?php
namespace App\Repository;
use App\Entity\CoachingSession;
use App\Entity\PostCategory;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method CoachingSession|null find($id, $lockMode = null, $lockVersion = null)
* @method CoachingSession|null findOneBy(array $criteria, array $orderBy = null)
* @method CoachingSession[] findAll()
* @method CoachingSession[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class CoachingSessionRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, CoachingSession::class);
}
public function search(int $page = 1, int $limit = 3, ?string $keywords)
{
$query = $this->createQueryBuilder('s');
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();
}
}