Gestion de plusieurs conditions pour un même champ + gestion des conditions : '<', '<=', '>', '>=', '=', 'LIKE'

This commit is contained in:
xdrm-brackets 2016-07-23 10:01:01 +02:00
parent 7e9c64d516
commit 7b250ec08f
2 changed files with 45 additions and 25 deletions

View File

@ -14,7 +14,7 @@
const COND_SUP = '>'; const COND_SUP = '>';
const COND_INFEQ = '<='; const COND_INFEQ = '<=';
const COND_SUPEQ = '>='; const COND_SUPEQ = '>=';
const COND_LIKE = '>='; const COND_LIKE = 'LIKE';
/* Attributs */ /* Attributs */
private $where; // Tableau associatif contenant les conditions private $where; // Tableau associatif contenant les conditions
@ -95,7 +95,12 @@
return $this; return $this;
/* (5) Si type OK, on enregistre la condition */ /* (5) Si type OK, on enregistre la condition */
$this->where[$keys[0]] = $primary; // {1} Si aucune condition pour ce champ, on crée un tableau //
if( !isset($this->where[$keys[0]]) )
$this->where[$keys[0]] = [];
// {2} On ajoute la condition //
$this->where[$keys[0]][] = $primary;
/* [3] Si clé composée /* [3] Si clé composée
@ -131,7 +136,12 @@
} }
/* (6) Si type OK, on enregistre la condition */ /* (6) Si type OK, on enregistre la condition */
$this->where[$key] = $primary[$i]; // {1} Si aucune condition pour ce champ, on crée un tableau //
if( !isset($this->where[$key]) )
$this->where[$key] = [];
// {2} On ajoute la condition //
$this->where[$key][] = $primary[$i];
} }
@ -210,7 +220,14 @@
/* [3] Si type OK, on enregistre la condition /* [3] Si type OK, on enregistre la condition
=========================================================*/ =========================================================*/
$this->where[$column_name] = $a[0]; /* (1) Si aucune condition pour ce champ, on crée un tableau */
if( !isset($this->where[$column_name]) )
$this->where[$column_name] = [];
/* (2) On ajoute la condition */
$this->where[$column_name][] = $a[0];
// On retourne l'object courant // On retourne l'object courant
return $this; return $this;
@ -420,9 +437,11 @@
=========================================================*/ =========================================================*/
/* (1) On met les conditions locales */ /* (1) On met les conditions locales */
$c = 0; $c = 0;
foreach($this->where as $field=>$value){ foreach($this->where as $field=>$conditions)
if( $c == 0 ) $requestS .= 'WHERE '.$this->schema['table'].'.'.$field.' '.$value[1].' :'.$this->schema['table'].'_x_'.$field."\n";
else $requestS .= 'AND '.$this->schema['table'].'.'.$field.' '.$value[1].' :'.$this->schema['table'].'_x_'.$field."\n"; foreach($conditions as $cdt=>$value){
if( $c == 0 ) $requestS .= 'WHERE '.$this->schema['table'].'.'.$field.' '.$value[1].' :'.$this->schema['table'].'_x_'.$field.'_'.$cdt."\n";
else $requestS .= 'AND '.$this->schema['table'].'.'.$field.' '.$value[1].' :'.$this->schema['table'].'_x_'.$field.'_'.$cdt."\n";
$c++; $c++;
} }
@ -436,9 +455,10 @@
/* (3) On ajoute les conditions des jointures */ /* (3) On ajoute les conditions des jointures */
foreach($this->joined as $rows) foreach($this->joined as $rows)
foreach($rows->where as $field=>$value){ foreach($rows->where as $field=>$conditions)
if( $c == 0 ) $requestS .= 'WHERE '.$rows->schema['table'].'.'.$field.' '.$value[1].' :'.$rows->schema['table'].'_x_'.$field."\n"; foreach($conditions as $cdt=>$value){
else $requestS .= 'AND '.$rows->schema['table'].'.'.$field.' '.$value[1].' :'.$rows->schema['table'].'_x_'.$field."\n"; if( $c == 0 ) $requestS .= 'WHERE '.$rows->schema['table'].'.'.$field.' '.$value[1].' :'.$rows->schema['table'].'_x_'.$field.'_'.$cdt."\n";
else $requestS .= 'AND '.$rows->schema['table'].'.'.$field.' '.$value[1].' :'.$rows->schema['table'].'_x_'.$field.'_'.$cdt."\n";
$c++; $c++;
} }
@ -451,20 +471,20 @@
$binded = []; $binded = [];
/* (1) On bind tous les paramètres locaux */ /* (1) On bind tous les paramètres locaux */
foreach($this->where as $field=>$value) foreach($this->where as $field=>$conditions)
$binded[':'.$this->schema['table'].'_x_'.$field] = $value[0]; foreach($conditions as $cdt=>$value)
$binded[':'.$this->schema['table'].'_x_'.$field.'_'.$cdt] = $value[0];
/* (3) On bind tous les paramètres des jointures */ /* (3) On bind tous les paramètres des jointures */
foreach($this->joined as $rows) foreach($this->joined as $rows)
foreach($rows->where as $field=>$value) foreach($rows->where as $field=>$conditions)
$binded[':'.$rows->schema['table'].'_x_'.$field] = $value[0]; foreach($conditions as $cdt=>$value)
$binded[':'.$rows->schema['table'].'_x_'.$field.'_'.$cdt] = $value[0];
/* [6] On exécute la requête et retourne le résultat /* [6] On exécute la requête et retourne le résultat
=========================================================*/ =========================================================*/
var_dump($requestS);
var_dump($binded);
/* (1) On exécute la requête */ /* (1) On exécute la requête */
$request->execute($binded); $request->execute($binded);

View File

@ -349,8 +349,8 @@
$myUser = $myUser =
Table::get('user') // Access to table 'user' Table::get('user') // Access to table 'user'
->getById([3, Rows::COND_SUP]) // PRIMARY KEY (if composed, all arguments in array) ->getById([3, Rows::COND_SUP]) // PRIMARY KEY (if composed, all arguments in array)
// ->getById([10, Rows::COND_INF]) // PRIMARY KEY (if composed, all arguments in array) ->getById([100, Rows::COND_INF]) // PRIMARY KEY (if composed, all arguments in array)
// ->getByUsername('gediaz') // Dynamic getter 'getByMySuperColumn' -> 'my_super_column' ->getByUsername(['g%', Rows::COND_LIKE]) // Dynamic getter 'getByMySuperColumn' -> 'my_super_column'
->select(['mail', 'username', 'firstname']) // Select clause ->select(['mail', 'username', 'firstname']) // Select clause
->select('id_user') // Select clause (added) ->select('id_user') // Select clause (added)
->join('id_warehouse', $warehouse) // joins warehouse (with name 'my-warehouse') to column 'id_warehouse' ->join('id_warehouse', $warehouse) // joins warehouse (with name 'my-warehouse') to column 'id_warehouse'