Should I beginTransaction / Commit every prepare->(“”) statement? Or only one per code?
I’m wringting some big stuff using this way, but I’m worried that, when I finish it, its all wrong. How should I do?
Example:
self::$pdo->beginTransaction();
$cmd = self::$pdo->prepare("
INSERT INTO historico_horas (id_data, id_colab, atrasos, data_ocorrencia, data_registro) VALUES (:iddata, :id, :qnt_atrasos, :dataocorrencia, :dataregistro)");
$id_data_gerado = self::$pdo->lastInsertId();
$cmd->bindParam(":iddata", $id_data_gerado);
$cmd->bindParam(':id', self::$ColabId);
$cmd->bindParam(':qnt_atrasos', $totalMinutos);
$cmd->bindParam(':dataocorrencia', $d_ocorr);
$cmd->bindParam(':dataregistro', self::$dataRegistro);
if ($cmd->execute()) {
if ($_ENV['APP_ENV'] == "development") {
self::$logger->info('Dados comitados com sucesso para o banco de horas.');
}
self::$pdo->commit();
Alert::Alert("AtrasoCad");
exit();
} else {
if ($_ENV['APP_ENV'] == "development") {
self::$logger->info('Não foi possível commitar os dados para o histórico.');
}
self::$pdo->rollBack();
}
exit();
5