ORM: Refactor de la méthode 'edit' forme différente de 'fetch' mais normalement même résultat

This commit is contained in:
xdrm-brackets 2016-07-24 18:11:46 +02:00
parent c503bf02f3
commit 780a3e73e7
3 changed files with 84 additions and 57 deletions

View File

@ -416,76 +416,84 @@
return false; return false;
/* [1] Rédaction de la clause UPDATE
/* [1] Initialisation des paramètres
=========================================================*/ =========================================================*/
/* (1) On initialise la requête */ /* (1) On initialise la requête */
$requestS = SQLBuilder::UPDATE($this->schema['table'])."\n"; $requestS = [];
/* (2) On initialise les paramètres */
/* [2] Rédaction de la clause SET
=========================================================*/
/* (0) On initialise les variables à "binder" */
$binded = []; $binded = [];
/* [2] Rédaction de la clause UPDATE
=========================================================*/
$requestS['UPDATE'] = SQLBuilder::UPDATE($this->schema['table']);
/* [3] Rédaction de la clause SET
=========================================================*/
/* (1) On met tout les champs à modifier */ /* (1) On met tout les champs à modifier */
$requestS .= SQLBuilder::SET($cleared, $binded)."\n"; $requestS['SET'] = SQLBuilder::SET($cleared, $binded);
/* [4] On rédige la clause WHERE/AND
/* [3] On rédige la clause WHERE/AND
=========================================================*/ =========================================================*/
/* (1) On met les conditions locales */ /* (1) On met les conditions locales */
$requestS['WHERE'] = [];
$c = 0; $c = 0;
foreach($this->where as $field=>$conditions) foreach($this->where as $field=>$conditions)
foreach($conditions as $cdt=>$value){ foreach($conditions as $cdt=>$value){
if( $value[1] == self::COND_IN ) // Si condition de type IN if( $value[1] == self::COND_IN ) // Si condition de type IN
$requestS .= SQLBuilder::IN([$this->schema['table'], $field], $value[0], $c, $binded)."\n"; $requestS['WHERE'][$c] = SQLBuilder::IN([$this->schema['table'], $field], $value[0], $c, $binded);
else // Sinon else // Sinon
$requestS .= SQLBuilder::WHERE([$this->schema['table'], $field], $value, $c, $binded)."\n"; $requestS['WHERE'][$c] = SQLBuilder::WHERE([$this->schema['table'], $field], $value, $c, $binded);
$c++; $c++;
} }
/* (2) On ajoute les jointures */ /* (2) On ajoute les jointures */
foreach($this->joined as $localField=>$rows){ // Note: On ajoute les requêtes des tables de jointures dans la clause WHERE //
if( $c == 0 ) $requestS .= 'WHERE '; foreach($this->joined as $field=>$data){
else $requestS .= 'AND '; // {1} On récupère la requête/les params de chaque jointure //
$joinedFetched = $data['object']->fetch(false);
// {1} On inclut la requête de la jointure // // {2} On met la clé étrangère pour la clause SELECT //
$requestS .= $this->schema['table'].'.'.$localField." in (\n\t"; $joinedFetched['request']['SELECT'] = [ $data['object']->schema['table'].'.'.$data['field'] ];
// {2} On récupère la requête // // {3} On construit la nouvelle requête //
$RowsFetched = $rows[0]->fetch(false); $joinedRequest = SQLBuilder::BUILD($joinedFetched['request']);
// {3} On ajoute la requête // // {4} On supprime les retours à la ligne //
$requestS .= implode("\n\t", explode("\n", $RowsFetched['request'])); $joinedRequest = str_replace("\n", " ", $joinedRequest);
// {4} On bind les params de la requête // // {5} On l'ajoute à la clause FROM avec comme alias le nom de la table de @data['object'] //
$binded = array_merge($binded, $RowsFetched['binded']); $requestS['WHERE'][] = $this->schema['table'].".$field in ($joinedRequest)";
// {5} On précise qu'on veut un seul résultat // // {6} On ajoute les variables à la requête courante //
$requestS .= ")\n"; $binded = array_merge($binded, $joinedFetched['binded']);
$c++;
} }
/* [4] Clause LIMIT /* [5] Clause LIMIT
=========================================================*/ =========================================================*/
if( $this->unique ) $requestS['LIMIT'] = ($this->unique) ? SQLBuilder::LIMIT(1) : SQLBuilder::LIMIT([]);
$requestS .= "LIMIT 1";
/* [5] On prépare la requête /* [6] On prépare et compose la requête
=========================================================*/ =========================================================*/
/* (0) On prépare la requête */ /* (1) On compose la requête */
$request = Database::getPDO()->prepare($requestS.';'); $requestString = SQLBuilder::BUILD($requestS).';';
/* (2) On prépare la requête */
$request = Database::getPDO()->prepare($requestString);
/* [6] On exécute la requête et retourne le résultat
/* [7] 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); $updated = $request->execute($binded);
@ -650,6 +658,7 @@
/* [2] On rédige la clause WHERE/AND /* [2] On rédige la clause WHERE/AND
=========================================================*/ =========================================================*/
/* (1) On met les conditions locales */ /* (1) On met les conditions locales */
$requestS['WHERE'] = [];
$c = 0; $c = 0;
foreach($this->where as $field=>$conditions) foreach($this->where as $field=>$conditions)
foreach($conditions as $cdt=>$value){ foreach($conditions as $cdt=>$value){

View File

@ -182,9 +182,7 @@
$c = 0; $c = 0;
foreach($values as $field=>$value){ foreach($values as $field=>$value){
/* (1) Champ */ /* (1) Champ */
$sql[$c] = ($c>0) ? "\n, " : ''; $sql[$c] = $field.' = ';
$sql[$c] .= $field.' = ';
/* (2) Variable */ /* (2) Variable */
$sql[$c] .= ':update_'.$field; $sql[$c] .= ':update_'.$field;
@ -308,6 +306,26 @@
case 'DELETE': case 'DELETE':
$sql .= "DELETE FROM $statements\n"; $sql .= "DELETE FROM $statements\n";
break; break;
/* (6) Clause UPDATE
---------------------------------------------------------*/
case 'UPDATE':
$sql .= "UPDATE $statements\n";
break;
break;
/* (7) Clause SET
---------------------------------------------------------*/
case 'SET':
$c = 0;
foreach($statements as $field){
$sql .= ($c>0) ? "\n, $field" : "SET $field";
$c++;
}
$sql .= "\n";
break;
} }
@ -319,7 +337,7 @@
/* [5] On retourne le résultat /* [2] On retourne le résultat
=========================================================*/ =========================================================*/
return $sql; return $sql;
} }

View File

@ -345,25 +345,25 @@
/* [1] FETCH /* [1] FETCH
=========================================================*/ =========================================================*/
$warehouse = // $warehouse =
Table::get('warehouse') // Access to table 'warehouse' // Table::get('warehouse') // Access to table 'warehouse'
->whereName(['stef-montauban', Rows::COND_EQUAL]) // condition : name = 'my-warehouse' // ->whereName(['stef-montauban', Rows::COND_EQUAL]) // condition : name = 'my-warehouse'
->select('name'); // ->select('name');
//
//
$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)
->whereId([[92, 93], Rows::COND_IN]) // PRIMARY KEY (other condition on same field) // ->whereId([[94, 95], Rows::COND_IN]) // PRIMARY KEY (other condition on same field)
->whereUsername(['%', Rows::COND_LIKE]) // Dynamic getter 'getByMySuperColumn' -> 'my_super_column' // ->whereUsername(['%', 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 // // SELECT
->delete(); // Result // // ->fetch();
var_dump($myUser); // var_dump($myUser);
/* [2] Modification /* [2] Modification
=========================================================*/ =========================================================*/