From 572637b72692a62c0913e0615598bc13279fd4e7 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Fri, 23 Oct 2020 15:31:13 +0200 Subject: [PATCH] Enhance update of 1 extrafield (simple update instead of delete+insert) --- htdocs/comm/propal/card.php | 2 +- htdocs/core/class/commonobject.class.php | 59 ++++++++++++++++++------ 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/htdocs/comm/propal/card.php b/htdocs/comm/propal/card.php index ee94c13bbf1..aa20ee83712 100644 --- a/htdocs/comm/propal/card.php +++ b/htdocs/comm/propal/card.php @@ -1321,7 +1321,7 @@ if (empty($reshook)) if ($ret < 0) $error++; if (!$error) { - $result = $object->insertExtraFields('PROPAL_MODIFY'); + $result = $object->updateExtraField(GETPOST('attribute', 'restricthtml'), 'PROPAL_MODIFY', $user); if ($result < 0) { setEventMessages($object->error, $object->errors, 'errors'); diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index c5179806ac2..952e2376f29 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -5195,7 +5195,7 @@ abstract class CommonObject $extrafields = new ExtraFields($this->db); $target_extrafields = $extrafields->fetch_name_optionals_label($this->table_element); - //Eliminate copied source object extra_fields that do not exist in target object + // Eliminate copied source object extra fields that do not exist in target object $new_array_options = array(); foreach ($this->array_options as $key => $value) { if (in_array(substr($key, 8), array_keys($target_extrafields))) // We remove the 'options_' from $key for test @@ -5325,11 +5325,10 @@ abstract class CommonObject if ($value == '-1') // -1 is key for no defined in combo list of objects { $new_array_options[$key] = ''; - } elseif ($value) - { + } elseif ($value) { $object = new $InfoFieldList[0]($this->db); - if (is_numeric($value)) $res = $object->fetch($value); - else $res = $object->fetch('', $value); + if (is_numeric($value)) $res = $object->fetch($value); // Common case + else $res = $object->fetch('', $value); // For compatibility if ($res > 0) $new_array_options[$key] = $object->id; else { @@ -5552,7 +5551,7 @@ abstract class CommonObject } /** - * Update an extra field value for the current object. + * Update 1 extra field value for the current object. Keep other fields unchanged. * Data to describe values to update are stored into $this->array_options=array('options_codeforfield1'=>'valueforfield1', 'options_codeforfield2'=>'valueforfield2', ...) * * @param string $key Key of the extrafield to update (without starting 'options_') @@ -5630,31 +5629,65 @@ abstract class CommonObject case 'datetime': $this->array_options["options_".$key] = $this->db->idate($this->array_options["options_".$key]); break; + /* case 'link': $param_list = array_keys($attributeParam['options']); // 0 : ObjectName // 1 : classPath $InfoFieldList = explode(":", $param_list[0]); dol_include_once($InfoFieldList[1]); - if ($value) + if ($InfoFieldList[0] && class_exists($InfoFieldList[0])) { - $object = new $InfoFieldList[0]($this->db); - $object->fetch(0, $value); - $this->array_options["options_".$key] = $object->id; + if ($value == '-1') // -1 is key for no defined in combo list of objects + { + $new_array_options[$key] = ''; + } elseif ($value) { + $object = new $InfoFieldList[0]($this->db); + if (is_numeric($value)) $res = $object->fetch($value); // Common case + else $res = $object->fetch('', $value); // For compatibility + + if ($res > 0) $new_array_options[$key] = $object->id; + else { + $this->error = "Id/Ref '".$value."' for object '".$object->element."' not found"; + $this->db->rollback(); + return -1; + } + } + } else { + dol_syslog('Error bad setup of extrafield', LOG_WARNING); } break; + */ } $this->db->begin(); - $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element."_extrafields SET ".$key."='".$this->db->escape($this->array_options["options_".$key])."'"; - $sql .= " WHERE fk_object = ".$this->id; + + $linealreadyfound = 0; + + // Check if there is already a line for this object (in most cases, it is, but sometimes it is not, for example when extra field has been created after), so we must keep this overload) + $sql .= "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX.$this->table_element."_extrafields WHERE fk_object = ".$this->id; + $resql = $this->db->query($sql); + if ($resql) { + $tmpobj = $this->db->fetch_object($resql); + if ($tmpobj) { + $linealreadyfound = $tmpobj->nb; + } + } + + if ($linealreadyfound) { + $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element."_extrafields SET ".$key." = '".$this->db->escape($this->array_options["options_".$key])."'"; + $sql .= " WHERE fk_object = ".$this->id; + } else { + $result = $this->insertExtraFields('', $user); + if ($result < 0) $error++; + } + $resql = $this->db->query($sql); if (!$resql) { $error++; $this->error = $this->db->lasterror(); } - if (!$error && $trigger) { // Call trigger