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