Change self to static in ModelMysql.

This commit is contained in:
kj 2020-03-24 19:37:39 -04:00
parent 682452efc1
commit c929a3a454

View File

@ -43,10 +43,10 @@ class ModelMySQL {
* @return mysqli
*/
private static function db(){
if (is_null(self::$db))
self::$db = Database::getConnection();
if (is_null(static::$db))
static::$db = Database::getConnection();
return self::$db;
return static::$db;
}
/*
@ -62,7 +62,7 @@ class ModelMySQL {
* Contiene el resultado de la llamada SQL.
*/
private static function query($query){
$db = self::db();
$db = static::db();
$result = $db->query($query);
if ($db->errno){
@ -82,7 +82,7 @@ class ModelMySQL {
* Reinicia la configuración de la sentencia SQL.
*/
private static function resetQuery(){
self::$querySelect = [
static::$querySelect = [
'select' => '*',
'where' => '',
'leftJoin' => '',
@ -96,44 +96,44 @@ class ModelMySQL {
}
/*
* Construye la sentencia SQL a partir self::$querySelect y una vez
* Construye la sentencia SQL a partir static::$querySelect y una vez
* construída, llama a resetQuery.
*
* @return string
* Contiene la sentencia SQL.
*/
private static function buildQuery(){
$sql = 'SELECT '.self::$querySelect['select'].' FROM '.self::table();
$sql = 'SELECT '.static::$querySelect['select'].' FROM '.static::table();
if (self::$querySelect['leftJoin'] != ''){
$sql .= ' LEFT JOIN ' . self::$querySelect['leftJoin'];
$sql .= ' ON '. self::$querySelect['on'];
} elseif(self::$querySelect['rightJoin'] != ''){
$sql .= ' RIGHT JOIN ' . self::$querySelect['leftJoin'];
$sql .= ' ON '. self::$querySelect['on'];
if (static::$querySelect['leftJoin'] != ''){
$sql .= ' LEFT JOIN ' . static::$querySelect['leftJoin'];
$sql .= ' ON '. static::$querySelect['on'];
} elseif(static::$querySelect['rightJoin'] != ''){
$sql .= ' RIGHT JOIN ' . static::$querySelect['leftJoin'];
$sql .= ' ON '. static::$querySelect['on'];
}
if (self::$querySelect['where'] != ''){
$sql .= ' WHERE '.self::$querySelect['where'];
if (static::$querySelect['where'] != ''){
$sql .= ' WHERE '.static::$querySelect['where'];
if (self::$querySelect['AndOr'] != ''){
$sql .= self::$querySelect['AndOr'];
if (static::$querySelect['AndOr'] != ''){
$sql .= static::$querySelect['AndOr'];
}
}
if (self::$querySelect['groupBy'] != ''){
$sql .= ' GROUP BY '.self::$querySelect['groupBy'];
if (static::$querySelect['groupBy'] != ''){
$sql .= ' GROUP BY '.static::$querySelect['groupBy'];
}
if (self::$querySelect['orderBy'] != ''){
$sql .= ' ORDER BY '.self::$querySelect['orderBy'];
if (static::$querySelect['orderBy'] != ''){
$sql .= ' ORDER BY '.static::$querySelect['orderBy'];
}
if (self::$querySelect['limit'] != ''){
$sql .= ' LIMIT '.self::$querySelect['limit'];
if (static::$querySelect['limit'] != ''){
$sql .= ' LIMIT '.static::$querySelect['limit'];
}
self::resetQuery();
static::resetQuery();
return $sql;
}
@ -186,16 +186,16 @@ class ModelMySQL {
}
/*
* Construye (a partir del nombre de la clase y el sufijo en self::$tableSufix)
* Construye (a partir del nombre de la clase y el sufijo en static::$tableSufix)
* y/o develve el nombre de la tabla de la BD en la que se alojará o
* se aloja el objeto actual.
*
* @return string
*/
private static function table(){
if (isset(self::$table))
return self::$table;
return self::className().self::$tableSufix;
if (isset(static::$table))
return static::$table;
return static::className().static::$tableSufix;
}
/*
@ -205,15 +205,15 @@ class ModelMySQL {
$atts = $this->getVars();
foreach ($atts as $key => $value){
$value = self::db()->real_escape_string($value);
$value = static::db()->real_escape_string($value);
$set[]="$key='$value'";
}
$table = self::table();
$table = static::table();
$pk = $this->primaryKey;
$pkv = $this->$pk;
$sql = "UPDATE $table SET ".join(', ', $set)." WHERE $pk='$pkv'";
self::query($sql);
static::query($sql);
}
/*
@ -221,7 +221,7 @@ class ModelMySQL {
* objeto actual.
*/
private function add(){
$db = self::db();
$db = static::db();
$atts = $this->getVars();
foreach ($atts as $key => $value){
@ -229,9 +229,9 @@ class ModelMySQL {
$values[] = "'".$db->real_escape_string($value)."'";
}
$table = self::table();
$table = static::table();
$sql = "INSERT INTO $table (".join(', ', $into).") VALUES (".join(', ', $values).")";
self::query($sql);
static::query($sql);
$pk = $this->primaryKey;
$this->$pk = $db->insert_id;
@ -256,15 +256,15 @@ class ModelMySQL {
$atts = $this->getVars();
foreach ($atts as $key => $value){
$value = self::db()->real_escape_string($value);
$value = static::db()->real_escape_string($value);
$set[]="$key='$value'";
}
$table = self::table();
$table = static::table();
$pk = $this->primaryKey;
$pkv = $this->$pk;
$sql = "DELETE FROM $table WHERE $pk='$pkv'";
self::query($sql);
static::query($sql);
}
/*
@ -274,13 +274,13 @@ class ModelMySQL {
* Columnas que se selecionarán en la consulta SQL.
*/
public static function select($columns){
$db = self::db();
$db = static::db();
$select = [];
foreach($columns as $column){
$select[] = $db->real_escape_string($column);
}
self::$querySelect['select'] = join(', ', $select);
static::$querySelect['select'] = join(', ', $select);
return new static();
}
@ -298,8 +298,8 @@ class ModelMySQL {
* El valor el valor a comparar en la columna.
*
* Sintaxis posibles:
* - self::where(columna, operador, valor)
* - self::where(columna, valor) // Operador por defecto "="
* - static::where(columna, operador, valor)
* - static::where(columna, valor) // Operador por defecto "="
*/
public static function where($column, $operator, $value=null){
if (is_null($value)){
@ -307,9 +307,9 @@ class ModelMySQL {
$operator = '=';
}
$value = self::db()->real_escape_string($value);
$value = static::db()->real_escape_string($value);
self::$querySelect['where'] = "$column$operator'$value'";
static::$querySelect['where'] = "$column$operator'$value'";
return new static();
}
@ -328,13 +328,13 @@ class ModelMySQL {
*/
public static function where_in($column,$arr, $in = true){
foreach($arr as $index => $value){
$arr[$index] = self::db()->real_escape_string($value);
$arr[$index] = static::db()->real_escape_string($value);
}
if ($in)
self::$querySelect['where'] = "$column IN (".join(', ',$arr).")";
static::$querySelect['where'] = "$column IN (".join(', ',$arr).")";
else
self::$querySelect['where'] = "$column NOT IN (".join(', ',$arr).")";
static::$querySelect['where'] = "$column NOT IN (".join(', ',$arr).")";
return new static();
}
@ -355,8 +355,8 @@ class ModelMySQL {
* Columna a comparar para hacer el join.
*
* Sintaxis posibles:
* - self::leftJoin(tabla,columnaA, operador, columnB)
* - self::leftJoin(tabla,columnaA, columnB) // Operador por defecto "="
* - static::leftJoin(tabla,columnaA, operador, columnB)
* - static::leftJoin(tabla,columnaA, columnB) // Operador por defecto "="
*/
public static function leftJoin($table, $columnA, $operator, $columnB = null){
if (is_null($columnB)){
@ -364,11 +364,11 @@ class ModelMySQL {
$operator = '=';
}
$columnA = self::db()->real_escape_string($columnA);
$columnB = self::db()->real_escape_string($columnB);
$columnA = static::db()->real_escape_string($columnA);
$columnB = static::db()->real_escape_string($columnB);
self::$querySelect['leftJoin'] = $table;
self::$querySelect['on'] = "$columnA$operator$columnB";
static::$querySelect['leftJoin'] = $table;
static::$querySelect['on'] = "$columnA$operator$columnB";
return new static();
@ -390,8 +390,8 @@ class ModelMySQL {
* Columna a comparar para hacer el join.
*
* Sintaxis posibles:
* - self::rightJoin(tabla,columnaA, operador, columnB)
* - self::rightJoin(tabla,columnaA, columnB) // Operador por defecto "="
* - static::rightJoin(tabla,columnaA, operador, columnB)
* - static::rightJoin(tabla,columnaA, columnB) // Operador por defecto "="
*/
public static function rightJoin($table, $columnA, $operator, $columnB = null){
if (is_null($columnB)){
@ -399,11 +399,11 @@ class ModelMySQL {
$operator = '=';
}
$columnA = self::db()->real_escape_string($columnA);
$columnB = self::db()->real_escape_string($columnB);
$columnA = static::db()->real_escape_string($columnA);
$columnB = static::db()->real_escape_string($columnB);
self::$querySelect['rightJoin'] = $table;
self::$querySelect['on'] = "$columnA$operator$columnB";
static::$querySelect['rightJoin'] = $table;
static::$querySelect['on'] = "$columnA$operator$columnB";
return new static();
@ -422,9 +422,9 @@ class ModelMySQL {
* El valor el valor a comparar en la columna.
*
* Sintaxis posibles:
* - self::and(columna, operador, valor)
* - self::and(columna, valor) // Operador por defecto "="
* - self::and(columna, valor)->and(columna, valor)->and(columna, valor) // anidado
* - static::and(columna, operador, valor)
* - static::and(columna, valor) // Operador por defecto "="
* - static::and(columna, valor)->and(columna, valor)->and(columna, valor) // anidado
*/
public static function and($column, $operator, $value=null){
if (is_null($value)){
@ -432,9 +432,9 @@ class ModelMySQL {
$operator = '=';
}
$value = self::db()->real_escape_string($value);
$value = static::db()->real_escape_string($value);
self::$querySelect['AndOr'] .= " AND $column$operator'$value'";
static::$querySelect['AndOr'] .= " AND $column$operator'$value'";
return new static();
}
@ -452,9 +452,9 @@ class ModelMySQL {
* El valor el valor a comparar en la columna.
*
* Sintaxis posibles:
* - self::or(columna, operador, valor)
* - self::or(columna, valor) // Operador por defecto "="
* - self::or(columna, valor)->or(columna, valor)->or(columna, valor) // anidado
* - static::or(columna, operador, valor)
* - static::or(columna, valor) // Operador por defecto "="
* - static::or(columna, valor)->or(columna, valor)->or(columna, valor) // anidado
*/
public static function or($column, $operator, $value=null){
if (is_null($value)){
@ -462,9 +462,9 @@ class ModelMySQL {
$operator = '=';
}
$value = self::db()->real_escape_string($value);
$value = static::db()->real_escape_string($value);
self::$querySelect['AndOr'] .= " OR $column$operator'$value'";
static::$querySelect['AndOr'] .= " OR $column$operator'$value'";
return new static();
}
@ -476,7 +476,7 @@ class ModelMySQL {
* Columnas por las que se agrupará.
*/
public static function groupBy($arr){
self::$querySelect['groupBy'] = join(', ', $arr);
static::$querySelect['groupBy'] = join(', ', $arr);
return new static();
}
@ -485,9 +485,9 @@ class ModelMySQL {
$final = (int)$final;
if ($final==0)
self::$querySelect['limit'] = $initial;
static::$querySelect['limit'] = $initial;
else
self::$querySelect['limit'] = $initial.', '.$final;
static::$querySelect['limit'] = $initial.', '.$final;
return new static();
}
@ -504,16 +504,16 @@ class ModelMySQL {
*/
public static function orderBy($value, $order = 'ASC'){
if ($value == "RAND"){
self::$querySelect['orderBy'] = 'RAND()';
static::$querySelect['orderBy'] = 'RAND()';
return new static();
}
$value = self::db()->real_escape_string($value);
$value = static::db()->real_escape_string($value);
if (!(strtoupper($order) == 'ASC' || strtoupper($order) == 'DESC'))
$order = 'ASC';
self::$querySelect['orderBy'] = $value.' '.$order;
static::$querySelect['orderBy'] = $value.' '.$order;
return new static();
}
@ -524,9 +524,9 @@ class ModelMySQL {
* @return int
*/
public static function count(){
self::$querySelect['select'] = 'count(*) as quantity';
$sql = self::buildQuery();
$result = self::query($sql)->fetch_assoc();
static::$querySelect['select'] = 'count(*) as quantity';
$sql = static::buildQuery();
$result = static::query($sql)->fetch_assoc();
return $result['quantity'];
}
@ -537,7 +537,7 @@ class ModelMySQL {
* @return ModelMySQL
*/
public static function getById($id){
return self::where('id', $id)->getFirst();
return static::where('id', $id)->getFirst();
}
/*
@ -555,7 +555,7 @@ class ModelMySQL {
$objAtts = array_keys((new $className())->getVars());
}
$db = self::db();
$db = static::db();
$search = $db->real_escape_string($search);
@ -565,10 +565,10 @@ class ModelMySQL {
$where[] = "$row LIKE '%$search%'";
}
if (self::$querySelect['where']=='')
self::$querySelect['where'] = join($where, ' OR ');
if (static::$querySelect['where']=='')
static::$querySelect['where'] = join($where, ' OR ');
else
self::$querySelect['where'] = self::$querySelect['where'] .' AND ('.join($where, ' OR ').')';
static::$querySelect['where'] = static::$querySelect['where'] .' AND ('.join($where, ' OR ').')';
return new static();
}
@ -579,13 +579,13 @@ class ModelMySQL {
* @return ModelMySQL[]
*/
public static function get(){ // Devuelve array vacío si no encuentra nada
$sql = self::buildQuery();
$result = self::query($sql);
$sql = static::buildQuery();
$result = static::query($sql);
$instaces = [];
while ($row = $result->fetch_assoc()){
$instaces[] = self::getInstance($row);
$instaces[] = static::getInstance($row);
}
return $instaces;
@ -598,8 +598,8 @@ class ModelMySQL {
* Puede retornar un objeto ModelMySQL o null.
*/
public static function getFirst(){ // Devuelve null si no encuentra nada
self::limit(1);
$instaces = self::get();
static::limit(1);
$instaces = static::get();
return empty($instaces) ? null : $instaces[0];
}
@ -609,14 +609,14 @@ class ModelMySQL {
* @return ModelMySQL[]
*/
public static function all(){
$sql = 'SELECT * FROM '.self::table();
$sql = 'SELECT * FROM '.static::table();
$result = self::query($sql);
$result = static::query($sql);
$instaces = [];
while ($row = $result->fetch_assoc()){
$instaces[] = self::getInstance($row);
$instaces[] = static::getInstance($row);
}
return $instaces;