ORM: Gestion de l'INSERTion multiple
This commit is contained in:
parent
ace1211fd0
commit
b983560744
|
@ -487,6 +487,8 @@
|
|||
/* AJOUTE UNE ENTREE DANS LA TABLE
|
||||
*
|
||||
* @entry<Array> Tableau associatif de la forme (colonne => valeur)
|
||||
* OU
|
||||
* @entries<Array> Tableau de la forme ([entry1, entry2])
|
||||
*
|
||||
* @return status<Boolean> Retourne si TRUE ou FALSE les entrées ont bien été supprimées
|
||||
*
|
||||
|
@ -494,36 +496,52 @@
|
|||
public function insert($entry){
|
||||
/* [0] On vérifie les paramètres
|
||||
=========================================================*/
|
||||
/* (1) Si c'est pas un tableau, erreur */
|
||||
if( !is_array($entry) )
|
||||
/* (1) Si c'est pas un tableau avec au moins une entrée, erreur */
|
||||
if( !is_array($entry) || count($entry) == 0 )
|
||||
return false;
|
||||
|
||||
// S'il n'y a qu'une entrée, on met au même format que s'il y en avait plusieurs
|
||||
$firstIndex = array_keys($entry)[0];
|
||||
if( !is_array($entry[$firstIndex]) )
|
||||
$entry = [ $entry ];
|
||||
|
||||
/* (2) On retire les champ inconnus */
|
||||
$cleared = [];
|
||||
|
||||
// Pour chaque entrée du tableau
|
||||
foreach($entry as $field=>$value)
|
||||
foreach($entry as $i=>$set){
|
||||
$cleared[$i] = [];
|
||||
|
||||
foreach($set as $field=>$value){
|
||||
|
||||
if( isset($this->schema['columns'][$field]) ) // Champ existe
|
||||
$cleared[$field] = $value;
|
||||
$cleared[$i][$field] = $value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* (3) On vérifie les types des champs */
|
||||
foreach($cleared as $field=>$value){
|
||||
foreach($cleared as $i=>$set){
|
||||
|
||||
foreach($set as $field=>$value){
|
||||
|
||||
$type = $this->schema['columns'][$field]['type'];
|
||||
|
||||
// {1} Si de type INT/FLOAT et pas numérique, on retire le champ //
|
||||
if( in_array($type, ['int', 'float']) && !is_numeric($value) && $value != self::DEFAULT )
|
||||
unset($cleared[$field]);
|
||||
unset($cleared[$i][$field]);
|
||||
|
||||
// {2} Si de type TEXT/VARCHAR et pas string, on retire le champ //
|
||||
if( in_array($type, ['text', 'varchar']) && !is_string($value) && $value != self::DEFAULT )
|
||||
unset($cleared[$field]);
|
||||
unset($cleared[$i][$field]);
|
||||
}
|
||||
|
||||
/* (4) Si il manque des données, erreur */
|
||||
if( count($cleared) != count($this->schema['columns']) )
|
||||
if( count($cleared[$i]) != count($this->schema['columns']) )
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* [1] On crée la requête
|
||||
=========================================================*/
|
||||
|
@ -532,7 +550,7 @@
|
|||
|
||||
/* (2) Clause : table(col1, col2, ...) */
|
||||
$c = 0;
|
||||
foreach($cleared as $field=>$value){
|
||||
foreach($this->schema['columns'] as $field=>$value){
|
||||
if( $c > 0 ) $requestS .= ', ';
|
||||
$requestS .= $field;
|
||||
|
||||
|
@ -542,21 +560,32 @@
|
|||
// Fin de clause
|
||||
$requestS .= ")\n";
|
||||
|
||||
|
||||
/* (3) Clause : VALUES(val1, val2, ...) */
|
||||
$requestS .= 'VALUES(';
|
||||
$v = 0;
|
||||
foreach($cleared as $i=>$set){
|
||||
if( $v == 0 ) $requestS .= 'VALUES(';
|
||||
else $requestS .= ",\n\t(";
|
||||
|
||||
$c = 0;
|
||||
foreach($cleared as $field=>$value){
|
||||
foreach($this->schema['columns'] as $field=>$column){
|
||||
if( $c > 0 ) $requestS .= ', ';
|
||||
|
||||
if( $value == self::DEFAULT) $requestS .= 'DEFAULT'; // On insère directement les valeurs 'DEFAULT'
|
||||
else $requestS .= ':insert_'.$field;
|
||||
// Si l'entrée est donnée
|
||||
if( isset($set[$field]) )
|
||||
if( $set[$field] == self::DEFAULT ) $requestS .= 'DEFAULT'; // On insère directement les valeurs 'DEFAULT'
|
||||
else $requestS .= ':insert_'.$field.'_'.$i;
|
||||
else
|
||||
$requestS .= 'DEFAULT';
|
||||
|
||||
$c++;
|
||||
}
|
||||
|
||||
// Fin de clause
|
||||
$requestS .= ")";
|
||||
$v++;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -567,10 +596,10 @@
|
|||
$binded = [];
|
||||
|
||||
/* (1) On bind les paramètres */
|
||||
foreach($cleared as $field=>$value)
|
||||
if( $value != self::DEFAULT )
|
||||
$binded[':insert_'.$field] = $value;
|
||||
|
||||
foreach($cleared as $i=>$set)
|
||||
foreach($this->schema['columns'] as $field=>$column)
|
||||
if( isset($set[$field]) && $set[$field] != self::DEFAULT )
|
||||
$binded[':insert_'.$field.'_'.$i] = $set[$field];
|
||||
|
||||
|
||||
/* [3] On exécute la requête et envoie le status
|
||||
|
|
|
@ -398,13 +398,24 @@
|
|||
// With ORM
|
||||
$insert = Table::get('user')
|
||||
->insert([
|
||||
[
|
||||
'id_user' => Rows::DEFAULT,
|
||||
'id_warehouse' => $id_warehouse,
|
||||
'code' => $code,
|
||||
'username' => $username,
|
||||
'firstname' => $firstname,
|
||||
'lastname' => $lastname,
|
||||
'mail' => $mail,
|
||||
'id_warehouse' => 7,
|
||||
'code' => 'AA-AA-AA-AA',
|
||||
'username' => 'AA',
|
||||
'firstname' => 'AA',
|
||||
'lastname' => 'AA',
|
||||
'mail' => 'AA@AA.AA'
|
||||
],
|
||||
[
|
||||
'id_user' => Rows::DEFAULT,
|
||||
'id_warehouse' => 7,
|
||||
'code' => 'BB-BB-BB-BB',
|
||||
'username' => 'BB',
|
||||
'firstname' => 'BB',
|
||||
'lastname' => 'BB',
|
||||
'mail' => 'BB@BB.BB'
|
||||
]
|
||||
]);
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue