From fce1468c2e274bace2822a513fb219026c306c01 Mon Sep 17 00:00:00 2001 From: David Goodwin Date: Sat, 11 Jun 2022 09:04:40 +0100 Subject: [PATCH] wrap save into a transaction so if it or a postSave script fail, we roll back --- model/PFAHandler.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/model/PFAHandler.php b/model/PFAHandler.php index 1322b8fd..510da644 100644 --- a/model/PFAHandler.php +++ b/model/PFAHandler.php @@ -589,20 +589,29 @@ abstract class PFAHandler unset($db_values[$key]); } # remove 'dont_write_to_db' columns } - + $pdo = db_connect(); try { + // start db transaction + $pdo->beginTransaction(); if ($this->new) { $result = db_insert($this->db_table, $db_values, array('created', 'modified'),true); } else { $result = db_update($this->db_table, $this->id_field, $this->id, $db_values, array('modified'), true); } + + // problems with this -> postSave, if it executes a db query, will not see the updated db values until this + // transaction has been committed. + $result = $this->postSave(); + + // commit db transaction + $pdo->commit(); + } catch (PDOException $e) { + // rollback + $pdo->rollBack(); $this->errormsg[] = Config::lang_f($this->msg['store_error'], $this->label); return false; } - - $result = $this->postSave(); - # db_log() even if postSave() failed db_log($this->domain, $this->msg['logname'], $this->id);