<?php
namespace App\Repository;
use App\Entity\Club;
use App\Entity\Deal;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Deal>
*
* @method Deal|null find($id, $lockMode = null, $lockVersion = null)
* @method Deal|null findOneBy(array $criteria, array $orderBy = null)
* @method Deal[] findAll()
* @method Deal[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class DealRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Deal::class);
}
public function save(Deal $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function remove(Deal $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function findByDistanceAndClub(
?float $ownLat,
?float $ownLon,
bool $ifClub,
array $params = [],
int $distance = null,
Club $club = null,
bool $ifLonOrLat,
)
{
$qb = $this->createQueryBuilder('d');
$qb->leftJoin('d.company', 'comp')
->leftJoin('comp.address', 'a');
if ($ifLonOrLat) {
if ($ownLat !== null && $ownLon !== null) {
$qb->addSelect(
"CASE WHEN (a.id IS NOT NULL) THEN (111.12 * SQRT(
POWER(
(a.longi - :lonB)
*
COS(
(a.lat + :latB)
/
2
)
, 2)
+
POWER(
(:latB - a.lat)
, 2)
)) ELSE 100000 END AS distance")
->setParameter('lonB', $ownLon)
->setParameter('latB', $ownLat);
}
if ($distance !== null) {
$qb->andWhere('
CASE WHEN (a.id IS NOT NULL) THEN (111.12 * SQRT(
POWER(
(a.longi - :lonB) * COS((a.lat + :latB) / 2), 2) +
POWER((:latB - a.lat), 2)
)) ELSE 100000 END
<= :dist')
->setParameter('dist', $distance);
}
if ($ifClub) {
$qb->andWhere('d.club = :club')
->setParameter('club', $club);
}
}
if (!empty($params) && array_key_exists('query', $params) && !empty($params['query'])) {
$qb->andWhere('d.title LIKE :query OR d.description LIKE :query OR comp.fti LIKE :query')
->setParameter('query', '%'.$params['query'].'%');
}
$page = $params['page'] ?? 1;
if ($page <= 0) {
$page = 1;
}
$limit = $params['limit'] ?? 10;
if ($limit <= 0) {
$limit = 10;
}
$qb->setFirstResult(($page - 1) * $limit)->setMaxResults($limit);
return $qb->getQuery()->getResult();
}
// /**
// * @return Deal[] Returns an array of Deal objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('d')
// ->andWhere('d.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('d.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?Deal
// {
// return $this->createQueryBuilder('d')
// ->andWhere('d.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}