Skip to content

Commit

Permalink
Run exists() on the full element query if distinct, etc., are set
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Aug 5, 2024
1 parent 4241f09 commit d224eb1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Fixed a PHP error that could occur when running Codeception tests. ([#15445](https://github.com/craftcms/cms/issues/15445))
- Fixed a bug where `deleteAsset`, `deleteCategory`, `deleteEntry`, and `deleteTag` GraphQL mutations were returning `null` rather than `true` or `false`. ([#15465](https://github.com/craftcms/cms/issues/15465))
- Fixed a styling issue. ([#15473](https://github.com/craftcms/cms/issues/15473))
- Fixed a bug where `exists()` 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), [#15223](https://github.com/craftcms/cms/pull/15223))

## 4.10.7 - 2024-07-29

Expand Down
33 changes: 27 additions & 6 deletions src/elements/db/ElementQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -1690,13 +1690,34 @@ public function exists($db = null): bool
if ($cachedResult !== null) {
return !empty($cachedResult);
}
try {
return $this->prepareSubquery()
->select('elements.id')
->exists($db);
} catch (QueryAbortedException) {
return false;

if (
!$this->distinct
&& empty($this->groupBy)
&& empty($this->having)
&& empty($this->union)
) {
try {
$subquery = $this->prepareSubquery();

// 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)
) {
return $subquery
->select('elements.id')
->exists($db);
}
} catch (QueryAbortedException) {
return false;
}
}

return parent::exists($db);
}

/**
Expand Down

0 comments on commit d224eb1

Please sign in to comment.