改进Query类的value方法 添加default参数 改进聚合查询方法

This commit is contained in:
thinkphp
2016-06-04 14:01:02 +08:00
parent 6dd242ab4b
commit 40b8b30dc7

View File

@@ -358,11 +358,12 @@ class Query
* 得到某个字段的值 * 得到某个字段的值
* @access public * @access public
* @param string $field 字段名 * @param string $field 字段名
* @param mixed $default 默认值
* @return mixed * @return mixed
*/ */
public function value($field) public function value($field, $default = null)
{ {
$result = false; $result = null;
if (!empty($this->options['cache'])) { if (!empty($this->options['cache'])) {
// 判断查询缓存 // 判断查询缓存
$cache = $this->options['cache']; $cache = $this->options['cache'];
@@ -383,7 +384,7 @@ class Query
// 清空查询条件 // 清空查询条件
$this->options = []; $this->options = [];
} }
return $result; return !is_null($result) ? $result : $default;
} }
/** /**
@@ -450,7 +451,7 @@ class Query
*/ */
public function count($field = '*') 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 = '*') public function sum($field = '*')
{ {
$result = $this->value('SUM(' . $field . ') AS tp_sum'); return $this->value('SUM(' . $field . ') AS tp_sum', 0);
return is_null($result) ? 0 : $result;
} }
/** /**
@@ -473,8 +473,7 @@ class Query
*/ */
public function min($field = '*') public function min($field = '*')
{ {
$result = $this->value('MIN(' . $field . ') AS tp_min'); return $this->value('MIN(' . $field . ') AS tp_min', 0);
return is_null($result) ? 0 : $result;
} }
/** /**
@@ -485,8 +484,7 @@ class Query
*/ */
public function max($field = '*') public function max($field = '*')
{ {
$result = $this->value('MAX(' . $field . ') AS tp_max'); return $this->value('MAX(' . $field . ') AS tp_max', 0);
return is_null($result) ? 0 : $result;
} }
/** /**
@@ -497,8 +495,7 @@ class Query
*/ */
public function avg($field = '*') public function avg($field = '*')
{ {
$result = $this->value('AVG(' . $field . ') AS tp_avg'); return $this->value('AVG(' . $field . ') AS tp_avg', 0);
return is_null($result) ? 0 : $result;
} }
/** /**