Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

queryScalar - check subquery before changing params #15223

Merged
merged 3 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Added `Garnish.muteResizeEvents()`.
- Fixed a JavaScript performance degradation bug. ([#14510](https://github.com/craftcms/cms/issues/14510))
- Fixed a bug where scalar element queries weren’t working if `distinct`, `groupBy`, `having,` or `union` params were set on them during query preparation. ([#15001](https://github.com/craftcms/cms/issues/15001))

## 4.10.1 - 2024-06-17

Expand Down
22 changes: 15 additions & 7 deletions src/elements/db/ElementQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -1878,11 +1878,21 @@ protected function queryScalar($selectExpression, $db): bool|string|null

try {
$subquery = $this->prepareSubquery();
$subquery->select = [$selectExpression];
$subquery->orderBy = null;
$subquery->limit = null;
$subquery->offset = null;
$command = $subquery->createCommand($db);

// If distinct, et al. were set by prepare(), don't mess with it
// see https://github.com/craftcms/cms/issues/15001#issuecomment-2174563927
if (
!$subquery->distinct
&& empty($subquery->groupBy)
&& empty($subquery->having)
&& empty($subquery->union)
) {
$subquery->select = [$selectExpression];
$subquery->orderBy = null;
$subquery->limit = null;
$subquery->offset = null;
return $subquery->createCommand($db)->queryScalar();
}
} catch (QueryAbortedException) {
return false;
} finally {
Expand All @@ -1891,8 +1901,6 @@ protected function queryScalar($selectExpression, $db): bool|string|null
$this->limit = $limit;
$this->offset = $offset;
}

return $command->queryScalar();
}

return parent::queryScalar($selectExpression, $db);
Expand Down
Loading