Skip to content

Commit c51add1

Browse files
committed
Fix issue when using count query on location search results
Fixes #391
1 parent 07594b0 commit c51add1

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.0.7 - 2024-06-06
2+
### Fixed
3+
- Fix issue when using count query on location search results (Fixes #391)
4+
15
## 4.0.6 - 2024-05-08
26
### Added
37
- French translations (via @pascalminator)

src/services/MapService.php

+11-6
Original file line numberDiff line numberDiff line change
@@ -373,19 +373,24 @@ private function _searchLocation (ElementQuery $query, mixed $value, string $tab
373373
];
374374

375375
// Filter the query
376-
$query
377-
->subQuery
378-
->addSelect($search . ' as [[mapsCalculatedDistance]]')
376+
$subQuery = $query->subQuery;
377+
$isCount = count($query->select) == 1 && array_values($query->select)[0] == 'COUNT(*)';
378+
379+
if (!$isCount)
380+
$subQuery
381+
->addSelect($search . ' as [[mapsCalculatedDistance]]');
382+
383+
$subQuery
379384
->andWhere($restrict)
380385
->andWhere([
381386
'not',
382387
['[[' . $table . '.lat]]' => null],
383388
]);
384389

385-
if (Craft::$app->getDb()->driverName === 'pgsql')
386-
$query->subQuery->andWhere($search . ' <= ' . $radius);
390+
if (Craft::$app->getDb()->driverName === 'pgsql' || $isCount)
391+
$subQuery->andWhere($search . ' <= ' . $radius);
387392
else
388-
$query->subQuery->andHaving('[[mapsCalculatedDistance]] <= ' . $radius);
393+
$subQuery->andHaving('[[mapsCalculatedDistance]] <= ' . $radius);
389394

390395
return '[[mapsCalculatedDistance]]';
391396
}

0 commit comments

Comments
 (0)