ORM: Suppression sur le même principe de jointure que la modification avec 'edit'

This commit is contained in:
xdrm-brackets 2016-07-23 15:53:51 +02:00
parent bb7fd1f2dd
commit 5fd130287a
2 changed files with 75 additions and 120 deletions

View File

@ -619,62 +619,13 @@
*
*/
public function delete(){
/* [0] Vérification des paramètres
=========================================================*/
/* (1) Si c'est pas un tableau, erreur */
if( !is_array($updates) )
return false;
/* (2) On retire les champ inconnus / clés primaires */
$cleared = [];
// Pour chaque entrée du tableau
foreach($updates as $field=>$value)
if( isset($this->schema['columns'][$field]) && !$this->schema['columns'][$field]['primary'] ) // Champ existe et n'est pas clé primaire
$cleared[$field] = $value;
/* (3) On vérifie les types des champs */
foreach($cleared 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) )
unset($cleared[$field]);
// {2} Si de type TEXT/VARCHAR et pas string, on retire le champ //
if( in_array($type, ['text', 'varchar']) && !is_string($value) )
unset($cleared[$field]);
}
/* (4) Si on a plus de champ, on retourne l'object courant */
if( count($cleared) == 0 )
return false;
/* [1] Rédaction de la clause UPDATE
/* [1] Rédaction de la clause DELETE FROM
=========================================================*/
/* (1) On initialise la requête */
$requestS = "UPDATE ".$this->schema['table']."\n";
$requestS = "DELETE FROM ".$this->schema['table']."\n";
/* [2] Rédaction de la clause SET
=========================================================*/
/* (5) On met tout les champs à modifier */
$c = 0;
foreach($cleared as $field=>$value){
if( $c == 0 ) $requestS .= 'SET '.$field.' = :update_'.$field;
else $requestS .= "\n, ".$field.' = :update_'.$field;
$c++;
}
$requestS .= "\n";
/* [3] On rédige la clause WHERE/AND
/* [2] On rédige la clause WHERE/AND
=========================================================*/
/* (1) On met les conditions locales */
$c = 0;
@ -713,7 +664,7 @@
}
/* [4] On exécute la requête et 'bind' les paramètres
/* [3] On exécute la requête et 'bind' les paramètres
=========================================================*/
/* (0) On prépare la requête */
$request = Database::getPDO()->prepare($requestS.';');
@ -730,17 +681,14 @@
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
/* [4] On exécute la requête et retourne le résultat
=========================================================*/
/* (1) On exécute la requête */
$updated = $request->execute($binded);
$deleted = $request->execute($binded);
/* (2) On retourne l'état de la requête */
return $updated;
return $deleted;
}

View File

@ -342,6 +342,8 @@
debug();
/* [1] FETCH
=========================================================*/
// $warehouse =
// Table::get('warehouse') // Access to table 'warehouse'
// ->whereName(['stef-montauban', Rows::COND_NOTEQ]); // condition : name = 'my-warehouse'
@ -356,69 +358,74 @@
// ->join('id_warehouse', $warehouse) // joins warehouse (with name 'my-warehouse') to column 'id_warehouse'
// ->unique() // unique result
// SELECT
// ->fetch(); // Result
// UPDATE
/* [2] Modification
=========================================================*/
// $warehouse =
// Table::get('warehouse') // Access to table 'warehouse'
// ->whereName(['stef-montauban', Rows::COND_NOTEQ]); // condition : name = 'my-warehouse'
//
//
// $myUser =
// Table::get('user') // Access to table 'user'
// ->whereId([100, Rows::COND_INF]) // PRIMARY KEY (other condition on same field)
// ->whereUsername(['jo%', 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
// ->edit([
// 'id_warehouse' => Table::get('warehouse')->whereName('stef-montauban')->unique()->select('id_warehouse')->fetch()['id_warehouse']
// ]);
$id_user = 10;
$id_warehouse = 7;
$code = '13-34-56-67';
$username = '13';
$firstname = '12';
$lastname = '12';
$mail = '12@ds.com';
// $edit_user = Database::getPDO()->prepare("UPDATE user
// SET code = :code,
// username = :username,
// firstname = :firstname,
// lastname = :lastname,
// mail = :mail
// WHERE id_user = :id_user
// AND id_warehouse = :id_warehouse");
// $edit_user->execute([
// ':code' => $code,
// ':username' => $username,
// ':firstname' => $firstname,
// ':lastname' => $lastname,
// ':mail' => $mail,
// ':id_user' => $id_user,
// ':id_warehouse' => $id_warehouse
/* [3] INSERTION
=========================================================*/
// $insert = Table::get('user')
// ->insert([
// [
// 'id_user' => Rows::DEFAULT,
// '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'
// ]
// ]);
// With ORM
$insert = Table::get('user')
->insert([
[
'id_user' => Rows::DEFAULT,
'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'
]
]);
/* [4] Suppression
=========================================================*/
$warehouse =
Table::get('warehouse') // Access to table 'warehouse'
->whereName('stef-montauban'); // condition : name = 'my-warehouse'
var_dump($insert);
$myUser =
Table::get('user') // Access to table 'user'
->whereId([100, Rows::COND_INF]) // PRIMARY KEY (other condition on same field)
->whereUsername(['no%', 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
->fetch();
var_dump($myUser);
?>