ORM: Refactor de la méthode 'edit' forme différente de 'fetch' mais normalement même résultat
This commit is contained in:
parent
c503bf02f3
commit
780a3e73e7
|
@ -416,76 +416,84 @@
|
|||
return false;
|
||||
|
||||
|
||||
/* [1] Rédaction de la clause UPDATE
|
||||
|
||||
|
||||
/* [1] Initialisation des paramètres
|
||||
=========================================================*/
|
||||
/* (1) On initialise la requête */
|
||||
$requestS = SQLBuilder::UPDATE($this->schema['table'])."\n";
|
||||
$requestS = [];
|
||||
|
||||
|
||||
|
||||
|
||||
/* [2] Rédaction de la clause SET
|
||||
=========================================================*/
|
||||
/* (0) On initialise les variables à "binder" */
|
||||
/* (2) On initialise les paramètres */
|
||||
$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 */
|
||||
$requestS .= SQLBuilder::SET($cleared, $binded)."\n";
|
||||
$requestS['SET'] = SQLBuilder::SET($cleared, $binded);
|
||||
|
||||
|
||||
|
||||
/* [3] On rédige la clause WHERE/AND
|
||||
/* [4] On rédige la clause WHERE/AND
|
||||
=========================================================*/
|
||||
/* (1) On met les conditions locales */
|
||||
$requestS['WHERE'] = [];
|
||||
$c = 0;
|
||||
foreach($this->where as $field=>$conditions)
|
||||
foreach($conditions as $cdt=>$value){
|
||||
|
||||
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
|
||||
$requestS .= SQLBuilder::WHERE([$this->schema['table'], $field], $value, $c, $binded)."\n";
|
||||
$requestS['WHERE'][$c] = SQLBuilder::WHERE([$this->schema['table'], $field], $value, $c, $binded);
|
||||
|
||||
$c++;
|
||||
}
|
||||
|
||||
/* (2) On ajoute les jointures */
|
||||
foreach($this->joined as $localField=>$rows){
|
||||
if( $c == 0 ) $requestS .= 'WHERE ';
|
||||
else $requestS .= 'AND ';
|
||||
// Note: On ajoute les requêtes des tables de jointures dans la clause WHERE //
|
||||
foreach($this->joined as $field=>$data){
|
||||
// {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 //
|
||||
$requestS .= $this->schema['table'].'.'.$localField." in (\n\t";
|
||||
// {2} On met la clé étrangère pour la clause SELECT //
|
||||
$joinedFetched['request']['SELECT'] = [ $data['object']->schema['table'].'.'.$data['field'] ];
|
||||
|
||||
// {2} On récupère la requête //
|
||||
$RowsFetched = $rows[0]->fetch(false);
|
||||
// {3} On construit la nouvelle requête //
|
||||
$joinedRequest = SQLBuilder::BUILD($joinedFetched['request']);
|
||||
|
||||
// {3} On ajoute la requête //
|
||||
$requestS .= implode("\n\t", explode("\n", $RowsFetched['request']));
|
||||
// {4} On supprime les retours à la ligne //
|
||||
$joinedRequest = str_replace("\n", " ", $joinedRequest);
|
||||
|
||||
// {4} On bind les params de la requête //
|
||||
$binded = array_merge($binded, $RowsFetched['binded']);
|
||||
// {5} On l'ajoute à la clause FROM avec comme alias le nom de la table de @data['object'] //
|
||||
$requestS['WHERE'][] = $this->schema['table'].".$field in ($joinedRequest)";
|
||||
|
||||
// {5} On précise qu'on veut un seul résultat //
|
||||
$requestS .= ")\n";
|
||||
|
||||
$c++;
|
||||
// {6} On ajoute les variables à la requête courante //
|
||||
$binded = array_merge($binded, $joinedFetched['binded']);
|
||||
}
|
||||
|
||||
|
||||
/* [4] Clause LIMIT
|
||||
/* [5] Clause LIMIT
|
||||
=========================================================*/
|
||||
if( $this->unique )
|
||||
$requestS .= "LIMIT 1";
|
||||
$requestS['LIMIT'] = ($this->unique) ? SQLBuilder::LIMIT(1) : SQLBuilder::LIMIT([]);
|
||||
|
||||
|
||||
/* [5] On prépare la requête
|
||||
/* [6] On prépare et compose la requête
|
||||
=========================================================*/
|
||||
/* (0) On prépare la requête */
|
||||
$request = Database::getPDO()->prepare($requestS.';');
|
||||
/* (1) On compose la requête */
|
||||
$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 */
|
||||
$updated = $request->execute($binded);
|
||||
|
@ -650,6 +658,7 @@
|
|||
/* [2] On rédige la clause WHERE/AND
|
||||
=========================================================*/
|
||||
/* (1) On met les conditions locales */
|
||||
$requestS['WHERE'] = [];
|
||||
$c = 0;
|
||||
foreach($this->where as $field=>$conditions)
|
||||
foreach($conditions as $cdt=>$value){
|
||||
|
|
|
@ -182,9 +182,7 @@
|
|||
$c = 0;
|
||||
foreach($values as $field=>$value){
|
||||
/* (1) Champ */
|
||||
$sql[$c] = ($c>0) ? "\n, " : '';
|
||||
|
||||
$sql[$c] .= $field.' = ';
|
||||
$sql[$c] = $field.' = ';
|
||||
|
||||
/* (2) Variable */
|
||||
$sql[$c] .= ':update_'.$field;
|
||||
|
@ -308,6 +306,26 @@
|
|||
case 'DELETE':
|
||||
$sql .= "DELETE FROM $statements\n";
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -345,25 +345,25 @@
|
|||
|
||||
/* [1] FETCH
|
||||
=========================================================*/
|
||||
$warehouse =
|
||||
Table::get('warehouse') // Access to table 'warehouse'
|
||||
->whereName(['stef-montauban', Rows::COND_EQUAL]) // condition : name = 'my-warehouse'
|
||||
->select('name');
|
||||
|
||||
|
||||
$myUser =
|
||||
Table::get('user') // Access to table 'user'
|
||||
->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)
|
||||
->whereUsername(['%', 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'
|
||||
// $warehouse =
|
||||
// Table::get('warehouse') // Access to table 'warehouse'
|
||||
// ->whereName(['stef-montauban', Rows::COND_EQUAL]) // condition : name = 'my-warehouse'
|
||||
// ->select('name');
|
||||
//
|
||||
//
|
||||
// $myUser =
|
||||
// Table::get('user') // Access to table 'user'
|
||||
// ->whereId([100, Rows::COND_INF]) // 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'
|
||||
// ->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
|
||||
->delete(); // Result
|
||||
var_dump($myUser);
|
||||
//
|
||||
// // SELECT
|
||||
// // ->fetch();
|
||||
// var_dump($myUser);
|
||||
|
||||
/* [2] Modification
|
||||
=========================================================*/
|
||||
|
|
Loading…
Reference in New Issue