From 08f664e136f1f9025a8eee5b2c3b74862d4d9eb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Thu, 20 Apr 2023 22:41:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ActiveQuery.php | 3 +-- SqlBuilder.php | 16 +++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/ActiveQuery.php b/ActiveQuery.php index 2533fbd..247505f 100644 --- a/ActiveQuery.php +++ b/ActiveQuery.php @@ -262,8 +262,7 @@ class ActiveQuery extends Component implements ISqlBuilder */ public function count(): int { - $builder = clone $this->builder; - $data = $this->execute($builder->count())->one(); + $data = $this->execute($this->builder->count())->one(); if ($data && is_array($data)) { return (int)array_shift($data); } diff --git a/SqlBuilder.php b/SqlBuilder.php index 6e1219d..4881470 100644 --- a/SqlBuilder.php +++ b/SqlBuilder.php @@ -205,6 +205,9 @@ class SqlBuilder extends Component */ public function one(): string { + if (count($this->query->select) < 1) { + $this->query->select = ['*']; + } return $this->_selectPrefix() . $this->_prefix() . $this->builderLimit($this->query); } @@ -215,6 +218,9 @@ class SqlBuilder extends Component */ public function all(): string { + if (count($this->query->select) < 1) { + $this->query->select = ['*']; + } return $this->_selectPrefix() . $this->_prefix() . $this->builderLimit($this->query); } @@ -225,8 +231,7 @@ class SqlBuilder extends Component */ public function count(): string { - $this->query->select('COUNT(*)'); - return $this->_selectPrefix() . $this->_prefix() . $this->builderLimit($this->query); + return $this->_selectPrefix(['COUNT(*)']) . $this->_prefix() . $this->builderLimit($this->query); } @@ -263,12 +268,9 @@ class SqlBuilder extends Component * @return string * @throws Exception */ - private function _selectPrefix(): string + private function _selectPrefix(array $select = ['*']): string { - if (count($this->query->select) < 1) { - $this->query->select = ['*']; - } - $select = "SELECT " . implode(',', $this->query->select) . " FROM " . $this->query->from; + $select = "SELECT " . implode(',', $select) . " FROM " . $this->query->from; if ($this->query->alias != "") { $select .= " AS " . $this->query->alias; }