From 15a54d0d745e41c5651f070ca935daf43c9f0538 Mon Sep 17 00:00:00 2001 From: idontmessabout Date: Wed, 7 Feb 2018 21:35:26 +0000 Subject: [PATCH] Update MapService.php Replace 'simplemap' table alias with randomly generated alias to prevent JOIN alias issue when using simplemap with element-api plugin. --- src/services/MapService.php | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/services/MapService.php b/src/services/MapService.php index f08e82b..b46ede0 100644 --- a/src/services/MapService.php +++ b/src/services/MapService.php @@ -257,22 +257,23 @@ public function modifyElementsQuery (ElementQueryInterface $query, $value) /** @var ElementQuery $query */ $tableName = MapRecord::$tableName; + $tableAlias = unqid('simplemap'); $on = [ 'and', - '[[elements.id]] = [[simplemap.ownerId]]', - '[[elements_sites.siteId]] = [[simplemap.ownerSiteId]]', + '[[elements.id]] = [['.$tableAlias.'.ownerId]]', + '[[elements_sites.siteId]] = [['.$tableAlias.'.ownerSiteId]]', ]; $query->query->join( 'JOIN', - "{$tableName} simplemap", + "{$tableName} {$tableAlias}", $on ); $query->subQuery->join( 'JOIN', - "{$tableName} simplemap", + "{$tableName} {$tableAlias}", $on ); @@ -282,7 +283,7 @@ public function modifyElementsQuery (ElementQueryInterface $query, $value) } if (array_key_exists('location', $value)) { - $this->_searchLocation($query, $value); + $this->_searchLocation($query, $value, $tableAlias); } else if (array_key_exists('distance', $query->orderBy)) { $this->_replaceOrderBy($query); } @@ -501,7 +502,7 @@ private function _padParts (Map $model) * * @throws Exception */ - private function _searchLocation (ElementQuery $query, $value) + private function _searchLocation (ElementQuery $query, $value, $tableAlias) { $location = $value['location']; $country = array_key_exists('country', $value) @@ -552,7 +553,7 @@ private function _searchLocation (ElementQuery $query, $value) ACOS( COS(RADIANS($location[lat])) * COS(RADIANS([[simplemap.lat]])) - * COS(RADIANS($location[lng]) - RADIANS([[simplemap.lng]])) + * COS(RADIANS($location[lng]) - RADIANS([[$tableAlias.lng]])) + SIN(RADIANS($location[lat])) * SIN(RADIANS([[simplemap.lat]])) ) @@ -565,13 +566,13 @@ private function _searchLocation (ElementQuery $query, $value) 'and', [ 'and', - "[[simplemap.lat]] >= $location[lat] - ($radius / $distanceUnit)", - "[[simplemap.lat]] <= $location[lat] + ($radius / $distanceUnit)", + "[[$tableAlias.lat]] >= $location[lat] - ($radius / $distanceUnit)", + "[[$tableAlias.lat]] <= $location[lat] + ($radius / $distanceUnit)", ], [ 'and', - "[[simplemap.lng]] >= $location[lng] - ($radius / ($distanceUnit * COS(RADIANS($location[lat]))))", - "[[simplemap.lng]] <= $location[lng] + ($radius / ($distanceUnit * COS(RADIANS($location[lat]))))", + "[[$tableAlias.lng]] >= $location[lng] - ($radius / ($distanceUnit * COS(RADIANS($location[lat]))))", + "[[$tableAlias.lng]] <= $location[lng] + ($radius / ($distanceUnit * COS(RADIANS($location[lat]))))", ] ]; @@ -603,4 +604,4 @@ private function _replaceOrderBy (ElementQuery $query, $distanceSearch = false) $query->orderBy($nextOrder); } -} \ No newline at end of file +}