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_INFEQ = '<=';
const COND_SUPEQ = '>=';
const COND_LIKE = '>=';
const COND_LIKE = 'LIKE';
/* Attributs */
private $where; // Tableau associatif contenant les conditions
@ -95,7 +95,12 @@
return $this;
/* (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
@ -131,7 +136,12 @@
}
/* (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
=========================================================*/
$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
return $this;
@ -420,12 +437,14 @@
=========================================================*/
/* (1) On met les conditions locales */
$c = 0;
foreach($this->where as $field=>$value){
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($this->where as $field=>$conditions)
$c++;
}
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++;
}
/* (2) On ajoute les jointures */
foreach($this->joined as $localField=>$rows){
@ -436,12 +455,13 @@
/* (3) On ajoute les conditions des jointures */
foreach($this->joined as $rows)
foreach($rows->where as $field=>$value){
if( $c == 0 ) $requestS .= 'WHERE '.$rows->schema['table'].'.'.$field.' '.$value[1].' :'.$rows->schema['table'].'_x_'.$field."\n";
else $requestS .= 'AND '.$rows->schema['table'].'.'.$field.' '.$value[1].' :'.$rows->schema['table'].'_x_'.$field."\n";
foreach($rows->where as $field=>$conditions)
foreach($conditions as $cdt=>$value){
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++;
}
/* [6] On exécute la requête et 'bind' les paramètres
@ -451,20 +471,20 @@
$binded = [];
/* (1) On bind tous les paramètres locaux */
foreach($this->where as $field=>$value)
$binded[':'.$this->schema['table'].'_x_'.$field] = $value[0];
foreach($this->where as $field=>$conditions)
foreach($conditions as $cdt=>$value)
$binded[':'.$this->schema['table'].'_x_'.$field.'_'.$cdt] = $value[0];
/* (3) On bind tous les paramètres des jointures */
foreach($this->joined as $rows)
foreach($rows->where as $field=>$value)
$binded[':'.$rows->schema['table'].'_x_'.$field] = $value[0];
foreach($rows->where as $field=>$conditions)
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
=========================================================*/
var_dump($requestS);
var_dump($binded);
/* (1) On exécute la requête */
$request->execute($binded);

View File

@ -348,13 +348,13 @@
$myUser =
Table::get('user') // Access to table 'user'
->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)
// ->getByUsername('gediaz') // Dynamic getter 'getByMySuperColumn' -> 'my_super_column'
->getById([3, Rows::COND_SUP]) // PRIMARY KEY (if composed, all arguments in array)
->getById([100, Rows::COND_INF]) // PRIMARY KEY (if composed, all arguments in array)
->getByUsername(['g%', Rows::COND_LIKE]) // Dynamic getter 'getByMySuperColumn' -> 'my_super_column'
->select(['mail', 'username', 'firstname']) // Select clause
->select('id_user') // Select clause (added)
->join('id_warehouse', $warehouse) // joins warehouse (with name 'my-warehouse') to column 'id_warehouse'
// ->unique() // unique result
->join('id_warehouse', $warehouse) // joins warehouse (with name 'my-warehouse') to column 'id_warehouse'
// ->unique() // unique result
->fetch(); // Result