add left and right join to ModelMysql.

This commit is contained in:
kj 2020-03-12 00:57:15 -04:00
parent 9539b43212
commit f5967591e1

View File

@ -18,6 +18,9 @@ class ModelMySQL {
static protected $querySelect = [ static protected $querySelect = [
'select' => '*', 'select' => '*',
'where' => '', 'where' => '',
'leftJoin' => '',
'rightJoin' => '',
'on' => '',
'AndOr' => '', 'AndOr' => '',
'orderBy'=>'', 'orderBy'=>'',
'groupBy'=>'', 'groupBy'=>'',
@ -49,6 +52,9 @@ class ModelMySQL {
self::$querySelect = [ self::$querySelect = [
'select' => '*', 'select' => '*',
'where' => '', 'where' => '',
'leftJoin' => '',
'rightJoin' => '',
'on' => '',
'AndOr' => '', 'AndOr' => '',
'orderBy'=>'', 'orderBy'=>'',
'groupBy'=>'', 'groupBy'=>'',
@ -59,6 +65,14 @@ class ModelMySQL {
private static function buildQuery(){ private static function buildQuery(){
$sql = 'SELECT '.self::$querySelect['select'].' FROM '.self::table(); $sql = 'SELECT '.self::$querySelect['select'].' FROM '.self::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 (self::$querySelect['where'] != ''){ if (self::$querySelect['where'] != ''){
$sql .= ' WHERE '.self::$querySelect['where']; $sql .= ' WHERE '.self::$querySelect['where'];
@ -228,6 +242,37 @@ class ModelMySQL {
return new static(); return new static();
} }
public static function leftJoin($table, $columnA, $operator, $columnB = null){
if (is_null($columnB)){
$columnB = $operator;
$operator = '=';
}
$columnA = self::db()->real_escape_string($columnA);
$columnB = self::db()->real_escape_string($columnB);
self::$querySelect['leftJoin'] = $table;
self::$querySelect['on'] = "$columnA$operator$columnB";
return new static();
}
public static function rightJoin($table, $columnA, $operator, $columnB = null){
if (is_null($columnB)){
$columnB = $operator;
$operator = '=';
}
$columnA = self::db()->real_escape_string($columnA);
$columnB = self::db()->real_escape_string($columnB);
self::$querySelect['rightJoin'] = $table;
self::$querySelect['on'] = "$columnA$operator$columnB";
return new static();
}
public static function and($column, $operator, $value=null){ public static function and($column, $operator, $value=null){
if (is_null($value)){ if (is_null($value)){