From 7cb406cc499348a79627a3fa3fbaf4b73c6d24f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 6 Aug 2023 22:06:42 +0200 Subject: [PATCH] fix selectUpcomingAndRecent when used with other selects --- src/Repository/EventRepository.php | 47 +++++++++++++++++------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/src/Repository/EventRepository.php b/src/Repository/EventRepository.php index 7e31398..ad49c8d 100644 --- a/src/Repository/EventRepository.php +++ b/src/Repository/EventRepository.php @@ -23,19 +23,24 @@ class EventRepository public function getAllByCommunity(Community $community, bool $useRelations = false, array $withRelations = []): Generator { - $select = $this->selectAllByCommunity($community); + $select = new Select(Container::$dbConnection, Event::getTable()); + $this->selectAllByCommunity($select, $community); yield from Container::$persistentDataManager->selectMultipleFromDb($select, Event::class, $useRelations, $withRelations); } public function countAllByCommunity(Community $community): int { - return $this->selectAllByCommunity($community)->count(); + $select = new Select(Container::$dbConnection, Event::getTable()); + $this->selectAllByCommunity($select, $community); + + return $select->count(); } public function getUpcomingAndRecentByCommunity(Community $community, DateTime $from, int $days, int $limit, bool $useRelations = false, array $withRelations = []): Generator { - $select = $this->selectAllByCommunity($community); + $select = new Select(Container::$dbConnection, Event::getTable()); + $this->selectAllByCommunity($select, $community); $this->selectUpcomingAndRecent($select, $from, $days); $select->orderBy('start', 'DESC'); $select->limit($limit, 0); @@ -45,7 +50,8 @@ class EventRepository public function getUpcomingAndRecentByUser(User $user, DateTime $from, int $days, int $limit, bool $useRelations = false, array $withRelations = []): Generator { - $select = $this->selectAllByUser($user); + $select = new Select(Container::$dbConnection, Event::getTable()); + $this->selectAllByUser($select, $user); $this->selectUpcomingAndRecent($select, $from, $days); $select->orderBy('start', 'DESC'); $select->limit($limit, 0); @@ -55,7 +61,8 @@ class EventRepository public function getCurrentByUser(User $user, DateTime $from, int $days, bool $useRelations = false, array $withRelations = []): ?Event { - $select = $this->selectAllByUser($user); + $select = new Select(Container::$dbConnection, Event::getTable()); + $this->selectAllByUser($select, $user); $this->selectUpcomingAndRecent($select, $from, $days); $select->orderBy('start', 'DESC'); $select->limit(1, 0); @@ -65,7 +72,8 @@ class EventRepository public function getPagedByCommunity(Community $community, int $page, int $itemsPerPage, bool $useRelations = false, array $withRelations = []): Generator { - $select = $this->selectAllByCommunity($community); + $select = new Select(Container::$dbConnection, Event::getTable()); + $this->selectAllByCommunity($select, $community); $select->orderBy('start', 'DESC'); $select->paginate($page, $itemsPerPage); @@ -74,7 +82,8 @@ class EventRepository public function searchByTitle(Community $community, string $title): Generator { - $select = $this->selectAllByCommunity($community); + $select = new Select(Container::$dbConnection, Event::getTable()); + $this->selectAllByCommunity($select, $community); $select->where('title', 'LIKE', '%' . $title . '%'); $select->orderBy('start', 'DESC'); $select->limit(10); @@ -82,31 +91,29 @@ class EventRepository yield from Container::$persistentDataManager->selectMultipleFromDb($select, Event::class); } - private function selectAllByCommunity(Community $community) + private function selectAllByCommunity(Select $select, Community $community): void { - $select = new Select(Container::$dbConnection, Event::getTable()); $select->where('community_id', '=', $community->getId()); - return $select; } - private function selectAllByUser(User $user) + private function selectAllByUser(Select $select, User $user): void { - $select = new Select(Container::$dbConnection, Event::getTable()); $select->innerJoin('communities', ['communities', 'id'], '=', ['events', 'community_id']); $select->innerJoin('community_members', ['communities', 'id'], '=', ['community_members', 'community_id']); $select->where(['community_members', 'user_id'], '=', $user->getId()); - return $select; } - private function selectUpcomingAndRecent(Select $select, DateTime $from, int $days) + private function selectUpcomingAndRecent(Select $select, DateTime $from, int $days): void { $select->where(function (Select $select) use ($from, $days) { - $select->where('start', '<', (clone $from)->add(DateInterval::createFromDateString("$days days"))->format('Y-m-d H:i:s')); - $select->where('end', '>', $from->format('Y-m-d H:i:s')); - }); - $select->orWhere(function (Select $select) use ($from, $days) { - $select->where('end', '>', (clone $from)->sub(DateInterval::createFromDateString("$days days"))->format('Y-m-d H:i:s')); - $select->where('start', '<', $from->format('Y-m-d H:i:s')); + $select->where(function (Select $select) use ($from, $days) { + $select->where('start', '<', (clone $from)->add(DateInterval::createFromDateString("$days days"))->format('Y-m-d H:i:s')); + $select->where('end', '>', $from->format('Y-m-d H:i:s')); + }); + $select->orWhere(function (Select $select) use ($from, $days) { + $select->where('end', '>', (clone $from)->sub(DateInterval::createFromDateString("$days days"))->format('Y-m-d H:i:s')); + $select->where('start', '<', $from->format('Y-m-d H:i:s')); + }); }); } } -- 2.45.2