2020-03-08 04:37:19 +01:00
|
|
|
<?php
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
|
|
|
* DuckBrain - Microframework
|
|
|
|
*
|
|
|
|
* Modelo ORM para objetos que hagan uso de una base de datos MySQL.
|
|
|
|
* Depende de que exista Libs\Database para poder funcionar.
|
|
|
|
*
|
|
|
|
* Autor: KJ
|
|
|
|
* Web: https://kj2.me
|
|
|
|
* Licencia: MIT
|
|
|
|
*/
|
2020-03-08 04:37:19 +01:00
|
|
|
|
|
|
|
namespace Libs;
|
|
|
|
|
|
|
|
use Libs\Database;
|
|
|
|
|
|
|
|
class ModelMySQL {
|
|
|
|
|
|
|
|
public $id;
|
|
|
|
|
2020-05-11 09:11:11 +02:00
|
|
|
static protected $primaryKey = 'id';
|
|
|
|
static protected $ignoreSave = ['id'];
|
|
|
|
static protected $forceSave = [];
|
2020-03-08 04:37:19 +01:00
|
|
|
static protected $table;
|
|
|
|
static protected $tableSufix = 's';
|
|
|
|
static protected $db;
|
|
|
|
static protected $querySelect = [
|
2020-03-27 15:47:00 +01:00
|
|
|
'select' => '*',
|
|
|
|
'where' => '',
|
|
|
|
'from' => '',
|
|
|
|
'leftJoin' => '',
|
2020-03-12 05:57:15 +01:00
|
|
|
'rightJoin' => '',
|
2020-03-27 13:50:33 +01:00
|
|
|
'innerJoin' => '',
|
2020-03-27 15:47:00 +01:00
|
|
|
'AndOr' => '',
|
|
|
|
'orderBy' => '',
|
|
|
|
'groupBy' => '',
|
|
|
|
'limit' => '',
|
2020-03-08 04:37:19 +01:00
|
|
|
];
|
2020-03-17 18:46:16 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Sirve para obtener la instancia de la base de datos
|
|
|
|
*
|
|
|
|
* @return mysqli
|
|
|
|
*/
|
2020-03-27 15:25:40 +01:00
|
|
|
private static function db() {
|
2020-03-25 00:37:39 +01:00
|
|
|
if (is_null(static::$db))
|
|
|
|
static::$db = Database::getConnection();
|
2020-03-08 04:37:19 +01:00
|
|
|
|
2020-03-25 00:37:39 +01:00
|
|
|
return static::$db;
|
2020-03-08 04:37:19 +01:00
|
|
|
}
|
|
|
|
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
|
|
|
* Ejecuta una sentencia SQL en la base de datos.
|
|
|
|
*
|
|
|
|
* @param string $query
|
|
|
|
* Contiene la sentencia SQL que se desea ejecutar
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
* En caso de que la sentencia SQL falle, devolverá un error en pantalla.
|
|
|
|
*
|
|
|
|
* @return mysqli_result
|
|
|
|
* Contiene el resultado de la llamada SQL.
|
|
|
|
*/
|
2020-03-27 15:25:40 +01:00
|
|
|
private static function query($query) {
|
2020-03-25 00:37:39 +01:00
|
|
|
$db = static::db();
|
2020-03-08 04:37:19 +01:00
|
|
|
|
|
|
|
$result = $db->query($query);
|
2020-03-27 15:25:40 +01:00
|
|
|
if ($db->errno) {
|
2020-03-17 18:46:16 +01:00
|
|
|
echo '<style>body{white-space: pre-line;}</style>';
|
2020-05-11 09:21:47 +02:00
|
|
|
throw new \Exception(
|
2020-05-11 09:13:32 +02:00
|
|
|
"\nFallo al consultar la base de datos\n" .
|
|
|
|
"Errno: $db->errno\n" .
|
|
|
|
"Error: $db->error\n" .
|
|
|
|
"Query: $query\n"
|
|
|
|
);
|
2020-03-08 04:37:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
|
|
|
* Reinicia la configuración de la sentencia SQL.
|
|
|
|
*/
|
2020-04-18 20:22:21 +02:00
|
|
|
protected static function resetQuery() {
|
2020-03-25 00:37:39 +01:00
|
|
|
static::$querySelect = [
|
2020-03-27 15:25:40 +01:00
|
|
|
'select' => '*',
|
|
|
|
'where' => '',
|
|
|
|
'from' => '',
|
|
|
|
'leftJoin' => '',
|
2020-03-12 05:57:15 +01:00
|
|
|
'rightJoin' => '',
|
2020-03-27 13:50:33 +01:00
|
|
|
'innerJoin' => '',
|
2020-03-27 15:25:40 +01:00
|
|
|
'AndOr' => '',
|
|
|
|
'orderBy' => '',
|
|
|
|
'groupBy' => '',
|
|
|
|
'limit' => '',
|
2020-03-08 04:37:19 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
2020-03-25 00:37:39 +01:00
|
|
|
* Construye la sentencia SQL a partir static::$querySelect y una vez
|
2020-03-17 18:46:16 +01:00
|
|
|
* construída, llama a resetQuery.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* Contiene la sentencia SQL.
|
|
|
|
*/
|
2020-03-27 15:25:40 +01:00
|
|
|
private static function buildQuery() {
|
2020-03-27 15:47:00 +01:00
|
|
|
$sql = 'SELECT '.static::$querySelect['select'];
|
2020-03-25 00:37:39 +01:00
|
|
|
|
2020-03-27 15:25:40 +01:00
|
|
|
if (static::$querySelect['from'] != '') {
|
|
|
|
$sql .= ' FROM '.static::$querySelect['from'];
|
|
|
|
} else {
|
|
|
|
$sql .= ' FROM '.static::table();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (static::$querySelect['leftJoin'] != '') {
|
2020-03-27 13:50:33 +01:00
|
|
|
$sql .= static::$querySelect['leftJoin'];
|
|
|
|
}
|
|
|
|
|
2020-03-27 15:25:40 +01:00
|
|
|
if(static::$querySelect['rightJoin'] != '') {
|
2020-03-27 13:50:33 +01:00
|
|
|
$sql .= static::$querySelect['rightJoin'];
|
|
|
|
}
|
|
|
|
|
2020-03-27 15:25:40 +01:00
|
|
|
if(static::$querySelect['innerJoin'] != '') {
|
2020-03-27 13:50:33 +01:00
|
|
|
$sql .= static::$querySelect['innerJoin'];
|
2020-03-12 05:57:15 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 15:25:40 +01:00
|
|
|
if (static::$querySelect['where'] != '') {
|
2020-03-25 00:37:39 +01:00
|
|
|
$sql .= ' WHERE '.static::$querySelect['where'];
|
2020-03-08 04:37:19 +01:00
|
|
|
|
2020-03-27 15:25:40 +01:00
|
|
|
if (static::$querySelect['AndOr'] != '') {
|
2020-03-25 00:37:39 +01:00
|
|
|
$sql .= static::$querySelect['AndOr'];
|
2020-03-08 04:37:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 15:25:40 +01:00
|
|
|
if (static::$querySelect['groupBy'] != '') {
|
2020-03-25 00:37:39 +01:00
|
|
|
$sql .= ' GROUP BY '.static::$querySelect['groupBy'];
|
2020-03-08 04:37:19 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 15:25:40 +01:00
|
|
|
if (static::$querySelect['orderBy'] != '') {
|
2020-03-25 00:37:39 +01:00
|
|
|
$sql .= ' ORDER BY '.static::$querySelect['orderBy'];
|
2020-03-08 04:37:19 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 15:25:40 +01:00
|
|
|
if (static::$querySelect['limit'] != '') {
|
2020-03-25 00:37:39 +01:00
|
|
|
$sql .= ' LIMIT '.static::$querySelect['limit'];
|
2020-03-08 04:37:19 +01:00
|
|
|
}
|
|
|
|
|
2020-03-25 00:37:39 +01:00
|
|
|
static::resetQuery();
|
2020-03-08 04:37:19 +01:00
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
|
|
|
* Crea una instancia del objeto actual a partir de un arreglo.
|
|
|
|
*
|
|
|
|
* @param mixed $elem
|
|
|
|
* Puede recibir un arreglo o un objeto que contiene los valores
|
|
|
|
* que tendrán sus atributos.
|
|
|
|
*
|
|
|
|
* @return ModelMySQL
|
|
|
|
* Retorna un objeto de la clase actual.
|
|
|
|
*/
|
2020-04-09 23:39:45 +02:00
|
|
|
public static function getInstance($elem = []) {
|
2020-03-08 04:37:19 +01:00
|
|
|
$class = get_called_class();
|
2020-05-02 01:17:47 +02:00
|
|
|
$instance = new $class;
|
2020-03-08 04:37:19 +01:00
|
|
|
|
2020-03-27 15:25:40 +01:00
|
|
|
foreach ($elem as $key => $value) {
|
2020-05-02 01:17:47 +02:00
|
|
|
$instance->$key = $value;
|
2020-03-08 04:37:19 +01:00
|
|
|
}
|
|
|
|
|
2020-05-02 01:17:47 +02:00
|
|
|
return $instance;
|
2020-03-08 04:37:19 +01:00
|
|
|
}
|
|
|
|
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
|
|
|
* @return array
|
|
|
|
* Contiene los atributos indexados del objeto actual.
|
|
|
|
*/
|
2020-04-10 00:22:47 +02:00
|
|
|
private function getVars() {
|
|
|
|
$reflection = new \ReflectionClass($this);
|
|
|
|
$properties = $reflection->getProperties(\ReflectionProperty::IS_PUBLIC);
|
|
|
|
$result = [];
|
|
|
|
|
|
|
|
foreach($properties as $property) {
|
|
|
|
$att = $property->name;
|
|
|
|
$result[$att] = $this->$att;
|
|
|
|
}
|
|
|
|
|
2020-05-11 09:11:11 +02:00
|
|
|
foreach (static::$ignoreSave as $del) {
|
2020-03-08 04:37:19 +01:00
|
|
|
unset($result[$del]);
|
|
|
|
}
|
|
|
|
|
2020-05-11 09:11:11 +02:00
|
|
|
foreach (static::$forceSave as $value) {
|
2020-03-08 04:37:19 +01:00
|
|
|
$result[$value] = $this->$value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
|
|
|
* @return string
|
|
|
|
* Devuelve el nombre de la clase actual
|
|
|
|
*/
|
2020-03-27 15:25:40 +01:00
|
|
|
public static function className() {
|
2020-03-08 04:37:19 +01:00
|
|
|
return strtolower(substr(strrchr(get_called_class(), '\\'), 1));
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
2020-03-25 00:37:39 +01:00
|
|
|
* Construye (a partir del nombre de la clase y el sufijo en static::$tableSufix)
|
2020-03-17 18:46:16 +01:00
|
|
|
* y/o develve el nombre de la tabla de la BD en la que se alojará o
|
|
|
|
* se aloja el objeto actual.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-03-27 15:25:40 +01:00
|
|
|
private static function table() {
|
2020-03-25 00:37:39 +01:00
|
|
|
if (isset(static::$table))
|
|
|
|
return static::$table;
|
|
|
|
return static::className().static::$tableSufix;
|
2020-03-08 04:37:19 +01:00
|
|
|
}
|
|
|
|
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
|
|
|
* Actualiza los valores en la BD con los valores del objeto actual
|
|
|
|
*/
|
2020-03-27 15:25:40 +01:00
|
|
|
private function update() {
|
2020-03-08 04:37:19 +01:00
|
|
|
$atts = $this->getVars();
|
|
|
|
|
2020-03-27 15:25:40 +01:00
|
|
|
foreach ($atts as $key => $value) {
|
2020-03-25 00:37:39 +01:00
|
|
|
$value = static::db()->real_escape_string($value);
|
2020-03-08 04:37:19 +01:00
|
|
|
$set[]="$key='$value'";
|
|
|
|
}
|
|
|
|
|
2020-03-25 00:37:39 +01:00
|
|
|
$table = static::table();
|
2020-05-11 09:11:11 +02:00
|
|
|
$pk = static::$primaryKey;
|
2020-03-08 04:37:19 +01:00
|
|
|
$pkv = $this->$pk;
|
|
|
|
$sql = "UPDATE $table SET ".join(', ', $set)." WHERE $pk='$pkv'";
|
2020-03-25 00:37:39 +01:00
|
|
|
static::query($sql);
|
2020-03-08 04:37:19 +01:00
|
|
|
}
|
|
|
|
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
|
|
|
* Inserta una nueva fila en la base de datos a partir del
|
|
|
|
* objeto actual.
|
|
|
|
*/
|
2020-03-27 15:25:40 +01:00
|
|
|
private function add() {
|
2020-03-25 00:37:39 +01:00
|
|
|
$db = static::db();
|
2020-03-08 04:37:19 +01:00
|
|
|
$atts = $this->getVars();
|
|
|
|
|
2020-03-27 15:25:40 +01:00
|
|
|
foreach ($atts as $key => $value) {
|
2020-07-18 03:54:17 +02:00
|
|
|
if (isset($value)) {
|
|
|
|
$into[] = "`$key`";
|
|
|
|
$values[] = "'".$db->real_escape_string($value)."'";
|
|
|
|
}
|
2020-03-08 04:37:19 +01:00
|
|
|
}
|
|
|
|
|
2020-03-25 00:37:39 +01:00
|
|
|
$table = static::table();
|
2020-03-08 04:37:19 +01:00
|
|
|
$sql = "INSERT INTO $table (".join(', ', $into).") VALUES (".join(', ', $values).")";
|
2020-03-25 00:37:39 +01:00
|
|
|
static::query($sql);
|
2020-03-08 04:37:19 +01:00
|
|
|
|
2020-05-11 09:11:11 +02:00
|
|
|
$pk = static::$primaryKey;
|
2020-03-08 04:37:19 +01:00
|
|
|
$this->$pk = $db->insert_id;
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
|
|
|
* Revisa si el objeto a guardar es nuevo o no y según el resultado
|
|
|
|
* llama a update para actualizar o add para insertar una nueva fila.
|
|
|
|
*/
|
2020-03-27 15:25:40 +01:00
|
|
|
public function save() {
|
2020-05-11 09:11:11 +02:00
|
|
|
$pk = static::$primaryKey;
|
2020-03-08 04:37:19 +01:00
|
|
|
if (isset($this->$pk))
|
|
|
|
$this->update();
|
|
|
|
else
|
|
|
|
$this->add();
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
|
|
|
* Elimina el objeto actual de la base de datos.
|
|
|
|
*/
|
2020-03-27 15:25:40 +01:00
|
|
|
public function delete() {
|
2020-03-08 04:37:19 +01:00
|
|
|
$atts = $this->getVars();
|
|
|
|
|
2020-03-27 15:25:40 +01:00
|
|
|
foreach ($atts as $key => $value) {
|
2020-03-25 00:37:39 +01:00
|
|
|
$value = static::db()->real_escape_string($value);
|
2020-03-08 04:37:19 +01:00
|
|
|
$set[]="$key='$value'";
|
|
|
|
}
|
|
|
|
|
2020-03-25 00:37:39 +01:00
|
|
|
$table = static::table();
|
2020-05-11 09:11:11 +02:00
|
|
|
$pk = static::$primaryKey;
|
2020-03-08 04:37:19 +01:00
|
|
|
$pkv = $this->$pk;
|
|
|
|
$sql = "DELETE FROM $table WHERE $pk='$pkv'";
|
2020-03-25 00:37:39 +01:00
|
|
|
static::query($sql);
|
2020-03-08 04:37:19 +01:00
|
|
|
}
|
|
|
|
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
|
|
|
* Define SELECT en la sentencia SQL.
|
|
|
|
*
|
|
|
|
* @param array $columns
|
|
|
|
* Columnas que se selecionarán en la consulta SQL.
|
|
|
|
*/
|
2020-03-27 15:25:40 +01:00
|
|
|
public static function select($columns) {
|
2020-03-25 00:37:39 +01:00
|
|
|
$db = static::db();
|
2020-03-08 04:37:19 +01:00
|
|
|
$select = [];
|
2020-03-27 15:25:40 +01:00
|
|
|
foreach($columns as $column) {
|
2020-03-17 18:46:16 +01:00
|
|
|
$select[] = $db->real_escape_string($column);
|
2020-03-08 04:37:19 +01:00
|
|
|
}
|
|
|
|
|
2020-03-25 00:37:39 +01:00
|
|
|
static::$querySelect['select'] = join(', ', $select);
|
2020-03-08 04:37:19 +01:00
|
|
|
|
|
|
|
return new static();
|
|
|
|
}
|
|
|
|
|
2020-03-27 15:25:40 +01:00
|
|
|
/*
|
|
|
|
* Define FROM en la sentencia SQL.
|
|
|
|
*
|
|
|
|
* @param array $tables
|
|
|
|
* Tablas que se selecionarán en la consulta SQL.
|
|
|
|
*/
|
|
|
|
public static function from($tables) {
|
|
|
|
$db = static::db();
|
|
|
|
$from = [];
|
|
|
|
foreach($tables as $table) {
|
|
|
|
$from[] = $db->real_escape_string($table);
|
|
|
|
}
|
|
|
|
|
|
|
|
static::$querySelect['from'] = join(', ', $from);
|
|
|
|
|
|
|
|
return new static();
|
|
|
|
}
|
|
|
|
|
2020-03-08 04:37:19 +01:00
|
|
|
/*
|
2020-03-17 18:46:16 +01:00
|
|
|
* Define el WHERE en la sentencia SQL.
|
|
|
|
*
|
|
|
|
* @param string $column
|
|
|
|
* La columna a comparar.
|
|
|
|
*
|
|
|
|
* @param string $operador
|
|
|
|
* El operador o el valor a comparar en la columna en caso de que eloperador sea "=".
|
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* El valor el valor a comparar en la columna.
|
|
|
|
*
|
2020-04-04 00:00:48 +02:00
|
|
|
* @param $no_quote
|
|
|
|
* Se usa cuando $value es una columna o un valor que no requiere comillas
|
2020-03-27 15:47:00 +01:00
|
|
|
*
|
2020-03-17 18:46:16 +01:00
|
|
|
* Sintaxis posibles:
|
2020-03-25 00:37:39 +01:00
|
|
|
* - static::where(columna, operador, valor)
|
|
|
|
* - static::where(columna, valor) // Operador por defecto "="
|
2020-03-08 04:37:19 +01:00
|
|
|
*/
|
2020-04-04 00:00:48 +02:00
|
|
|
public static function where($column, $operator, $value=null, $no_quote = false) {
|
2020-03-27 15:25:40 +01:00
|
|
|
if (is_null($value)) {
|
2020-03-08 04:37:19 +01:00
|
|
|
$value = $operator;
|
|
|
|
$operator = '=';
|
|
|
|
}
|
|
|
|
|
2020-03-25 00:37:39 +01:00
|
|
|
$value = static::db()->real_escape_string($value);
|
2020-03-08 04:37:19 +01:00
|
|
|
|
2020-04-04 00:00:48 +02:00
|
|
|
if ($no_quote)
|
2020-03-27 15:47:00 +01:00
|
|
|
static::$querySelect['where'] = "$column$operator$value";
|
|
|
|
else
|
|
|
|
static::$querySelect['where'] = "$column$operator'$value'";
|
2020-03-08 04:37:19 +01:00
|
|
|
|
|
|
|
return new static();
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
|
|
|
* Define WHERE usando IN en la sentencia SQL.
|
|
|
|
*
|
|
|
|
* @param string $column
|
|
|
|
* La columna a comparar.
|
|
|
|
*
|
|
|
|
* @param array $arr
|
|
|
|
* Arreglo con todos los valores a comparar con la columna.
|
|
|
|
*
|
|
|
|
* @param boolean $in
|
|
|
|
* Define si se usará IN o NOT IN en la sentencia SQL.
|
|
|
|
*/
|
2020-03-27 15:25:40 +01:00
|
|
|
public static function where_in($column,$arr, $in = true) {
|
|
|
|
foreach($arr as $index => $value) {
|
2020-03-25 00:37:39 +01:00
|
|
|
$arr[$index] = static::db()->real_escape_string($value);
|
2020-03-08 04:37:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($in)
|
2020-03-25 00:37:39 +01:00
|
|
|
static::$querySelect['where'] = "$column IN (".join(', ',$arr).")";
|
2020-03-08 04:37:19 +01:00
|
|
|
else
|
2020-03-25 00:37:39 +01:00
|
|
|
static::$querySelect['where'] = "$column NOT IN (".join(', ',$arr).")";
|
2020-03-08 04:37:19 +01:00
|
|
|
|
|
|
|
return new static();
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
|
|
|
* Define LEFT JOIN en la sentencia SQL.
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* Tabla que se va a juntar a la del objeto actual.
|
|
|
|
*
|
|
|
|
* @param string $columnA
|
|
|
|
* Columna a comparar para hacer el join.
|
|
|
|
*
|
|
|
|
* @param string $operador
|
|
|
|
* Operador o columna a comparar para hacer el join en caso de que el operador sea "=".
|
|
|
|
*
|
|
|
|
* @param string $columnB
|
|
|
|
* Columna a comparar para hacer el join.
|
|
|
|
*
|
|
|
|
* Sintaxis posibles:
|
2020-03-25 00:37:39 +01:00
|
|
|
* - static::leftJoin(tabla,columnaA, operador, columnB)
|
|
|
|
* - static::leftJoin(tabla,columnaA, columnB) // Operador por defecto "="
|
2020-03-17 18:46:16 +01:00
|
|
|
*/
|
2020-03-27 15:25:40 +01:00
|
|
|
public static function leftJoin($table, $columnA, $operator, $columnB = null) {
|
|
|
|
if (is_null($columnB)) {
|
2020-03-12 05:57:15 +01:00
|
|
|
$columnB = $operator;
|
|
|
|
$operator = '=';
|
|
|
|
}
|
|
|
|
|
2020-03-25 00:37:39 +01:00
|
|
|
$columnA = static::db()->real_escape_string($columnA);
|
|
|
|
$columnB = static::db()->real_escape_string($columnB);
|
2020-03-12 05:57:15 +01:00
|
|
|
|
2020-03-27 13:50:33 +01:00
|
|
|
static::$querySelect['leftJoin'] .= ' LEFT JOIN ' . $table . ' ON ' . "$columnA$operator$columnB";
|
2020-03-12 05:57:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
return new static();
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
|
|
|
* Define RIGHT JOIN en la sentencia SQL.
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* Tabla que se va a juntar a la del objeto actual.
|
|
|
|
*
|
|
|
|
* @param string $columnA
|
|
|
|
* Columna a comparar para hacer el join.
|
|
|
|
*
|
|
|
|
* @param string $operador
|
|
|
|
* Operador o columna a comparar para hacer el join en caso de que el operador sea "=".
|
|
|
|
*
|
|
|
|
* @param string $columnB
|
|
|
|
* Columna a comparar para hacer el join.
|
|
|
|
*
|
|
|
|
* Sintaxis posibles:
|
2020-03-25 00:37:39 +01:00
|
|
|
* - static::rightJoin(tabla,columnaA, operador, columnB)
|
|
|
|
* - static::rightJoin(tabla,columnaA, columnB) // Operador por defecto "="
|
2020-03-17 18:46:16 +01:00
|
|
|
*/
|
2020-03-27 15:25:40 +01:00
|
|
|
public static function rightJoin($table, $columnA, $operator, $columnB = null) {
|
|
|
|
if (is_null($columnB)) {
|
2020-03-12 05:57:15 +01:00
|
|
|
$columnB = $operator;
|
|
|
|
$operator = '=';
|
|
|
|
}
|
|
|
|
|
2020-03-25 00:37:39 +01:00
|
|
|
$columnA = static::db()->real_escape_string($columnA);
|
|
|
|
$columnB = static::db()->real_escape_string($columnB);
|
2020-03-12 05:57:15 +01:00
|
|
|
|
2020-03-27 13:50:33 +01:00
|
|
|
static::$querySelect['rightJoin'] .= ' RIGHT JOIN ' . $table . ' ON ' . "$columnA$operator$columnB";
|
|
|
|
|
|
|
|
return new static();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Define INNER JOIN en la sentencia SQL.
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* Tabla que se va a juntar a la del objeto actual.
|
|
|
|
*
|
|
|
|
* @param string $columnA
|
|
|
|
* Columna a comparar para hacer el join.
|
|
|
|
*
|
|
|
|
* @param string $operador
|
|
|
|
* Operador o columna a comparar para hacer el join en caso de que el operador sea "=".
|
|
|
|
*
|
|
|
|
* @param string $columnB
|
|
|
|
* Columna a comparar para hacer el join.
|
|
|
|
*
|
|
|
|
* Sintaxis posibles:
|
|
|
|
* - static::innerJoin(tabla,columnaA, operador, columnB)
|
|
|
|
* - static::innerJoin(tabla,columnaA, columnB) // Operador por defecto "="
|
|
|
|
*/
|
2020-03-27 15:25:40 +01:00
|
|
|
public static function innerJoin($table, $columnA, $operator, $columnB = null) {
|
|
|
|
if (is_null($columnB)) {
|
2020-03-27 13:50:33 +01:00
|
|
|
$columnB = $operator;
|
|
|
|
$operator = '=';
|
|
|
|
}
|
|
|
|
|
|
|
|
$columnA = static::db()->real_escape_string($columnA);
|
|
|
|
$columnB = static::db()->real_escape_string($columnB);
|
2020-03-12 05:57:15 +01:00
|
|
|
|
2020-03-27 13:50:33 +01:00
|
|
|
static::$querySelect['innerJoin'] .= ' INNER JOIN ' . $table . ' ON ' . "$columnA$operator$columnB";
|
2020-03-12 05:57:15 +01:00
|
|
|
|
|
|
|
return new static();
|
|
|
|
}
|
2020-03-08 04:37:19 +01:00
|
|
|
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
|
|
|
* Define AND en la sentencia SQL (se puede anidar).
|
|
|
|
*
|
|
|
|
* @param string $column
|
|
|
|
* La columna a comparar.
|
|
|
|
*
|
|
|
|
* @param string $operador
|
|
|
|
* El operador o el valor a comparar en la columna en caso de que eloperador sea "=".
|
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* El valor el valor a comparar en la columna.
|
|
|
|
*
|
2020-04-04 00:00:48 +02:00
|
|
|
* @param $no_quote
|
|
|
|
* Se usa cuando $value es una columna o un valor que no requiere comillas
|
2020-03-27 15:47:00 +01:00
|
|
|
*
|
2020-03-17 18:46:16 +01:00
|
|
|
* Sintaxis posibles:
|
2020-03-25 00:37:39 +01:00
|
|
|
* - static::and(columna, operador, valor)
|
|
|
|
* - static::and(columna, valor) // Operador por defecto "="
|
|
|
|
* - static::and(columna, valor)->and(columna, valor)->and(columna, valor) // anidado
|
2020-03-17 18:46:16 +01:00
|
|
|
*/
|
2020-04-04 00:00:48 +02:00
|
|
|
public static function and($column, $operator, $value=null, $no_quote = false) {
|
2020-03-27 15:25:40 +01:00
|
|
|
if (is_null($value)) {
|
2020-03-08 04:37:19 +01:00
|
|
|
$value = $operator;
|
|
|
|
$operator = '=';
|
|
|
|
}
|
|
|
|
|
2020-03-25 00:37:39 +01:00
|
|
|
$value = static::db()->real_escape_string($value);
|
2020-03-08 04:37:19 +01:00
|
|
|
|
2020-04-04 00:00:48 +02:00
|
|
|
if ($no_quote)
|
2020-03-27 15:47:00 +01:00
|
|
|
static::$querySelect['AndOr'] .= " AND $column$operator$value";
|
|
|
|
else
|
|
|
|
static::$querySelect['AndOr'] .= " AND $column$operator'$value'";
|
2020-03-08 04:37:19 +01:00
|
|
|
|
|
|
|
return new static();
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
|
|
|
* Define OR en la sentencia SQL (se puede anidar).
|
|
|
|
*
|
|
|
|
* @param string $column
|
|
|
|
* La columna a comparar.
|
|
|
|
*
|
|
|
|
* @param string $operador
|
|
|
|
* El operador o el valor a comparar en la columna en caso de que eloperador sea "=".
|
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* El valor el valor a comparar en la columna.
|
|
|
|
*
|
2020-04-04 00:00:48 +02:00
|
|
|
* @param $no_quote
|
|
|
|
* Se usa cuando $value es una columna o un valor que no requiere comillas
|
2020-03-27 15:47:00 +01:00
|
|
|
*
|
2020-03-17 18:46:16 +01:00
|
|
|
* Sintaxis posibles:
|
2020-03-25 00:37:39 +01:00
|
|
|
* - static::or(columna, operador, valor)
|
|
|
|
* - static::or(columna, valor) // Operador por defecto "="
|
|
|
|
* - static::or(columna, valor)->or(columna, valor)->or(columna, valor) // anidado
|
2020-03-17 18:46:16 +01:00
|
|
|
*/
|
2020-04-04 00:00:48 +02:00
|
|
|
public static function or($column, $operator, $value=null, $no_quote = false) {
|
2020-03-27 15:25:40 +01:00
|
|
|
if (is_null($value)) {
|
2020-03-08 04:37:19 +01:00
|
|
|
$value = $operator;
|
|
|
|
$operator = '=';
|
|
|
|
}
|
|
|
|
|
2020-03-25 00:37:39 +01:00
|
|
|
$value = static::db()->real_escape_string($value);
|
2020-03-08 04:37:19 +01:00
|
|
|
|
2020-04-04 00:00:48 +02:00
|
|
|
if ($no_quote)
|
2020-03-27 15:47:00 +01:00
|
|
|
static::$querySelect['AndOr'] .= " OR $column$operator$value";
|
|
|
|
else
|
|
|
|
static::$querySelect['AndOr'] .= " OR $column$operator'$value'";
|
2020-03-08 04:37:19 +01:00
|
|
|
|
|
|
|
return new static();
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
|
|
|
* Define GROUP BY en la sentencia SQL
|
|
|
|
*
|
|
|
|
* @param array $arr
|
|
|
|
* Columnas por las que se agrupará.
|
|
|
|
*/
|
2020-03-27 15:25:40 +01:00
|
|
|
public static function groupBy($arr) {
|
2020-03-25 00:37:39 +01:00
|
|
|
static::$querySelect['groupBy'] = join(', ', $arr);
|
2020-03-08 04:37:19 +01:00
|
|
|
return new static();
|
|
|
|
}
|
|
|
|
|
2020-03-27 15:25:40 +01:00
|
|
|
public static function limit($initial, $final = 0) {
|
2020-03-08 04:37:19 +01:00
|
|
|
$initial = (int)$initial;
|
|
|
|
$final = (int)$final;
|
|
|
|
|
|
|
|
if ($final==0)
|
2020-03-25 00:37:39 +01:00
|
|
|
static::$querySelect['limit'] = $initial;
|
2020-03-08 04:37:19 +01:00
|
|
|
else
|
2020-03-25 00:37:39 +01:00
|
|
|
static::$querySelect['limit'] = $initial.', '.$final;
|
2020-03-08 04:37:19 +01:00
|
|
|
|
|
|
|
return new static();
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
|
|
|
* Define ORDER BY en la sentencia SQL
|
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* Columna por la que se ordenará.
|
|
|
|
*
|
|
|
|
* @param string $order
|
|
|
|
* Define si el orden será de manera ascendente (ASC),
|
|
|
|
* descendente (DESC) o aleatorio (RAND).
|
|
|
|
*/
|
2020-03-27 15:25:40 +01:00
|
|
|
public static function orderBy($value, $order = 'ASC') {
|
|
|
|
if ($value == "RAND") {
|
2020-03-25 00:37:39 +01:00
|
|
|
static::$querySelect['orderBy'] = 'RAND()';
|
2020-03-08 04:37:19 +01:00
|
|
|
return new static();
|
|
|
|
}
|
|
|
|
|
2020-03-25 00:37:39 +01:00
|
|
|
$value = static::db()->real_escape_string($value);
|
2020-03-08 04:37:19 +01:00
|
|
|
|
|
|
|
if (!(strtoupper($order) == 'ASC' || strtoupper($order) == 'DESC'))
|
|
|
|
$order = 'ASC';
|
|
|
|
|
2020-03-25 00:37:39 +01:00
|
|
|
static::$querySelect['orderBy'] = $value.' '.$order;
|
2020-03-08 04:37:19 +01:00
|
|
|
|
|
|
|
return new static();
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
|
|
|
* Retorna la cantidad de filas que hay en un query.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2020-03-27 15:25:40 +01:00
|
|
|
public static function count() {
|
2020-03-25 00:37:39 +01:00
|
|
|
static::$querySelect['select'] = 'count(*) as quantity';
|
|
|
|
$sql = static::buildQuery();
|
|
|
|
$result = static::query($sql)->fetch_assoc();
|
2020-03-08 04:37:19 +01:00
|
|
|
return $result['quantity'];
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
|
|
|
* Obtiene una instancia según su id.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return ModelMySQL
|
|
|
|
*/
|
2020-03-27 15:25:40 +01:00
|
|
|
public static function getById($id) {
|
2020-05-11 09:11:11 +02:00
|
|
|
return static::where(static::$primaryKey, $id)->getFirst();
|
2020-03-08 04:37:19 +01:00
|
|
|
}
|
|
|
|
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
|
|
|
* Realiza una búsqueda en la tabla de la instancia actual.
|
|
|
|
*
|
|
|
|
* @param string $search
|
|
|
|
* Contenido a buscar.
|
|
|
|
*
|
|
|
|
* @param array $in
|
|
|
|
* Columnas en las que se va a buscar (null para buscar en todas)
|
|
|
|
*/
|
2020-03-27 15:25:40 +01:00
|
|
|
public static function search($search, $in = null) {
|
|
|
|
if ($in == null) {
|
2020-03-17 18:46:16 +01:00
|
|
|
$className = get_called_class();
|
|
|
|
$objAtts = array_keys((new $className())->getVars());
|
2020-03-08 04:37:19 +01:00
|
|
|
}
|
|
|
|
|
2020-03-25 00:37:39 +01:00
|
|
|
$db = static::db();
|
2020-03-08 04:37:19 +01:00
|
|
|
|
|
|
|
$search = $db->real_escape_string($search);
|
|
|
|
|
|
|
|
$where = [];
|
|
|
|
|
2020-03-27 15:25:40 +01:00
|
|
|
foreach($in as $row) {
|
2020-03-17 18:53:48 +01:00
|
|
|
$where[] = "$row LIKE '%$search%'";
|
2020-03-08 04:37:19 +01:00
|
|
|
}
|
|
|
|
|
2020-03-25 00:37:39 +01:00
|
|
|
if (static::$querySelect['where']=='')
|
|
|
|
static::$querySelect['where'] = join($where, ' OR ');
|
2020-03-08 04:37:19 +01:00
|
|
|
else
|
2020-03-25 00:37:39 +01:00
|
|
|
static::$querySelect['where'] = static::$querySelect['where'] .' AND ('.join($where, ' OR ').')';
|
2020-03-08 04:37:19 +01:00
|
|
|
|
|
|
|
return new static();
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
|
|
|
* Obtener los resultados de la consulta SQL.
|
|
|
|
*
|
|
|
|
* @return ModelMySQL[]
|
|
|
|
*/
|
2020-03-27 15:25:40 +01:00
|
|
|
public static function get() { // Devuelve array vacío si no encuentra nada
|
2020-03-25 00:37:39 +01:00
|
|
|
$sql = static::buildQuery();
|
|
|
|
$result = static::query($sql);
|
2020-03-08 04:37:19 +01:00
|
|
|
|
2020-05-02 20:04:49 +02:00
|
|
|
$instances = [];
|
2020-03-08 04:37:19 +01:00
|
|
|
|
2020-03-27 15:25:40 +01:00
|
|
|
while ($row = $result->fetch_assoc()) {
|
2020-05-02 20:04:49 +02:00
|
|
|
$instances[] = static::getInstance($row);
|
2020-03-08 04:37:19 +01:00
|
|
|
}
|
|
|
|
|
2020-05-02 20:04:49 +02:00
|
|
|
return $instances;
|
2020-03-08 04:37:19 +01:00
|
|
|
}
|
|
|
|
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
|
|
|
* El primer elemento de la consulta SQL.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
* Puede retornar un objeto ModelMySQL o null.
|
|
|
|
*/
|
2020-03-27 15:25:40 +01:00
|
|
|
public static function getFirst() { // Devuelve null si no encuentra nada
|
2020-03-25 00:37:39 +01:00
|
|
|
static::limit(1);
|
2020-05-02 20:04:49 +02:00
|
|
|
$instances = static::get();
|
|
|
|
return empty($instances) ? null : $instances[0];
|
2020-03-08 04:37:19 +01:00
|
|
|
}
|
|
|
|
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
|
|
|
* Obtener todos los elementos del la tabla de la instancia actual.
|
|
|
|
*
|
|
|
|
* @return ModelMySQL[]
|
|
|
|
*/
|
2020-03-27 15:25:40 +01:00
|
|
|
public static function all() {
|
2020-03-25 00:37:39 +01:00
|
|
|
$sql = 'SELECT * FROM '.static::table();
|
2020-03-08 04:37:19 +01:00
|
|
|
|
2020-03-25 00:37:39 +01:00
|
|
|
$result = static::query($sql);
|
2020-03-08 04:37:19 +01:00
|
|
|
|
2020-05-02 20:04:49 +02:00
|
|
|
$instances = [];
|
2020-03-08 04:37:19 +01:00
|
|
|
|
2020-03-27 15:25:40 +01:00
|
|
|
while ($row = $result->fetch_assoc()) {
|
2020-05-02 20:04:49 +02:00
|
|
|
$instances[] = static::getInstance($row);
|
2020-03-08 04:37:19 +01:00
|
|
|
}
|
|
|
|
|
2020-05-02 20:04:49 +02:00
|
|
|
return $instances;
|
2020-03-08 04:37:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|