Enhance update of 1 extrafield (simple update instead of delete+insert)

This commit is contained in:
Laurent Destailleur 2020-10-23 15:31:13 +02:00
parent d21ad72e4a
commit 572637b726
2 changed files with 47 additions and 14 deletions

View File

@ -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');

View File

@ -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