src/Repository/Hotels/HotelsRepository.php line 57

Open in your IDE?
  1. <?php
  2. namespace App\Repository\Hotels;
  3. use App\Data\SearchhotelsData;
  4. use App\Entity\Hotels\Hotels;
  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 Hotels|null find($id, $lockMode = null, $lockVersion = null)
  13.  * @method Hotels|null findOneBy(array $criteria, array $orderBy = null)
  14.  * @method Hotels[]    findAll()
  15.  * @method Hotels[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  16.  */
  17. class HotelsRepository extends ServiceEntityRepository
  18. {
  19.     /**
  20.      * @var PaginatorInterface
  21.      */
  22.     private $paginator;
  23.     public function __construct(ManagerRegistry $registryPaginatorInterface $paginator)
  24.     {
  25.         parent::__construct($registryHotels::class);
  26.         $this->paginator $paginator;
  27.     }
  28.     /**
  29.      * @param $page
  30.      * @return PaginationInterface
  31.      */
  32.     public function findHotelsByActivated($page): PaginationInterface
  33.     {
  34.         $query $this->createQueryBuilder('h')
  35.             ->select('h''c')
  36.             ->leftJoin('h.cities''c')
  37.             ->andWhere('h.activated = 1')
  38.             ->andWhere('h.online = 1')
  39.             ->orderBy('h.id''DESC')
  40.             ->getQuery();
  41.         return $this->paginator->paginate(
  42.             $query,
  43.             $page,
  44.             16
  45.         );
  46.     }
  47.     /**
  48.      * @param SearchhotelsData $search
  49.      * @return PaginationInterface
  50.      */
  51.     public function findSearch(SearchhotelsData $search): PaginationInterface
  52.     {
  53.         $query $this->getSearchQuery($search)->getQuery();
  54.         return $this->paginator->paginate(
  55.             $query,
  56.             $search->page,
  57.             16
  58.         );
  59.     }
  60.     private function getSearchQuery(SearchhotelsData $search$ignorePrice false): QueryBuilder{
  61.         $query =  $this->createQueryBuilder('h')
  62.             ->select('h''a''c')
  63.             ->leftJoin('h.annonceshotels''a')
  64.             ->leftJoin('h.cities''c')
  65.             ->andWhere('h.activated = TRUE')
  66.             ->andWhere('h.online = TRUE')
  67.             ->orderBy('h.ordre''ASC')
  68.         ;
  69.         if (!empty($search->q)){
  70.             $query $query
  71.                 ->andWhere('h.name LIKE :q')
  72.                 ->setParameter('q'"%{$search->q}%")
  73.             ;
  74.         }
  75.         if (!empty($search->ville)){
  76.             $query $query
  77.                 ->andWhere('h.cities IN (:citie)')
  78.                 ->setParameter('citie'$search->ville)
  79.             ;
  80.         }
  81.         if (!empty($search->min)){
  82.             $query $query
  83.                 ->andWhere('a.price >= :min')
  84.                 ->setParameter('min'$search->min)
  85.             ;
  86.         }
  87.         if (!empty($search->max)){
  88.             $query $query
  89.                 ->andWhere('a.price <= :max')
  90.                 ->setParameter('max'$search->max)
  91.             ;
  92.         }
  93.         return $query;
  94.     }
  95.     public function getHotelRandom(Hotels $hotels$limit)
  96.     {
  97.         return $this->createQueryBuilder('h')
  98.             ->andWhere('h.id != :hotel')
  99.             ->setParameter('hotel'$hotels)
  100.             ->orderBy('RAND()')
  101.             ->setMaxResults($limit)
  102.             ->getQuery()
  103.             ->getResult()
  104.             ;
  105.     }
  106.     /**
  107.      * @return int|mixed|string|null
  108.      * @throws NonUniqueResultException
  109.      */
  110.     public function featured()
  111.     {
  112.         return $this->createQueryBuilder('h')
  113.             ->andWhere('h.online = 1')
  114.             ->andWhere('h.activated = 1')
  115.             ->orderBy('RAND()')
  116.             ->setMaxResults(1)
  117.             ->getQuery()
  118.             ->getOneOrNullResult()
  119.             ;
  120.     }
  121.     // /**
  122.     //  * @return Hotels[] Returns an array of Hotels objects
  123.     //  */
  124.     /*
  125.     public function findByExampleField($value)
  126.     {
  127.         return $this->createQueryBuilder('h')
  128.             ->andWhere('h.exampleField = :val')
  129.             ->setParameter('val', $value)
  130.             ->orderBy('h.id', 'ASC')
  131.             ->setMaxResults(10)
  132.             ->getQuery()
  133.             ->getResult()
  134.         ;
  135.     }
  136.     */
  137.     /*
  138.     public function findOneBySomeField($value): ?Hotels
  139.     {
  140.         return $this->createQueryBuilder('h')
  141.             ->andWhere('h.exampleField = :val')
  142.             ->setParameter('val', $value)
  143.             ->getQuery()
  144.             ->getOneOrNullResult()
  145.         ;
  146.     }
  147.     */
  148. }