ORM: Edit avec condition 'IN'
This commit is contained in:
parent
4b25bbe008
commit
bc95bad2b3
|
@ -377,21 +377,18 @@
|
||||||
/* [1] Rédaction de la clause UPDATE
|
/* [1] Rédaction de la clause UPDATE
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (1) On initialise la requête */
|
/* (1) On initialise la requête */
|
||||||
$requestS = "UPDATE ".$this->schema['table']."\n";
|
$requestS = SQLBuilder::UPDATE($this->schema['table'])."\n";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* [2] Rédaction de la clause SET
|
/* [2] Rédaction de la clause SET
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (5) On met tout les champs à modifier */
|
/* (0) On initialise les variables à "binder" */
|
||||||
$c = 0;
|
$binded = [];
|
||||||
foreach($cleared as $field=>$value){
|
|
||||||
if( $c == 0 ) $requestS .= 'SET '.$field.' = :update_'.$field;
|
|
||||||
else $requestS .= "\n, ".$field.' = :update_'.$field;
|
|
||||||
$c++;
|
|
||||||
}
|
|
||||||
|
|
||||||
$requestS .= "\n";
|
/* (1) On met tout les champs à modifier */
|
||||||
|
$requestS .= SQLBuilder::SET($cleared, $binded)."\n";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -401,8 +398,11 @@
|
||||||
$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++;
|
||||||
}
|
}
|
||||||
|
@ -413,20 +413,26 @@
|
||||||
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++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -434,28 +440,13 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* [4] On exécute la requête et 'bind' les paramètres
|
/* [4] On prépare la requête
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (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 */
|
/* [5] On exécute la requête et retourne le résultat
|
||||||
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 bind les nouvelles valeurs */
|
|
||||||
foreach($cleared as $field=>$value)
|
|
||||||
$binded[':update_'.$field] = $value;
|
|
||||||
|
|
||||||
/* [6] On exécute la requête et retourne le résultat
|
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (1) On exécute la requête */
|
/* (1) On exécute la requête */
|
||||||
$updated = $request->execute($binded);
|
$updated = $request->execute($binded);
|
||||||
|
|
|
@ -72,6 +72,30 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* CONSTRUIT LA REQUETE TEXTUELLE "UPDATE" AVEC LA TABLE EN QUESTION
|
||||||
|
*
|
||||||
|
* @table<String> Table en question
|
||||||
|
*
|
||||||
|
* @return sql<String> Renvoie la clause remplie
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static function UPDATE($table){
|
||||||
|
/* [0] Initialisation
|
||||||
|
=========================================================*/
|
||||||
|
$sql = 'UPDATE ';
|
||||||
|
|
||||||
|
/* [1] On construit la requête
|
||||||
|
=========================================================*/
|
||||||
|
$sql .= $table;
|
||||||
|
|
||||||
|
return $sql;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* CONSTRUIT LA REQUETE TEXTUELLE "IN" AVEC UNE LISTE DE TABLES
|
/* CONSTRUIT LA REQUETE TEXTUELLE "IN" AVEC UNE LISTE DE TABLES
|
||||||
*
|
*
|
||||||
* @field<Array> Tableau contenant [table, field]
|
* @field<Array> Tableau contenant [table, field]
|
||||||
|
@ -146,6 +170,43 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* CONSTRUIT LA REQUETE TEXTUELLE "SET" AVEC UNE LISTE DE TABLES
|
||||||
|
*
|
||||||
|
* @values<Array> Tableau de la forme [ field=>value, field2=>value2 ]
|
||||||
|
* @binded<Arary> Tableau associatif contenant les variables "bindés" -> ajout des champs
|
||||||
|
*
|
||||||
|
* @return sql<String> Renvoie la clause remplie
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static function SET($values, &$binded){
|
||||||
|
/* [0] Initialisation
|
||||||
|
=========================================================*/
|
||||||
|
$sql = 'SET ';
|
||||||
|
|
||||||
|
|
||||||
|
/* [1] On construit la requête
|
||||||
|
=========================================================*/
|
||||||
|
$c = 0;
|
||||||
|
foreach($values as $field=>$value){
|
||||||
|
/* (1) Champ */
|
||||||
|
if( $c > 0 ) $sql .= "\n, ";
|
||||||
|
$sql .= $field.' = ';
|
||||||
|
|
||||||
|
/* (2) Variable */
|
||||||
|
$sql .= ':update_'.$field;
|
||||||
|
|
||||||
|
$binded[':update_'.$field] = $value;
|
||||||
|
|
||||||
|
$c++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $sql;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -372,18 +372,21 @@
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// $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(['jo%', 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
|
||||||
|
//
|
||||||
|
// // ->fetch();
|
||||||
|
// ->edit([
|
||||||
|
// 'id_warehouse' => Table::get('warehouse')->whereName(['stef-montauban', Rows::COND_EQUAL])->unique()->select('id_warehouse')->fetch()['id_warehouse']
|
||||||
|
// ]);
|
||||||
|
|
||||||
// ->edit([
|
// var_dump($myUser);
|
||||||
// 'id_warehouse' => Table::get('warehouse')->whereName('stef-montauban')->unique()->select('id_warehouse')->fetch()['id_warehouse']
|
|
||||||
// ]);
|
|
||||||
|
|
||||||
|
|
||||||
/* [3] INSERTION
|
/* [3] INSERTION
|
||||||
|
|
Loading…
Reference in New Issue