src/Repository/DealRepository.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Club;
  4. use App\Entity\Deal;
  5. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. /**
  8.  * @extends ServiceEntityRepository<Deal>
  9.  *
  10.  * @method Deal|null find($id, $lockMode = null, $lockVersion = null)
  11.  * @method Deal|null findOneBy(array $criteria, array $orderBy = null)
  12.  * @method Deal[]    findAll()
  13.  * @method Deal[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  14.  */
  15. class DealRepository extends ServiceEntityRepository
  16. {
  17.     public function __construct(ManagerRegistry $registry)
  18.     {
  19.         parent::__construct($registryDeal::class);
  20.     }
  21.     public function save(Deal $entitybool $flush false): void
  22.     {
  23.         $this->getEntityManager()->persist($entity);
  24.         if ($flush) {
  25.             $this->getEntityManager()->flush();
  26.         }
  27.     }
  28.     public function remove(Deal $entitybool $flush false): void
  29.     {
  30.         $this->getEntityManager()->remove($entity);
  31.         if ($flush) {
  32.             $this->getEntityManager()->flush();
  33.         }
  34.     }
  35.     public function findByDistanceAndClub(
  36.         ?float $ownLat,
  37.         ?float $ownLon,
  38.         bool $ifClub,
  39.         array $params = [],
  40.         int $distance null,
  41.         Club $club null,
  42.         bool $ifLonOrLat,
  43.     )
  44.     {
  45.         $qb $this->createQueryBuilder('d');
  46.             $qb->leftJoin('d.company''comp')
  47.                 ->leftJoin('comp.address''a');
  48.         if ($ifLonOrLat) {
  49.             if ($ownLat !== null && $ownLon !== null) {
  50.                 $qb->addSelect(
  51.                     "CASE WHEN (a.id IS NOT NULL) THEN (111.12 * SQRT(
  52.                         POWER(
  53.                             (a.longi - :lonB)
  54.                             *
  55.                             COS(
  56.                                 (a.lat + :latB)
  57.                                 /
  58.                                 2
  59.                             )
  60.                         , 2)
  61.                         +
  62.                         POWER(
  63.                             (:latB - a.lat)
  64.                         , 2)
  65.                     )) ELSE 100000 END AS distance")
  66.                     ->setParameter('lonB'$ownLon)
  67.                     ->setParameter('latB'$ownLat);
  68.             }
  69.             if ($distance !== null) {
  70.                 $qb->andWhere('
  71.                 CASE WHEN (a.id IS NOT NULL) THEN (111.12 * SQRT(
  72.                     POWER(
  73.                         (a.longi - :lonB) * COS((a.lat + :latB) / 2), 2) +
  74.                     POWER((:latB - a.lat), 2)
  75.                 )) ELSE 100000 END
  76.                 <= :dist')
  77.                     ->setParameter('dist'$distance);
  78.             }
  79.             if ($ifClub) {
  80.                 $qb->andWhere('d.club = :club')
  81.                     ->setParameter('club'$club);
  82.             }
  83.         }
  84.         if (!empty($params) && array_key_exists('query'$params) && !empty($params['query'])) {
  85.             $qb->andWhere('d.title LIKE :query OR d.description LIKE :query OR comp.fti LIKE :query')
  86.                 ->setParameter('query''%'.$params['query'].'%');
  87.         }
  88.         $page $params['page'] ?? 1;
  89.         if ($page <= 0) {
  90.             $page 1;
  91.         }
  92.         $limit $params['limit'] ?? 10;
  93.         if ($limit <= 0) {
  94.             $limit 10;
  95.         }
  96.         $qb->setFirstResult(($page 1) * $limit)->setMaxResults($limit);
  97.         return $qb->getQuery()->getResult();
  98.     }
  99. //    /**
  100. //     * @return Deal[] Returns an array of Deal objects
  101. //     */
  102. //    public function findByExampleField($value): array
  103. //    {
  104. //        return $this->createQueryBuilder('d')
  105. //            ->andWhere('d.exampleField = :val')
  106. //            ->setParameter('val', $value)
  107. //            ->orderBy('d.id', 'ASC')
  108. //            ->setMaxResults(10)
  109. //            ->getQuery()
  110. //            ->getResult()
  111. //        ;
  112. //    }
  113. //    public function findOneBySomeField($value): ?Deal
  114. //    {
  115. //        return $this->createQueryBuilder('d')
  116. //            ->andWhere('d.exampleField = :val')
  117. //            ->setParameter('val', $value)
  118. //            ->getQuery()
  119. //            ->getOneOrNullResult()
  120. //        ;
  121. //    }
  122. }