From 3e4495dd6ef602b2754c0e32fc47caed297ed3ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 13 Jun 2024 18:02:48 +0200 Subject: [PATCH] Refactor AccompanyingPeriod::getNextCalendarForPerson to enhance performance --- .../Entity/AccompanyingPeriod.php | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php index 03a587620..569e3449a 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php @@ -869,19 +869,24 @@ class AccompanyingPeriod implements public function getNextCalendarsForPerson(Person $person, $limit = 5): ReadableCollection { $today = new \DateTimeImmutable('today'); - $criteria = Criteria::create() - ->where(Criteria::expr()->gte('startDate', $today)) - // ->andWhere(Criteria::expr()->memberOf('persons', $person)) - ->orderBy(['startDate' => 'DESC']) - ->setMaxResults($limit * 2); - return $this->calendars->matching($criteria) - ->matching( - // due to a bug, filter two times - Criteria::create() - ->where(Criteria::expr()->memberOf('persons', $person)) - ->setMaxResults($limit) - ); + $criteria = Criteria::create(); + $expr = Criteria::expr(); + + $criteria + ->where( + $expr->gte('startDate', $today), + ) + ->orderBy(['startDate' => 'ASC']); + + $criteriaByPerson = Criteria::create(); + $criteriaByPerson + ->where( + $expr->memberOf('persons', $person) + ) + ->setMaxResults($limit); + + return $this->calendars->matching($criteria)->matching($criteriaByPerson); } /**