- Add option for reset query or not at get, getFirts, buildQuery and count
- Add sql_calc_found_rows method. - Add found_rows method. - Improve count method usind sql_calc_found_rows and found_rows.
This commit is contained in:
parent
9785954870
commit
e3b97601e2
@ -36,6 +36,7 @@ class ModelMySQL {
|
||||
'orderBy' => '',
|
||||
'groupBy' => '',
|
||||
'limit' => '',
|
||||
'sql_calc_found_rows' => false
|
||||
];
|
||||
|
||||
/*
|
||||
@ -94,6 +95,7 @@ class ModelMySQL {
|
||||
'orderBy' => '',
|
||||
'groupBy' => '',
|
||||
'limit' => '',
|
||||
'sql_calc_found_rows' => false
|
||||
];
|
||||
}
|
||||
|
||||
@ -101,10 +103,16 @@ class ModelMySQL {
|
||||
* Construye la sentencia SQL a partir static::$querySelect y una vez
|
||||
* construída, llama a resetQuery.
|
||||
*
|
||||
* @param boolean $resetQuery
|
||||
* Indica si el query debe reiniciarse o no (por defecto es true).
|
||||
*
|
||||
* @return string
|
||||
* Contiene la sentencia SQL.
|
||||
*/
|
||||
protected static function buildQuery() {
|
||||
protected static function buildQuery($resetQuery = true) {
|
||||
if (static::$querySelect['sql_calc_found_rows'])
|
||||
$sql = 'SELECT SQL_CALC_FOUND_ROWS '.join(', ', static::$querySelect['select']);
|
||||
else
|
||||
$sql = 'SELECT '.join(', ', static::$querySelect['select']);
|
||||
|
||||
if (static::$querySelect['from'] != '') {
|
||||
@ -145,6 +153,7 @@ class ModelMySQL {
|
||||
$sql .= ' LIMIT '.static::$querySelect['limit'];
|
||||
}
|
||||
|
||||
if ($resetQuery)
|
||||
static::resetQuery();
|
||||
|
||||
return $sql;
|
||||
@ -616,22 +625,44 @@ class ModelMySQL {
|
||||
/*
|
||||
* Retorna la cantidad de filas que hay en un query.
|
||||
*
|
||||
* @param boolean $resetQuery
|
||||
* Indica si el query debe reiniciarse o no (por defecto es true).
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public static function count() {
|
||||
if (static::$querySelect['select'] == ['*'])
|
||||
static::$querySelect['select'] = ['count(*) as quantity'];
|
||||
else
|
||||
static::$querySelect['select'][] = 'count(*) as quantity';
|
||||
$sql = static::buildQuery();
|
||||
public static function count($resetQuery = true) {
|
||||
static::$querySelect['select'] = ['1'];
|
||||
static::$querySelect['sql_calc_found_rows'] = true;
|
||||
static::$querySelect['limit'] = '1';
|
||||
$sql = static::buildQuery($resetQuery);
|
||||
$result = static::query($sql)->fetch_assoc();
|
||||
return static::found_row();
|
||||
}
|
||||
|
||||
/*
|
||||
* Retorna las filas contadas en el último query.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public static function found_row() {
|
||||
$result = static::query('SELECT FOUND_ROWS() AS quantity')->fetch_assoc();
|
||||
return $result['quantity'];
|
||||
}
|
||||
|
||||
/*
|
||||
* Obtiene una instancia según su id.
|
||||
* Habilita el conteo de todos las coincidencias posibles incluso usando limit.
|
||||
*
|
||||
* @param int $id
|
||||
* @return ModelMySQL
|
||||
*/
|
||||
public static function sql_calc_found_rows() {
|
||||
static::$querySelect['sql_calc_found_rows'] = true;
|
||||
return new static();
|
||||
}
|
||||
|
||||
/*
|
||||
* Obtiene una instancia según su primary key (generalmente id).
|
||||
*
|
||||
* @param mixed $id
|
||||
* @return ModelMySQL
|
||||
*/
|
||||
public static function getById($id) {
|
||||
@ -674,10 +705,13 @@ class ModelMySQL {
|
||||
/*
|
||||
* Obtener los resultados de la consulta SQL.
|
||||
*
|
||||
* @param boolean $resetQuery
|
||||
* Indica si el query debe reiniciarse o no (por defecto es true).
|
||||
*
|
||||
* @return ModelMySQL[]
|
||||
*/
|
||||
public static function get() { // Devuelve array vacío si no encuentra nada
|
||||
$sql = static::buildQuery();
|
||||
public static function get($resetQuery = true) { // Devuelve array vacío si no encuentra nada
|
||||
$sql = static::buildQuery($resetQuery);
|
||||
$result = static::query($sql);
|
||||
|
||||
$instances = [];
|
||||
@ -692,12 +726,15 @@ class ModelMySQL {
|
||||
/*
|
||||
* El primer elemento de la consulta SQL.
|
||||
*
|
||||
* @param boolean $resetQuery
|
||||
* Indica si el query debe reiniciarse o no (por defecto es true).
|
||||
*
|
||||
* @return mixed
|
||||
* Puede retornar un objeto ModelMySQL o null.
|
||||
*/
|
||||
public static function getFirst() { // Devuelve null si no encuentra nada
|
||||
public static function getFirst($resetQuery = true) { // Devuelve null si no encuentra nada
|
||||
static::limit(1);
|
||||
$instances = static::get();
|
||||
$instances = static::get($resetQuery);
|
||||
return empty($instances) ? null : $instances[0];
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user