Add option for use non-optimized count.

This commit is contained in:
kj 2021-09-11 18:27:15 -04:00
parent 405cf1cb34
commit b80ab19d7e

View File

@ -628,9 +628,12 @@ class ModelMySQL {
* @param boolean $resetQuery
* 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
*/
public static function count($resetQuery = true) {
public static function count($resetQuery = true, $optimized = true) {
if (!$resetQuery)
$backup = [
'select' => static::$querySelect['select'],
@ -639,12 +642,23 @@ class ModelMySQL {
'orderBy' => static::$querySelect['orderBy']
];
if ($optimized) {
static::$querySelect['select'] = ['1'];
static::$querySelect['sql_calc_found_rows'] = true;
static::$querySelect['limit'] = '1';
static::$querySelect['orderBy'] = '';
$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) {
static::$querySelect['select'] = $backup['select'];
@ -653,8 +667,7 @@ class ModelMySQL {
static::$querySelect['orderBy'] = $backup['orderBy'];
}
$result = static::query($sql)->fetch_assoc();
return static::found_row();
return $result;
}
/*