Enable to use limit funtion on count function instead send limit directly.

This commit is contained in:
kj 2021-09-11 20:02:23 -04:00
parent 182635bc9c
commit b8b1a1c8f9

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 int $límite * @param boolean $useLimit
* Establece un límite máximo a contar (útil en caso de querer optimizar en tablas muy extensas). * Permite usar limit para estabecer un máximo inical y final para contar. Requiere que se haya definido antes el límite.
* *
* @return int * @return int
*/ */
public static function count($resetQuery = true, $limit = null) { public static function count($resetQuery = true, $useLimit = false) {
if (!$resetQuery) if (!$resetQuery)
$backup = [ $backup = [
'select' => static::$querySelect['select'], 'select' => static::$querySelect['select'],
@ -642,10 +642,9 @@ class ModelMySQL {
'orderBy' => static::$querySelect['orderBy'] 'orderBy' => static::$querySelect['orderBy']
]; ];
if (is_numeric($limit)) { if ($useLimit && static::$querySelect['limit'] != '') {
static::$querySelect['select'] = ['1']; static::$querySelect['select'] = ['1'];
static::$querySelect['sql_calc_found_rows'] = false; static::$querySelect['sql_calc_found_rows'] = false;
static::$querySelect['limit'] = $limit;
static::$querySelect['orderBy'] = ''; static::$querySelect['orderBy'] = '';
$sql = 'SELECT COUNT(1) AS quantity FROM ('.static::buildQuery($resetQuery).') AS counted'; $sql = 'SELECT COUNT(1) AS quantity FROM ('.static::buildQuery($resetQuery).') AS counted';