From fe98d94692309b5768204b0a507e2676af02ef84 Mon Sep 17 00:00:00 2001 From: kj Date: Wed, 22 Jul 2020 10:52:40 -0400 Subject: [PATCH] Fix count when there is a "group by" in the query (do not delete actual select). --- src/Libs/ModelMySQL.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Libs/ModelMySQL.php b/src/Libs/ModelMySQL.php index 3b50f3e..e2bf0a2 100644 --- a/src/Libs/ModelMySQL.php +++ b/src/Libs/ModelMySQL.php @@ -25,7 +25,7 @@ class ModelMySQL { static protected $tableSufix = 's'; static protected $db; static protected $querySelect = [ - 'select' => '*', + 'select' => ['*'], 'where' => '', 'from' => '', 'leftJoin' => '', @@ -83,7 +83,7 @@ class ModelMySQL { */ protected static function resetQuery() { static::$querySelect = [ - 'select' => '*', + 'select' => ['*'], 'where' => '', 'from' => '', 'leftJoin' => '', @@ -104,7 +104,7 @@ class ModelMySQL { * Contiene la sentencia SQL. */ private static function buildQuery() { - $sql = 'SELECT '.static::$querySelect['select']; + $sql = 'SELECT '.join(', ', static::$querySelect['select']); if (static::$querySelect['from'] != '') { $sql .= ' FROM '.static::$querySelect['from']; @@ -302,7 +302,7 @@ class ModelMySQL { $select[] = $db->real_escape_string($column); } - static::$querySelect['select'] = join(', ', $select); + static::$querySelect['select'] = $select; return new static(); } @@ -612,7 +612,10 @@ class ModelMySQL { * @return int */ public static function count() { - static::$querySelect['select'] = 'count(*) as quantity'; + if (static::$querySelect['select'] == ['*']) + static::$querySelect['select'] = ['count(*) as quantity']; + else + static::$querySelect['select'][] = 'count(*) as quantity'; $sql = static::buildQuery(); $result = static::query($sql)->fetch_assoc(); return $result['quantity'];