Allow change from on ModelMySQL.

This commit is contained in:
kj 2020-03-27 10:25:40 -04:00
parent f6c82c7a8c
commit 2d377eb803

View File

@ -85,6 +85,7 @@ class ModelMySQL {
static::$querySelect = [ static::$querySelect = [
'select' => '*', 'select' => '*',
'where' => '', 'where' => '',
'from' => '',
'leftJoin' => '', 'leftJoin' => '',
'rightJoin' => '', 'rightJoin' => '',
'innerJoin' => '', 'innerJoin' => '',
@ -103,7 +104,13 @@ class ModelMySQL {
* Contiene la sentencia SQL. * Contiene la sentencia SQL.
*/ */
private static function buildQuery() { private static function buildQuery() {
$sql = 'SELECT '.static::$querySelect['select'].' FROM '.static::table(); $sql = 'SELECT '.static::$querySelect['select']:
if (static::$querySelect['from'] != '') {
$sql .= ' FROM '.static::$querySelect['from'];
} else {
$sql .= ' FROM '.static::table();
}
if (static::$querySelect['leftJoin'] != '') { if (static::$querySelect['leftJoin'] != '') {
$sql .= static::$querySelect['leftJoin']; $sql .= static::$querySelect['leftJoin'];
@ -289,6 +296,24 @@ class ModelMySQL {
return new static(); return new static();
} }
/*
* 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();
}
/* /*
* Define el WHERE en la sentencia SQL. * Define el WHERE en la sentencia SQL.
* *