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 b983560744
commit 969f814da0
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,6 +342,8 @@
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'
@ -356,69 +358,74 @@
// ->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
// 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([ // ->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',
// $edit_user = Database::getPDO()->prepare("UPDATE user // 'firstname' => 'AA',
// SET code = :code, // 'lastname' => 'AA',
// username = :username, // 'mail' => 'AA@AA.AA'
// firstname = :firstname, // ],
// lastname = :lastname, // [
// mail = :mail // 'id_user' => Rows::DEFAULT,
// WHERE id_user = :id_user // 'id_warehouse' => 7,
// AND id_warehouse = :id_warehouse"); // 'code' => 'BB-BB-BB-BB',
// $edit_user->execute([ // 'username' => 'BB',
// ':code' => $code, // 'firstname' => 'BB',
// ':username' => $username, // 'lastname' => 'BB',
// ':firstname' => $firstname, // 'mail' => 'BB@BB.BB'
// ':lastname' => $lastname, // ]
// ':mail' => $mail,
// ':id_user' => $id_user,
// ':id_warehouse' => $id_warehouse
// ]); // ]);
/* [4] Suppression
// With ORM =========================================================*/
$insert = Table::get('user') $warehouse =
->insert([ Table::get('warehouse') // Access to table 'warehouse'
[ ->whereName('stef-montauban'); // condition : name = 'my-warehouse'
'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($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);
?> ?>