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(){ public function delete(){
/* [0] Vérification des paramètres /* [1] Rédaction de la clause DELETE FROM
=========================================================*/
/* (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) On initialise la requête */ /* (1) On initialise la requête */
$requestS = "UPDATE ".$this->schema['table']."\n"; $requestS = "DELETE FROM ".$this->schema['table']."\n";
/* [2] On rédige la clause WHERE/AND
/* [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
=========================================================*/ =========================================================*/
/* (1) On met les conditions locales */ /* (1) On met les conditions locales */
$c = 0; $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 */ /* (0) On prépare la requête */
$request = Database::getPDO()->prepare($requestS.';'); $request = Database::getPDO()->prepare($requestS.';');
@ -730,17 +681,14 @@
foreach($conditions as $cdt=>$value) foreach($conditions as $cdt=>$value)
$binded[':'.$rows->schema['table'].'_x_'.$field.'_'.$cdt] = $value[0]; $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 */ /* (1) On exécute la requête */
$updated = $request->execute($binded); $deleted = $request->execute($binded);
/* (2) On retourne l'état de la requête */ /* (2) On retourne l'état de la requête */
return $updated; return $deleted;
} }

View File

@ -342,83 +342,90 @@
debug(); debug();
/* [1] FETCH
=========================================================*/
// $warehouse = // $warehouse =
// Table::get('warehouse') // Access to table 'warehouse' // Table::get('warehouse') // Access to table 'warehouse'
// ->whereName(['stef-montauban', Rows::COND_NOTEQ]); // condition : name = 'my-warehouse' // ->whereName(['stef-montauban', Rows::COND_NOTEQ]); // 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)
// ->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)
// ->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
// ->fetch(); // Result
// SELECT /* [2] Modification
// ->fetch(); // Result =========================================================*/
// $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
// UPDATE // ->edit([
// ->edit([ // 'id_warehouse' => Table::get('warehouse')->whereName('stef-montauban')->unique()->select('id_warehouse')->fetch()['id_warehouse']
// 'id_warehouse' => Table::get('warehouse')->whereName('stef-montauban')->unique()->select('id_warehouse')->fetch()['id_warehouse'] // ]);
// ]);
$id_user = 10; /* [3] INSERTION
$id_warehouse = 7; =========================================================*/
$code = '13-34-56-67'; // $insert = Table::get('user')
$username = '13'; // ->insert([
$firstname = '12'; // [
$lastname = '12'; // 'id_user' => Rows::DEFAULT,
$mail = '12@ds.com'; // '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'
// ]
// ]);
// $edit_user = Database::getPDO()->prepare("UPDATE user /* [4] Suppression
// SET code = :code, =========================================================*/
// username = :username, $warehouse =
// firstname = :firstname, Table::get('warehouse') // Access to table 'warehouse'
// lastname = :lastname, ->whereName('stef-montauban'); // condition : name = 'my-warehouse'
// 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
// ]);
$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
// With ORM ->fetch();
$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'
]
]);
var_dump($myUser);
var_dump($insert);
?> ?>