From 45869321ac227c33eee29020bd9b3b7061403a82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 5 Jul 2020 15:15:29 +0200 Subject: [PATCH 1/2] MAPG-193 fix PersistentDataManager::selectMultipleFromDb --- src/PersistentData/PersistentDataManager.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/PersistentData/PersistentDataManager.php b/src/PersistentData/PersistentDataManager.php index 3f2c817..38c58db 100644 --- a/src/PersistentData/PersistentDataManager.php +++ b/src/PersistentData/PersistentDataManager.php @@ -27,8 +27,9 @@ class PersistentDataManager public function selectMultipleFromDb(Select $select, string $type, bool $withRelations = false): Generator { $select = $this->createSelect($select, $type, $withRelations); + $result = $select->execute(); - while ($data = $select->execute()->fetch(IResultSet::FETCH_ASSOC)) { + while ($data = $result->fetch(IResultSet::FETCH_ASSOC)) { $model = new $type(); $this->fillWithData($data, $model); From 27fc687043efcda700ed365dfeadbe147e408f7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 5 Jul 2020 15:17:28 +0200 Subject: [PATCH 2/2] MAPG-193 replace Select usage to repository calls where it is easily possible --- src/Controller/MapAdminController.php | 55 +++++++-------------------- src/Controller/MapsController.php | 2 +- src/Repository/PlaceRepository.php | 13 ++++++- 3 files changed, 26 insertions(+), 44 deletions(-) diff --git a/src/Controller/MapAdminController.php b/src/Controller/MapAdminController.php index 7f64d0f..6a46f28 100644 --- a/src/Controller/MapAdminController.php +++ b/src/Controller/MapAdminController.php @@ -1,10 +1,8 @@ columns(Place::getFields()); - $select->where('map_id', '=', $map->getId()); - - $result = $select->execute(); - - while ($placeData = $result->fetch(IResultSet::FETCH_ASSOC)) { - $place = new Place(); - - $this->pdm->fillWithData($placeData, $place); - + foreach ($this->placeRepository->getAllForMap($map) as $place) { $this->pdm->deleteFromDb($place); } } private function calculateMapBounds(Map $map): Bounds { - //TODO: from repository or relations - $select = new Select(\Container::$dbConnection, 'places'); - $select->columns(['lat', 'lng']); - $select->where('map_id', '=', $map->getId()); - - $result = $select->execute(); - $bounds = new Bounds(); - while ($place = $result->fetch(IResultSet::FETCH_ASSOC)) { - $bounds->extend(new Position($place['lat'], $place['lng'])); + + foreach ($this->placeRepository->getAllForMap($map) as $place) { + $bounds->extend($place->getPosition()); } return $bounds; @@ -218,28 +198,19 @@ class MapAdminController implements ISecured private function &getPlaces(Map $map): array { - //TODO: from repository or relations - $select = new Select(\Container::$dbConnection, 'places'); - $select->columns(['id', 'lat', 'lng', 'pano_id_cached', 'pano_id_cached_timestamp', 'pov_heading', 'pov_pitch', 'pov_zoom']); - $select->where('map_id', '=', $map->getId()); - - $result = $select->execute(); - $places = []; - while ($place = $result->fetch(IResultSet::FETCH_ASSOC)) { - $noPano = $place['pano_id_cached_timestamp'] && $place['pano_id_cached'] === null; + foreach ($this->placeRepository->getAllForMap($map) as $place) { + $noPano = $place->getPanoIdCachedTimestampDate() !== null && $place->getPanoIdCached() === null; - $places[$place['id']] = [ - 'id' => $place['id'], - 'lat' => $place['lat'], - 'lng' => $place['lng'], + $placeId = $place->getId(); + + $places[$placeId] = [ + 'id' => $placeId, + 'lat' => $place->getLat(), + 'lng' => $place->getLng(), 'panoId' => null, - 'pov' => [ - 'heading' => (float) $place['pov_heading'], - 'pitch' => (float) $place['pov_pitch'], - 'zoom' => (float) $place['pov_zoom'] - ], + 'pov' => $place->getPov()->toArray(), 'noPano' => $noPano ]; } diff --git a/src/Controller/MapsController.php b/src/Controller/MapsController.php index 3c90d56..4385e76 100644 --- a/src/Controller/MapsController.php +++ b/src/Controller/MapsController.php @@ -19,7 +19,7 @@ class MapsController public function getMaps(): IContent { - //TODO: from repository + //TODO: from repository - count should be added $select = new Select(\Container::$dbConnection, 'maps'); $select->columns([ ['maps', 'id'], diff --git a/src/Repository/PlaceRepository.php b/src/Repository/PlaceRepository.php index 16f2afe..50f7366 100644 --- a/src/Repository/PlaceRepository.php +++ b/src/Repository/PlaceRepository.php @@ -1,6 +1,8 @@ pdm->selectFromDbById($placeId, Place::class); } + public function getAllForMap(Map $map): Generator + { + $select = new Select(\Container::$dbConnection); + $select->where('map_id', '=', $map->getId()); + + yield from $this->pdm->selectMultipleFromDb($select, Place::class); + } + + //TODO: use Map instead of id public function getRandomForMapWithValidPano(int $mapId, array $exclude = [], array &$placesWithoutPano): Place { $placesWithoutPano = []; @@ -37,7 +48,7 @@ class PlaceRepository private function selectRandomFromDbForMap(int $mapId, array $exclude): ?Place { - $select = new Select(\Container::$dbConnection, 'places'); + $select = new Select(\Container::$dbConnection); $select->where('id', 'NOT IN', $exclude); $select->where('map_id', '=', $mapId);