From 5fd130287af3fa460a37f1f881c088050479f7e7 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 23 Jul 2016 15:53:51 +0200 Subject: [PATCH] =?UTF-8?q?ORM:=20Suppression=20sur=20le=20m=C3=AAme=20pri?= =?UTF-8?q?ncipe=20de=20jointure=20que=20la=20modification=20avec=20'edit'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manager/ORM/Rows.php | 66 +++------------------- test/automate.php | 129 +++++++++++++++++++++++-------------------- 2 files changed, 75 insertions(+), 120 deletions(-) diff --git a/manager/ORM/Rows.php b/manager/ORM/Rows.php index 45f4a99..3b4056b 100644 --- a/manager/ORM/Rows.php +++ b/manager/ORM/Rows.php @@ -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; } diff --git a/test/automate.php b/test/automate.php index 152ead2..835a7b0 100755 --- a/test/automate.php +++ b/test/automate.php @@ -342,83 +342,90 @@ debug(); + /* [1] FETCH + =========================================================*/ // $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 + // 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 + + // SELECT + // ->fetch(); // Result - // SELECT - // ->fetch(); // Result + /* [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 - // UPDATE - // ->edit([ - // 'id_warehouse' => Table::get('warehouse')->whereName('stef-montauban')->unique()->select('id_warehouse')->fetch()['id_warehouse'] - // ]); + // ->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'; + /* [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' + // ] + // ]); - // $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 - // ]); + /* [4] Suppression + =========================================================*/ + $warehouse = + Table::get('warehouse') // Access to table 'warehouse' + ->whereName('stef-montauban'); // 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(['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 - $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' - ] - ]); + ->fetch(); - - var_dump($insert); + var_dump($myUser); ?>