*/ final class EmployeeContractPeriodRepository extends ServiceEntityRepository { public function __construct(ManagerRegistry $registry) { parent::__construct($registry, EmployeeContractPeriod::class); } /** * @param list $employees * * @return list */ public function findByEmployeesAndDateRange(array $employees, DateTimeImmutable $from, DateTimeImmutable $to): array { if ([] === $employees) { return []; } return $this->createQueryBuilder('p') ->andWhere('p.employee IN (:employees)') ->andWhere('p.startDate <= :to') ->andWhere('p.endDate IS NULL OR p.endDate >= :from') ->setParameter('employees', $employees) ->setParameter('from', $from) ->setParameter('to', $to) ->orderBy('p.startDate', 'ASC') ->getQuery() ->getResult() ; } public function findOneCoveringDate(Employee $employee, DateTimeImmutable $date): ?EmployeeContractPeriod { return $this->createQueryBuilder('p') ->andWhere('p.employee = :employee') ->andWhere('p.startDate <= :date') ->andWhere('p.endDate IS NULL OR p.endDate >= :date') ->setParameter('employee', $employee) ->setParameter('date', $date) ->orderBy('p.startDate', 'DESC') ->setMaxResults(1) ->getQuery() ->getOneOrNullResult() ; } public function closeOpenPeriods(Employee $employee, DateTimeImmutable $endDate): int { return $this->createQueryBuilder('p') ->update() ->set('p.endDate', ':endDate') ->andWhere('p.employee = :employee') ->andWhere('p.endDate IS NULL') ->setParameter('employee', $employee) ->setParameter('endDate', $endDate) ->getQuery() ->execute() ; } }