src/Repository/Restaurants/RestaurantsRepository.php line 111

Open in your IDE?
  1. <?php
  2. namespace App\Repository\Restaurants;
  3. use App\Data\SearchrestaurantData;
  4. use App\Entity\Restaurants\Restaurants;
  5. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Doctrine\ORM\NonUniqueResultException;
  8. use Doctrine\ORM\QueryBuilder;
  9. use Knp\Component\Pager\Pagination\PaginationInterface;
  10. use Knp\Component\Pager\PaginatorInterface;
  11. /**
  12.  * @method Restaurants|null find($id, $lockMode = null, $lockVersion = null)
  13.  * @method Restaurants|null findOneBy(array $criteria, array $orderBy = null)
  14.  * @method Restaurants[]    findAll()
  15.  * @method Restaurants[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  16.  */
  17. class RestaurantsRepository extends ServiceEntityRepository
  18. {
  19.     /**
  20.      * @var PaginatorInterface
  21.      */
  22.     private $paginator;
  23.     public function __construct(ManagerRegistry $registryPaginatorInterface $paginator)
  24.     {
  25.         parent::__construct($registryRestaurants::class);
  26.         $this->paginator $paginator;
  27.     }
  28.     /**
  29.      * @param $page
  30.      * @return PaginationInterface
  31.      */
  32.     public function findRestaurantsByActivated($page): PaginationInterface
  33.     {
  34.         $query $this->createQueryBuilder('r')
  35.             ->select('r')
  36.             ->andWhere('r.activated = 1')
  37.             ->andWhere('r.online = 1')
  38.             ->orderBy('r.id''DESC')
  39.             ->getQuery();
  40.         return $this->paginator->paginate(
  41.             $query,
  42.             $page,
  43.             16
  44.         );
  45.     }
  46.     /**
  47.      * @param SearchrestaurantData $search
  48.      * @return PaginationInterface
  49.      */
  50.     public function findSearch(SearchrestaurantData $search): PaginationInterface
  51.     {
  52.         $query $this->getSearchQuery($search)->getQuery();
  53.         return $this->paginator->paginate(
  54.             $query,
  55.             $search->page,
  56.             16
  57.         );
  58.     }
  59.     /**
  60.      * @param SearchrestaurantData $search
  61.      * @return QueryBuilder
  62.      */
  63.     private function getSearchQuery(SearchrestaurantData $search): QueryBuilder{
  64.         $query =  $this->createQueryBuilder('r')
  65.             ->select('r''c''s''cm')
  66.             ->leftJoin('r.cities''c')
  67.             ->leftJoin('r.specialites''s')
  68.             ->leftJoin('c.communes''cm')
  69.             ->andWhere('r.activated = TRUE')
  70.             ->andWhere('r.online = TRUE')
  71.             ->orderBy('r.ordre''ASC')
  72.         ;
  73.         if (!empty($search->q)){
  74.             $query $query
  75.                 ->andWhere('r.name LIKE :q')
  76.                 ->setParameter('q'"%{$search->q}%")
  77.             ;
  78.         }
  79.         if (!empty($search->villes)){
  80.             $query $query
  81.                 ->andWhere('c.id = :citie')
  82.                 ->setParameter('citie'$search->villes->getId())
  83.             ;
  84.         }
  85.         if (!empty($search->communes)){
  86.             $query $query
  87.                 ->andWhere('cm.id = :commune')
  88.                 ->setParameter('commune'$search->communes->getId())
  89.             ;
  90.         }
  91.         if (!empty($search->specialite)){
  92.             $query $query
  93.                 ->andWhere('s.id IN (:specialite)')
  94.                 ->setParameter('specialite'$search->specialite)
  95.             ;
  96.         }
  97.         return $query;
  98.     }
  99.     public function getRestaurantsRandom(Restaurants $restaurants$limit)
  100.     {
  101.         return $this->createQueryBuilder('r')
  102.             ->andWhere('r.id != :restaurant')
  103.             ->setParameter('restaurant'$restaurants)
  104.             ->orderBy('RAND()')
  105.             ->setMaxResults($limit)
  106.             ->getQuery()
  107.             ->getResult()
  108.             ;
  109.     }
  110.     /**
  111.      * @return int|mixed|string|null
  112.      * @throws NonUniqueResultException
  113.      */
  114.     public function featured()
  115.     {
  116.         return $this->createQueryBuilder('r')
  117.             ->andWhere('r.online = 1')
  118.             ->andWhere('r.activated = 1')
  119.             ->orderBy('RAND()')
  120.             ->setMaxResults(1)
  121.             ->getQuery()
  122.             ->getOneOrNullResult()
  123.             ;
  124.     }
  125. }