This commit is contained in:
@@ -4,6 +4,7 @@ namespace App\Repository;
|
||||
|
||||
use App\Entity\RoomAvailability;
|
||||
use App\Entity\RoomCategory;
|
||||
use DateInterval;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
@@ -129,14 +130,16 @@ class RoomAvailabilityRepository extends ServiceEntityRepository
|
||||
/**
|
||||
* For a given RoomCategory, sets its availability for all days on a period with the given amount.
|
||||
*/
|
||||
public function bulkEditRoomAvailabilityForCategoryOnPeriod(RoomCategory $roomCategory, \DateTimeInterface $startDate, \DateTimeInterface $endDate, int $amount): void
|
||||
public function bulkEditRoomAvailabilityForCategoryOnPeriod(RoomCategory $roomCategory, \DateTimeInterface $startDate, \DateTimeInterface $endDate, int $amount, bool $useTransactions = true): void
|
||||
{
|
||||
$strStartDate = $startDate->format('Y-m-d');
|
||||
$strEndDate = $endDate->format('Y-m-d');
|
||||
$roomCatId = $roomCategory->getId();
|
||||
|
||||
$entityManager = $this->getEntityManager();
|
||||
$entityManager->beginTransaction();
|
||||
if ($useTransactions) {
|
||||
$entityManager->beginTransaction();
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
@@ -144,7 +147,7 @@ class RoomAvailabilityRepository extends ServiceEntityRepository
|
||||
$this->createQueryBuilder('ra')
|
||||
->delete()
|
||||
->andWhere('ra.day >= :startDate')
|
||||
->andWhere('ra.day < :endDate')
|
||||
->andWhere('ra.day <= :endDate')
|
||||
->andWhere('ra.roomCategory = :roomCategory')
|
||||
->setParameter('roomCategory', $roomCatId)
|
||||
->setParameter('startDate', $strStartDate)
|
||||
@@ -154,8 +157,10 @@ class RoomAvailabilityRepository extends ServiceEntityRepository
|
||||
|
||||
$entityManager->flush();
|
||||
|
||||
// Include end date.
|
||||
$endDateIncluded = date_add($endDate, new DateInterval("P1D"));
|
||||
//For each day, create item and persist it.
|
||||
$daterange = new \DatePeriod($startDate, new \DateInterval('P1D'), $endDate);
|
||||
$daterange = new \DatePeriod($startDate, new \DateInterval('P1D'), $endDateIncluded);
|
||||
foreach ($daterange as $date) {
|
||||
$item = new RoomAvailability();
|
||||
$item->setRoomCategory($roomCategory);
|
||||
@@ -166,12 +171,15 @@ class RoomAvailabilityRepository extends ServiceEntityRepository
|
||||
}
|
||||
|
||||
$entityManager->flush();
|
||||
$entityManager->commit();
|
||||
|
||||
if ($useTransactions) {
|
||||
$entityManager->commit();
|
||||
}
|
||||
} catch (\Throwable $th) {
|
||||
$entityManager->rollback();
|
||||
if ($useTransactions) {
|
||||
$entityManager->rollback();
|
||||
}
|
||||
throw $th;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user