Clean code using of oldcopy

This commit is contained in:
Laurent Destailleur 2022-08-22 15:06:09 +02:00
parent 35de9843d0
commit f17f22755c
7 changed files with 32 additions and 23 deletions

View File

@ -3132,11 +3132,13 @@ class ContratLigne extends CommonObjectLine
}
}
// $this->oldcopy should have been set by the caller of update (here properties were already modified)
if (empty($this->oldcopy)) {
$this->oldcopy = dol_clone($this);
}
$this->db->begin();
$this->oldcopy = dol_clone($this);
// Update request
$sql = "UPDATE ".MAIN_DB_PREFIX."contratdet SET";
$sql .= " fk_contrat=".((int) $this->fk_contrat).",";

View File

@ -8918,11 +8918,12 @@ abstract class CommonObject
/**
* Function to prepare a part of the query for insert.
* Note $this->${field} are set by the page that make the createCommon or the updateCommon.
* $this->${field} should be a clean value. The page can run
* Function to prepare a part of the query for insert by returning an array with all properties of object.
*
* @return array
* Note $this->${field} are set by the page that make the createCommon() or the updateCommon().
* $this->${field} should be a clean and string value (so date are formated for SQL insert).
*
* @return array Array with all values of each properties to update
*/
protected function setSaveQuery()
{
@ -8950,7 +8951,7 @@ abstract class CommonObject
}
} elseif ($this->isInt($info) || $this->isFloat($info)) {
if ($field == 'entity' && is_null($this->{$field})) {
$queryarray[$field] = $conf->entity;
$queryarray[$field] = ((int) $conf->entity);
} else {
// $this->{$field} may be null, '', 0, '0', 123, '123'
if ((isset($this->{$field}) && $this->{$field} != '') || !empty($info['notnull'])) {
@ -9378,6 +9379,11 @@ abstract class CommonObject
$now = dol_now();
// $this->oldcopy should have been set by the caller of update
//if (empty($this->oldcopy)) {
// $this->oldcopy = dol_clone($this);
//}
$fieldvalues = $this->setSaveQuery();
if (array_key_exists('date_modification', $fieldvalues) && empty($fieldvalues['date_modification'])) {
@ -9423,6 +9429,7 @@ abstract class CommonObject
$sql = 'UPDATE '.$this->db->prefix().$this->table_element.' SET '.implode(', ', $tmp).' WHERE rowid='.((int) $this->id);
$this->db->begin();
if (!$error) {
$res = $this->db->query($sql);
if (!$res) {

View File

@ -524,6 +524,8 @@ class Fichinter extends CommonObject
dol_syslog(get_class($this)."::setDraft", LOG_DEBUG);
$this->oldcopy = dol_clone($this);
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."fichinter";
@ -532,10 +534,6 @@ class Fichinter extends CommonObject
$resql = $this->db->query($sql);
if ($resql) {
if (!$error) {
$this->oldcopy = dol_clone($this);
}
if (!$error) {
// Call trigger
$result = $this->call_trigger('FICHINTER_UNVALIDATE', $user);
@ -1411,6 +1409,8 @@ class Fichinter extends CommonObject
$this->db->begin();
$this->oldcopy = dol_clone($this);
$sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET ref_client = ".(empty($ref_client) ? 'NULL' : "'".$this->db->escape($ref_client)."'");
$sql .= " WHERE rowid = ".((int) $this->id);
@ -1422,7 +1422,6 @@ class Fichinter extends CommonObject
}
if (!$error) {
$this->oldcopy = dol_clone($this);
$this->ref_client = $ref_client;
}

View File

@ -1050,6 +1050,7 @@ class Product extends CommonObject
}
if ($result >= 0) {
// $this->oldcopy should have been set by the caller of update (here properties were already modified)
if (empty($this->oldcopy)) {
$this->oldcopy = dol_clone($this);
}

View File

@ -384,9 +384,7 @@ class Productlot extends CommonObject
$this->import_key = trim($this->import_key);
}
// Check parameters
// Put here code to add a control on parameters values
// $this->oldcopy should have been set by the caller of update (here properties were already modified)
if (empty($this->oldcopy)) {
$this->oldcopy = dol_clone($this);
}

View File

@ -262,6 +262,7 @@ class Dolresource extends CommonObject
$this->country_id = 0;
}
// $this->oldcopy should have been set by the caller of update (here properties were already modified)
if (empty($this->oldcopy)) {
$this->oldcopy = dol_clone($this);
}

View File

@ -870,9 +870,10 @@ class Ticket extends CommonObject
global $conf, $langs, $hookmanager;
$error = 0;
if (empty($notrigger)) {
$this->oldcopy = dol_clone($this, 1);
}
// $this->oldcopy should have been set by the caller of update (here properties were already modified)
//if (empty($this->oldcopy)) {
// $this->oldcopy = dol_clone($this);
//}
// Clean parameters
if (isset($this->ref)) {
@ -1463,13 +1464,12 @@ class Ticket extends CommonObject
$error = 0;
if ($this->statut != self::STATUS_CANCELED) { // no closed
if (empty($notrigger)) {
$this->oldcopy = dol_clone($this, 1);
}
$this->oldcopy = dol_clone($this);
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."ticket";
$sql .= " SET fk_statut = ".Ticket::STATUS_READ.", date_read='".$this->db->idate(dol_now())."'";
$sql .= " SET fk_statut = ".Ticket::STATUS_READ.", date_read = '".$this->db->idate(dol_now())."'";
$sql .= " WHERE rowid = ".((int) $this->id);
dol_syslog(get_class($this)."::markAsRead");
@ -1518,10 +1518,11 @@ class Ticket extends CommonObject
global $conf, $langs;
$error = 0;
$this->db->begin();
$this->oldcopy = dol_clone($this);
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."ticket";
if ($id_assign_user > 0) {
$sql .= " SET fk_user_assign=".((int) $id_assign_user).", fk_statut = ".Ticket::STATUS_ASSIGNED;