- Remove option for use non-optimized count.

- Add option for limit count.
This commit is contained in:
kj 2021-09-11 19:04:54 -04:00
parent b80ab19d7e
commit 182635bc9c

View File

@ -628,12 +628,12 @@ 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 * @param int $límite
* Indica si se usará el conteo optimizado con sql_calc_found_rows o el normal con count(). * Establece un límite máximo a contar (útil en caso de querer optimizar en tablas muy extensas).
* *
* @return int * @return int
*/ */
public static function count($resetQuery = true, $optimized = true) { public static function count($resetQuery = true, $limit = null) {
if (!$resetQuery) if (!$resetQuery)
$backup = [ $backup = [
'select' => static::$querySelect['select'], 'select' => static::$querySelect['select'],
@ -642,7 +642,16 @@ class ModelMySQL {
'orderBy' => static::$querySelect['orderBy'] 'orderBy' => static::$querySelect['orderBy']
]; ];
if ($optimized) { if (is_numeric($limit)) {
static::$querySelect['select'] = ['1'];
static::$querySelect['sql_calc_found_rows'] = false;
static::$querySelect['limit'] = $limit;
static::$querySelect['orderBy'] = '';
$sql = 'SELECT COUNT(1) AS quantity FROM ('.static::buildQuery($resetQuery).') AS counted';
$queryResult = static::query($sql)->fetch_assoc();
$result = $queryResult['quantity'];
} else {
static::$querySelect['select'] = ['1']; static::$querySelect['select'] = ['1'];
static::$querySelect['sql_calc_found_rows'] = true; static::$querySelect['sql_calc_found_rows'] = true;
static::$querySelect['limit'] = '1'; static::$querySelect['limit'] = '1';
@ -651,13 +660,6 @@ class ModelMySQL {
$sql = static::buildQuery($resetQuery); $sql = static::buildQuery($resetQuery);
static::query($sql); static::query($sql);
$result = static::found_row(); $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) {