Add option for use non-optimized count.
This commit is contained in:
parent
405cf1cb34
commit
b80ab19d7e
@ -628,23 +628,37 @@ class ModelMySQL {
|
|||||||
* @param boolean $resetQuery
|
* @param boolean $resetQuery
|
||||||
* Indica si el query debe reiniciarse o no (por defecto es true).
|
* Indica si el query debe reiniciarse o no (por defecto es true).
|
||||||
*
|
*
|
||||||
|
* @param boolean $optimized
|
||||||
|
* Indica si se usará el conteo optimizado con sql_calc_found_rows o el normal con count().
|
||||||
|
*
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public static function count($resetQuery = true) {
|
public static function count($resetQuery = true, $optimized = true) {
|
||||||
if (!$resetQuery)
|
if (!$resetQuery)
|
||||||
$backup = [
|
$backup = [
|
||||||
'select' => static::$querySelect['select'],
|
'select' => static::$querySelect['select'],
|
||||||
'sql_calc_found_rows' => static::$querySelect['sql_calc_found_rows'],
|
'sql_calc_found_rows' => static::$querySelect['sql_calc_found_rows'],
|
||||||
'limit' => static::$querySelect['limit'],
|
'limit' => static::$querySelect['limit'],
|
||||||
'orderBy' => static::$querySelect['orderBy']
|
'orderBy' => static::$querySelect['orderBy']
|
||||||
];
|
];
|
||||||
|
|
||||||
static::$querySelect['select'] = ['1'];
|
if ($optimized) {
|
||||||
static::$querySelect['sql_calc_found_rows'] = true;
|
static::$querySelect['select'] = ['1'];
|
||||||
static::$querySelect['limit'] = '1';
|
static::$querySelect['sql_calc_found_rows'] = true;
|
||||||
static::$querySelect['orderBy'] = '';
|
static::$querySelect['limit'] = '1';
|
||||||
|
static::$querySelect['orderBy'] = '';
|
||||||
|
|
||||||
$sql = static::buildQuery($resetQuery);
|
$sql = static::buildQuery($resetQuery);
|
||||||
|
static::query($sql);
|
||||||
|
$result = static::found_row();
|
||||||
|
} else {
|
||||||
|
static::$querySelect['select'] = ['count(1) as quantity'];
|
||||||
|
static::$querySelect['orderBy'] = '';
|
||||||
|
|
||||||
|
$sql = static::buildQuery($resetQuery);
|
||||||
|
$queryResult = static::query($sql)->fetch_assoc();
|
||||||
|
$result = $queryResult['quantity'];
|
||||||
|
}
|
||||||
|
|
||||||
if (!$resetQuery) {
|
if (!$resetQuery) {
|
||||||
static::$querySelect['select'] = $backup['select'];
|
static::$querySelect['select'] = $backup['select'];
|
||||||
@ -653,8 +667,7 @@ class ModelMySQL {
|
|||||||
static::$querySelect['orderBy'] = $backup['orderBy'];
|
static::$querySelect['orderBy'] = $backup['orderBy'];
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = static::query($sql)->fetch_assoc();
|
return $result;
|
||||||
return static::found_row();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Loading…
Reference in New Issue
Block a user