<?php
namespace App\Repository;
use App\Entity\Appointment;
use App\Entity\Doctor;
use Boab\CmsBundle\Service\Pagination;
use Boab\CmsBundle\Service\PaginationInterface;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Iterator;
/**
* @extends ServiceEntityRepository<Doctor>
*
* @method Doctor|null find($id, $lockMode = null, $lockVersion = null)
* @method Doctor|null findOneBy(array $criteria, array $orderBy = null)
* @method Doctor[] findAll()
* @method Doctor[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class DoctorRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry, private PaginationInterface $pagination)
{
parent::__construct($registry, Doctor::class);
}
public function save(Doctor $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function remove(Doctor $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function findAllDoctors()
{
$qb = $this->createQueryBuilder('d')
->orderBy('d.id', 'ASC')
->setMaxResults(10)
->getQuery();
return $this->pagination->paginate($qb);
}
public function findAllByLocation(string $lat, string $lng, int $distance=0, $name)
{
$qb = $this->createQueryBuilder('d');
$qb->select('d', 'SUM(r.rating) as totalRating', 'COUNT(r) as numberOfRatings')
->leftJoin('d.reviews', 'r');
if ($lat && $lng) {
$qb->addSelect('
(6371 *
ACOS(
COS(RADIANS(:lat)) *
COS(RADIANS(d.latitude)) *
COS(RADIANS(d.longitude) - RADIANS(:lng)) +
SIN(RADIANS(:lat)) *
SIN(RADIANS(d.latitude))
))
AS HIDDEN distance'
);
$qb->setParameter('lat', $lat);
$qb->setParameter('lng', $lng);
$qb->orderBy('distance');
if ($distance > 0) {
$qb->having('distance <= :distance');
$qb->setParameter('distance', $distance);
}
}
/*
$qb->addSelect('
(6371 *
ACOS(
COS(RADIANS(:lat)) *
COS(RADIANS(d.latitude)) *
COS(RADIANS(d.longitude) - RADIANS(:lng)) +
SIN(RADIANS(:lat)) *
SIN(RADIANS(d.latitude))
))
AS HIDDEN distance'
);
*/
if($name){
$qb->andWhere("CONCAT(d.firstname, ' ', d.lastname) LIKE :name");
$qb->setParameter('name', '%' . $name . '%');
}
$qb->groupBy('d');
return $this->pagination->paginate($qb->getQuery());
}
}