mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 15:12:48 +08:00
改进count查询对group的支持
This commit is contained in:
@@ -509,10 +509,17 @@ class Query
|
|||||||
* COUNT查询
|
* COUNT查询
|
||||||
* @access public
|
* @access public
|
||||||
* @param string $field 字段名
|
* @param string $field 字段名
|
||||||
* @return integer
|
* @return integer|string
|
||||||
*/
|
*/
|
||||||
public function count($field = '*')
|
public function count($field = '*')
|
||||||
{
|
{
|
||||||
|
if (isset($this->options['group'])) {
|
||||||
|
// 支持GROUP
|
||||||
|
$options = $this->getOptions();
|
||||||
|
$subSql = $this->options($options)->field('count(' . $field . ')')->bind($this->bind)->buildSql();
|
||||||
|
return $this->table([$subSql => '_group_count_'])->value('COUNT(*) AS tp_count', 0, true);
|
||||||
|
}
|
||||||
|
|
||||||
return $this->value('COUNT(' . $field . ') AS tp_count', 0, true);
|
return $this->value('COUNT(' . $field . ') AS tp_count', 0, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1710,7 +1717,7 @@ class Query
|
|||||||
}
|
}
|
||||||
|
|
||||||
list($guid) = explode(' ', $tableName);
|
list($guid) = explode(' ', $tableName);
|
||||||
$db = $this->getConfig('database');
|
$db = $this->getConfig('database');
|
||||||
if (!isset(self::$info[$db . '.' . $guid])) {
|
if (!isset(self::$info[$db . '.' . $guid])) {
|
||||||
if (!strpos($guid, '.')) {
|
if (!strpos($guid, '.')) {
|
||||||
$schema = $db . '.' . $guid;
|
$schema = $db . '.' . $guid;
|
||||||
@@ -1724,7 +1731,7 @@ class Query
|
|||||||
$info = $this->connection->getFields($guid);
|
$info = $this->connection->getFields($guid);
|
||||||
}
|
}
|
||||||
$fields = array_keys($info);
|
$fields = array_keys($info);
|
||||||
$bind = $type = [];
|
$bind = $type = [];
|
||||||
foreach ($info as $key => $val) {
|
foreach ($info as $key => $val) {
|
||||||
// 记录字段类型
|
// 记录字段类型
|
||||||
$type[$key] = $val['type'];
|
$type[$key] = $val['type'];
|
||||||
@@ -1889,7 +1896,7 @@ class Query
|
|||||||
$relation = $key;
|
$relation = $key;
|
||||||
$with[$key] = $key;
|
$with[$key] = $key;
|
||||||
} elseif (is_string($relation) && strpos($relation, '.')) {
|
} elseif (is_string($relation) && strpos($relation, '.')) {
|
||||||
$with[$key] = $relation;
|
$with[$key] = $relation;
|
||||||
list($relation, $subRelation) = explode('.', $relation, 2);
|
list($relation, $subRelation) = explode('.', $relation, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2227,7 +2234,7 @@ class Query
|
|||||||
if ($data instanceof Query) {
|
if ($data instanceof Query) {
|
||||||
return $data->select();
|
return $data->select();
|
||||||
} elseif ($data instanceof \Closure) {
|
} elseif ($data instanceof \Closure) {
|
||||||
call_user_func_array($data, [& $this]);
|
call_user_func_array($data, [ & $this]);
|
||||||
$data = null;
|
$data = null;
|
||||||
}
|
}
|
||||||
// 分析查询表达式
|
// 分析查询表达式
|
||||||
@@ -2327,7 +2334,7 @@ class Query
|
|||||||
if ($data instanceof Query) {
|
if ($data instanceof Query) {
|
||||||
return $data->find();
|
return $data->find();
|
||||||
} elseif ($data instanceof \Closure) {
|
} elseif ($data instanceof \Closure) {
|
||||||
call_user_func_array($data, [& $this]);
|
call_user_func_array($data, [ & $this]);
|
||||||
$data = null;
|
$data = null;
|
||||||
}
|
}
|
||||||
// 分析查询表达式
|
// 分析查询表达式
|
||||||
@@ -2659,10 +2666,10 @@ class Query
|
|||||||
if (isset($options['page'])) {
|
if (isset($options['page'])) {
|
||||||
// 根据页数计算limit
|
// 根据页数计算limit
|
||||||
list($page, $listRows) = $options['page'];
|
list($page, $listRows) = $options['page'];
|
||||||
$page = $page > 0 ? $page : 1;
|
$page = $page > 0 ? $page : 1;
|
||||||
$listRows = $listRows > 0 ? $listRows : (is_numeric($options['limit']) ? $options['limit'] : 20);
|
$listRows = $listRows > 0 ? $listRows : (is_numeric($options['limit']) ? $options['limit'] : 20);
|
||||||
$offset = $listRows * ($page - 1);
|
$offset = $listRows * ($page - 1);
|
||||||
$options['limit'] = $offset . ',' . $listRows;
|
$options['limit'] = $offset . ',' . $listRows;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->options = [];
|
$this->options = [];
|
||||||
|
|||||||
Reference in New Issue
Block a user