Skip to content

Commit

Permalink
Fix issue when using count query on location search results
Browse files Browse the repository at this point in the history
Fixes #391
  • Loading branch information
Tam committed Jun 6, 2024
1 parent 07594b0 commit c51add1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.0.7 - 2024-06-06
### Fixed
- Fix issue when using count query on location search results (Fixes #391)

## 4.0.6 - 2024-05-08
### Added
- French translations (via @pascalminator)
Expand Down
17 changes: 11 additions & 6 deletions src/services/MapService.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,19 +373,24 @@ private function _searchLocation (ElementQuery $query, mixed $value, string $tab
];

// Filter the query
$query
->subQuery
->addSelect($search . ' as [[mapsCalculatedDistance]]')
$subQuery = $query->subQuery;
$isCount = count($query->select) == 1 && array_values($query->select)[0] == 'COUNT(*)';

if (!$isCount)
$subQuery
->addSelect($search . ' as [[mapsCalculatedDistance]]');

$subQuery
->andWhere($restrict)
->andWhere([
'not',
['[[' . $table . '.lat]]' => null],
]);

if (Craft::$app->getDb()->driverName === 'pgsql')
$query->subQuery->andWhere($search . ' <= ' . $radius);
if (Craft::$app->getDb()->driverName === 'pgsql' || $isCount)
$subQuery->andWhere($search . ' <= ' . $radius);
else
$query->subQuery->andHaving('[[mapsCalculatedDistance]] <= ' . $radius);
$subQuery->andHaving('[[mapsCalculatedDistance]] <= ' . $radius);

return '[[mapsCalculatedDistance]]';
}
Expand Down

0 comments on commit c51add1

Please sign in to comment.