2
I’m developing a system and I’m trying to simplify my life with querys basics of insert
and update
, and I came across a question:
I must create a query for each case, or one that meets all the cases, where they have the same logic?
As querys that I developed are these:
//$nome_tabela : nome da tabela sem o 'tb_'
//$info : vetor ordenado de informações a serem inseridas
//
//obs.: os indices do vetor devem ser o nome do respectivo campo
// sem o '_tabela'
//
private function pdo_cadastro($nome_tabela, $info) {
//montagem da query dinâmica
$cont_param = 0;
$sql = "INSERT INTO tb_$nome_tabela(";
foreach ($info as $index => $key) {
$sql .= $index . "_" . $nome_tabela . ", ";
$cont_param += 1;
}
$sql = substr_replace($sql, "", -2);
$sql .= ") VALUES (";
for ($i = 0; $i < $cont_param; $i++) {
$sql .= "?,";
}
$sql = substr_replace($sql, "", -1);
$sql .= ")";
//---------------------------------
//execução da query
try {
$prepara = $this->pdo->prepare($sql);
$controle = 1;
foreach ($info as $index => $key) {
$prepara->bindParam($controle, $info[$index], PDO::PARAM_INT);
$controle += 1;
}
$prepara->execute();
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
}
//$nome_tabela : nome da tabela sem o 'tb_'
//$info : vetor ordenado de informações a serem inseridas
//$campo_id : nome do campo do código identificador
//
//obs.: os indices do vetor devem ser o nome do respectivo campo
// sem o '_tabela'
//
private function pdo_edit($nome_tabela, $info, $campo_id) {
//montagem da query dinâmica
$cont_param = 0;
$sql = "UPDATE tb_$nome_tabela SET ";
foreach ($info as $index => $key) {
if($index!==$campo_id){
$sql .= $index . "_" . $nome_tabela . " = ?, ";
$cont_param += 1;
}
}
$sql = substr_replace($sql, "", -2);
$sql .= " WHERE ". $campo_id . "_" . $nome_tabela . " = ?" ;
//---------------------------------
try {
$prepara = $this->pdo->prepare($sql);
$controle = 1;
foreach ($info as $index => $key) {
$prepara -> bindParam($controle, $info[$index], PDO::PARAM_INT);
$controle += 1;
}
$prepara -> execute();
} catch (PDOException $e) {
print "Error!: " . $e -> getMessage() . "<br/>";
die();
}
}
I inform everything I need at the call, telling you that the forms were built by following the names of the columns in DB. With this added the table name and the identification field (the id
in my case e.g. id_cliente AUTO-INCREMENT PRIMARY
), I can perform these actions only with these two functions.
Is that a good practice or should I actually make one for each case?
I prefer the way it is, in the future may have to make a change that is difficult to maintain. I recommend reading at: What are the concepts of cohesion and coupling? which is related to your question.
– Marconi
Reading is really good @Marconi Now on the issue of coupling, my goal is, in fact, to establish a strong relationship between the interface and the bank and a standard of nomenclatures. The coupling is quite high, but in the mini-world of this system is exactly what I need :)
– Silvio Santos