Merge pull request #24152 from OPEN-DSI/develop_new_provide_oldcopy_in_setvaluefrom_function

NEW - Provide the oldcopy value when calling setValueFrom() function with a trigger key
This commit is contained in:
Laurent Destailleur 2023-03-10 19:10:23 +01:00 committed by GitHub
commit c20471bc99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2089,10 +2089,6 @@ abstract class CommonObject
$id_field = 'rowid';
}
$error = 0;
$this->db->begin();
// Special case
if ($table == 'product' && $field == 'note_private') {
$field = 'note';
@ -2101,6 +2097,32 @@ abstract class CommonObject
$fk_user_field = 'fk_user_mod';
}
if ($trigkey) {
$oldvalue = null;
$sql = "SELECT " . $field;
$sql .= " FROM " . MAIN_DB_PREFIX . $table;
$sql .= " WHERE " . $id_field . " = " . ((int) $id);
$resql = $this->db->query($sql);
if ($resql) {
if ($obj = $this->db->fetch_object($resql)) {
if ($format == 'date') {
$oldvalue = $this->db->jdate($obj->$field);
} else {
$oldvalue = $obj->$field;
}
}
} else {
$this->error = $this->db->lasterror();
return -1;
}
}
$error = 0;
$this->db->begin();
$sql = "UPDATE ".$this->db->prefix().$table." SET ";
if ($format == 'text') {
@ -2133,6 +2155,11 @@ abstract class CommonObject
} else {
$result = $this->fetchCommon($id);
}
$this->oldcopy = clone $this;
if (property_exists($this->oldcopy, $field)) {
$this->oldcopy->$field = $oldvalue;
}
if ($result >= 0) {
$result = $this->call_trigger($trigkey, (!empty($fuser) && is_object($fuser)) ? $fuser : $user); // This may set this->errors
}