ORM: Delete avec condition 'IN'

This commit is contained in:
xdrm-brackets 2016-07-24 12:18:30 +02:00
parent 81ff8d158e
commit 4b25bbe008
3 changed files with 64 additions and 42 deletions

View File

@ -611,59 +611,59 @@
/* [2] On rédige la clause WHERE/AND /* [2] On rédige la clause WHERE/AND
=========================================================*/ =========================================================*/
/* (0) On initialise les variables à "binder" */
$binded = [];
/* (1) On met les conditions locales */ /* (1) On met les conditions locales */
$c = 0; $c = 0;
foreach($this->where as $field=>$conditions) foreach($this->where as $field=>$conditions)
foreach($conditions as $cdt=>$value){ foreach($conditions as $cdt=>$value){
if( $c == 0 ) $requestS .= 'WHERE '.$this->schema['table'].'.'.$field.' '.substr($value[1], 2, -2).' :'.$this->schema['table'].'_x_'.$field.'_'.$cdt."\n";
else $requestS .= 'AND '.$this->schema['table'].'.'.$field.' '.substr($value[1], 2, -2).' :'.$this->schema['table'].'_x_'.$field.'_'.$cdt."\n"; if( $value[1] == self::COND_IN ) // Si condition de type IN
$requestS .= SQLBuilder::IN([$this->schema['table'], $field], $value[0], $c, $binded)."\n";
else // Sinon
$requestS .= SQLBuilder::WHERE([$this->schema['table'], $field], $value, $c, $binded)."\n";
$c++; $c++;
} }
/* (2) On ajoute les jointures */ /* (2) On ajoute les jointures */
foreach($this->joined as $localField=>$rows){ foreach($this->joined as $localField=>$rows){
if( $c == 0 ) $requestS .= 'WHERE '; if( $c == 0 ) $requestS .= 'WHERE ';
else $requestS .= 'AND '; else $requestS .= 'AND ';
// {1} Clause SELECT interne // // {1} Clause SELECT interne //
$requestS .= $this->schema['table'].'.'.$localField." = (\n\tSELECT "; $requestS .= $this->schema['table'].'.'.$localField." = (\n\t";
// Jointure du SELECT, champ joint lié au champ local // Jointure du SELECT, champ joint lié au champ local
$requestS .= $rows->schema['table'].'.'.$this->schema['columns'][$localField]['references'][1]."\n"; $requestS .= SQLBuilder::SELECT([
$rows->schema['table'] => [ $this->schema['columns'][$localField]['references'][1] ]
])."\n";
// {2} Clause FROM interne // // {2} Clause FROM interne //
$requestS .= "\tFROM ".$this->schema['columns'][$localField]['references'][0]."\n"; $requestS .= "\t".SQLBuilder::FROM([$this->schema['columns'][$localField]['references'][0]])."\n";
// {3} Clause WHERE/AND interne // // {3} Clause WHERE/AND interne //
$c2 = 0; $c2 = 0;
foreach($rows->where as $field=>$conditions) foreach($rows->where as $field=>$conditions)
foreach($conditions as $cdt=>$value){ foreach($conditions as $cdt=>$value){
if( $c2 == 0 ) $requestS .= "\tWHERE ".$rows->schema['table'].'.'.$field.' '.substr($value[1], 2, -2).' :'.$rows->schema['table'].'_x_'.$field.'_'.$cdt."\n"; if( $value[1] == self::COND_IN ) // Si condition de type IN
else $requestS .= "\tAND ".$rows->schema['table'].'.'.$field.' '.substr($value[1], 2, -2).' :'.$rows->schema['table'].'_x_'.$field.'_'.$cdt."\n"; $requestS .= "\t".SQLBuilder::IN([$rows->schema['table'], $field], $value[0], $c2, $binded)."\n";
else // Sinon
$requestS .= "\t".SQLBuilder::WHERE([$rows->schema['table'], $field], $value, $c2, $binded)."\n";
$c2++; $c2++;
} }
$requestS .= "\tLIMIT 1)\n"; $requestS .= "\tLIMIT 1)\n";
} }
/* [3] On prépare la requête
/* [3] On exécute la requête et 'bind' les paramètres
=========================================================*/ =========================================================*/
/* (0) On prépare la requête */ /* (0) On prépare la requête */
$request = Database::getPDO()->prepare($requestS.';'); $request = Database::getPDO()->prepare($requestS.';');
$binded = [];
/* (1) On bind tous les paramètres locaux */
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=>$conditions)
foreach($conditions as $cdt=>$value)
$binded[':'.$rows->schema['table'].'_x_'.$field.'_'.$cdt] = $value[0];
/* [4] On exécute la requête et retourne le résultat /* [4] On exécute la requête et retourne le résultat

View File

@ -20,8 +20,16 @@
public static function getAll($id_warehouse){ public static function getAll($id_warehouse){
/* [1] On récupère les modules de l'entrepot courant /* [1] On récupère les modules de l'entrepot courant
=========================================================*/ =========================================================*/
$module = Table::get('module'); $module = Table::get('module')
// ->where ->whereName([ $_SESSION['WAREHOUSE']['modules'], Rows::COND_IN ]);
$chip = Table::get('chip')
->select('*');
$module_merge = Table::get('module_merge')
->join('id_module', $module);
var_dump($module_merge->fetch());
/* [2] On retourne le résultat /* [2] On retourne le résultat

View File

@ -345,24 +345,24 @@
/* [1] FETCH /* [1] FETCH
=========================================================*/ =========================================================*/
$warehouse = // $warehouse =
Table::get('warehouse') // Access to table 'warehouse' // Table::get('warehouse') // Access to table 'warehouse'
->whereName(['stef-montauban', Rows::COND_EQUAL]); // condition : name = 'my-warehouse' // ->whereName(['stef-montauban', Rows::COND_EQUAL]); // condition : name = 'my-warehouse'
//
//
$myUser = // $myUser =
Table::get('user') // Access to table 'user' // Table::get('user') // Access to table 'user'
->whereId([100, Rows::COND_INF]) // PRIMARY KEY (other condition on same field) // ->whereId([100, Rows::COND_INF]) // PRIMARY KEY (other condition on same field)
->whereId([[1,4,6,9], Rows::COND_IN]) // PRIMARY KEY (other condition on same field) // ->whereId([[1,4,6,9], Rows::COND_IN]) // PRIMARY KEY (other condition on same field)
->whereUsername(['%e%', Rows::COND_LIKE]) // Dynamic getter 'getByMySuperColumn' -> 'my_super_column' // ->whereUsername(['%e%', 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'
// ->unique() // unique result // // ->unique() // unique result
//
// SELECT // // SELECT
->fetch(); // Result // ->fetch(); // Result
var_dump($myUser); // var_dump($myUser);
/* [2] Modification /* [2] Modification
=========================================================*/ =========================================================*/
@ -374,6 +374,7 @@
// $myUser = // $myUser =
// Table::get('user') // Access to table 'user' // Table::get('user') // Access to table 'user'
// ->whereId([100, Rows::COND_INF]) // PRIMARY KEY (other condition on same field) // ->whereId([100, Rows::COND_INF]) // PRIMARY KEY (other condition on same field)
// ->whereId([[1,4,6,9], Rows::COND_IN]) // PRIMARY KEY (other condition on same field)
// ->whereUsername(['jo%', Rows::COND_LIKE]) // Dynamic getter 'getByMySuperColumn' -> 'my_super_column' // ->whereUsername(['jo%', 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)
@ -420,13 +421,26 @@
// $myUser = // $myUser =
// Table::get('user') // Access to table 'user' // Table::get('user') // Access to table 'user'
// ->whereId([100, Rows::COND_INF]) // PRIMARY KEY (other condition on same field) // ->whereId([100, Rows::COND_INF]) // PRIMARY KEY (other condition on same field)
// ->whereUsername(['no%', Rows::COND_LIKE]) // Dynamic getter 'getByMySuperColumn' -> 'my_super_column' // ->whereId([[1,4,6,9], Rows::COND_IN]) // PRIMARY KEY (other condition on same field)
// ->whereUsername(['n%', 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'
// // ->unique() // unique result // // ->unique() // unique result
// //
// ->delete(); // ->delete();
//
// $module = Table::get('module')
// ->whereName([ $_SESSION['WAREHOUSE']['modules'], Rows::COND_IN ]);
//
// $chip = Table::get('chip')
// ->select('*');
//
// $module_merge = Table::get('module_merge')
// ->join('id_module', $module)
// ->join('id_chip', $chip);
//
// var_dump($module_merge->fetch());
?> ?>