Add inner join to ModelMySQL.

This commit is contained in:
kj 2020-03-27 08:50:33 -04:00
parent c929a3a454
commit f6c82c7a8c

View File

@ -30,7 +30,7 @@ class ModelMySQL {
'where' => '',
'leftJoin' => '',
'rightJoin' => '',
'on' => '',
'innerJoin' => '',
'AndOr' => '',
'orderBy'=>'',
'groupBy'=>'',
@ -87,7 +87,7 @@ class ModelMySQL {
'where' => '',
'leftJoin' => '',
'rightJoin' => '',
'on' => '',
'innerJoin' => '',
'AndOr' => '',
'orderBy'=>'',
'groupBy'=>'',
@ -106,11 +106,15 @@ class ModelMySQL {
$sql = 'SELECT '.static::$querySelect['select'].' FROM '.static::table();
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'];
$sql .= static::$querySelect['leftJoin'];
}
if(static::$querySelect['rightJoin'] != ''){
$sql .= static::$querySelect['rightJoin'];
}
if(static::$querySelect['innerJoin'] != ''){
$sql .= static::$querySelect['innerJoin'];
}
if (static::$querySelect['where'] != ''){
@ -367,8 +371,7 @@ class ModelMySQL {
$columnA = static::db()->real_escape_string($columnA);
$columnB = static::db()->real_escape_string($columnB);
static::$querySelect['leftJoin'] = $table;
static::$querySelect['on'] = "$columnA$operator$columnB";
static::$querySelect['leftJoin'] .= ' LEFT JOIN ' . $table . ' ON ' . "$columnA$operator$columnB";
return new static();
@ -402,9 +405,40 @@ class ModelMySQL {
$columnA = static::db()->real_escape_string($columnA);
$columnB = static::db()->real_escape_string($columnB);
static::$querySelect['rightJoin'] = $table;
static::$querySelect['on'] = "$columnA$operator$columnB";
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 "="
*/
public static function innerJoin($table, $columnA, $operator, $columnB = null){
if (is_null($columnB)){
$columnB = $operator;
$operator = '=';
}
$columnA = static::db()->real_escape_string($columnA);
$columnB = static::db()->real_escape_string($columnB);
static::$querySelect['innerJoin'] .= ' INNER JOIN ' . $table . ' ON ' . "$columnA$operator$columnB";
return new static();
}