diff --git a/CHANGELOG.md b/CHANGELOG.md index 957cb6fd1..75e0d939f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 2.0.2 under development -- no changes in this release. +- Enh #1172: Refactor `Query::queryScalar()` to use a cloned `Query` object (@darkspock) ## 2.0.1 February 09, 2026 diff --git a/src/Query/Query.php b/src/Query/Query.php index f499893f5..81dfa7b73 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -833,24 +833,13 @@ protected function queryScalar(string|ExpressionInterface $selectExpression): bo && empty($this->having) && empty($this->union) ) { - $select = $this->select; - $order = $this->orderBy; - $limit = $this->limit; - $offset = $this->offset; + $query = clone $this; + $query->select = [$selectExpression]; + $query->orderBy = []; + $query->limit = null; + $query->offset = null; - $this->select = [$selectExpression]; - $this->orderBy = []; - $this->limit = null; - $this->offset = null; - - $command = $this->createCommand(); - - $this->select = $select; - $this->orderBy = $order; - $this->limit = $limit; - $this->offset = $offset; - - return $command->queryScalar(); + return $query->createCommand()->queryScalar(); } $query = (new self($this->db))->select($selectExpression)->from(['c' => $this]);