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