From 40b8b30dc72b43763abba2ea04fc6cafa0d28d57 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 4 Jun 2016 14:01:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BQuery=E7=B1=BB=E7=9A=84value?= =?UTF-8?q?=E6=96=B9=E6=B3=95=20=E6=B7=BB=E5=8A=A0default=E5=8F=82?= =?UTF-8?q?=E6=95=B0=20=E6=94=B9=E8=BF=9B=E8=81=9A=E5=90=88=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 31bec497..3d757d99 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -358,11 +358,12 @@ class Query * 得到某个字段的值 * @access public * @param string $field 字段名 + * @param mixed $default 默认值 * @return mixed */ - public function value($field) + public function value($field, $default = null) { - $result = false; + $result = null; if (!empty($this->options['cache'])) { // 判断查询缓存 $cache = $this->options['cache']; @@ -383,7 +384,7 @@ class Query // 清空查询条件 $this->options = []; } - return $result; + return !is_null($result) ? $result : $default; } /** @@ -450,7 +451,7 @@ class Query */ public function count($field = '*') { - return $this->value('COUNT(' . $field . ') AS tp_count'); + return $this->value('COUNT(' . $field . ') AS tp_count', 0); } /** @@ -461,8 +462,7 @@ class Query */ public function sum($field = '*') { - $result = $this->value('SUM(' . $field . ') AS tp_sum'); - return is_null($result) ? 0 : $result; + return $this->value('SUM(' . $field . ') AS tp_sum', 0); } /** @@ -473,8 +473,7 @@ class Query */ public function min($field = '*') { - $result = $this->value('MIN(' . $field . ') AS tp_min'); - return is_null($result) ? 0 : $result; + return $this->value('MIN(' . $field . ') AS tp_min', 0); } /** @@ -485,8 +484,7 @@ class Query */ public function max($field = '*') { - $result = $this->value('MAX(' . $field . ') AS tp_max'); - return is_null($result) ? 0 : $result; + return $this->value('MAX(' . $field . ') AS tp_max', 0); } /** @@ -497,8 +495,7 @@ class Query */ public function avg($field = '*') { - $result = $this->value('AVG(' . $field . ') AS tp_avg'); - return is_null($result) ? 0 : $result; + return $this->value('AVG(' . $field . ') AS tp_avg', 0); } /**