From 4c85068a85e533e333c45c814c647346cf33aa29 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Thu, 21 Mar 2019 11:53:04 +0100 Subject: [PATCH 1/2] FIX Can't insert if there is extrafields mandatory on another entity. --- htdocs/core/class/commonobject.class.php | 17 +++++++++++++++-- htdocs/core/class/extrafields.class.php | 16 +++++++++++++--- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index d8d27a0378a..77e0a557c5d 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -4979,8 +4979,9 @@ abstract class CommonObject $table_element = $this->table_element; if ($table_element == 'categorie') $table_element = 'categories'; // For compatibility + dol_syslog(get_class($this)."::insertExtraFields delete then insert", LOG_DEBUG); + $sql_del = "DELETE FROM ".MAIN_DB_PREFIX.$table_element."_extrafields WHERE fk_object = ".$this->id; - dol_syslog(get_class($this)."::insertExtraFields delete", LOG_DEBUG); $this->db->query($sql_del); $sql = "INSERT INTO ".MAIN_DB_PREFIX.$table_element."_extrafields (fk_object"; @@ -4991,6 +4992,11 @@ abstract class CommonObject if ($extrafields->attributes[$this->table_element]['type'][$attributeKey] != 'separate') // Only for other type than separator $sql.=",".$attributeKey; } + // We must insert a default value for fields for other entities that are mandatory to avoid not null error + foreach($extrafields->attributes[$this->table_element]['mandatoryfieldsofotherentities'] as $tmpkey => $tmpval) + { + $sql.=",".$tmpkey; + } $sql .= ") VALUES (".$this->id; foreach($new_array_options as $key => $value) @@ -5009,10 +5015,17 @@ abstract class CommonObject } } } + // We must insert a default value for fields for other entities that are mandatory to avoid not null error + foreach($extrafields->attributes[$this->table_element]['mandatoryfieldsofotherentities'] as $tmpkey => $tmpval) + { + if (in_array($tmpval, array('int', 'double'))) $sql.=", 0"; + else $sql.=", ''"; + } + $sql.=")"; - dol_syslog(get_class($this)."::insertExtraFields insert", LOG_DEBUG); $resql = $this->db->query($sql); + if (! $resql) { $this->error=$this->db->lasterror(); diff --git a/htdocs/core/class/extrafields.class.php b/htdocs/core/class/extrafields.class.php index 48eabbefe9e..679352dde06 100644 --- a/htdocs/core/class/extrafields.class.php +++ b/htdocs/core/class/extrafields.class.php @@ -747,7 +747,7 @@ class ExtraFields * @param boolean $forceload Force load of extra fields whatever is option MAIN_EXTRAFIELDS_DISABLED. Deprecated. Should not be required. * @return array Array of attributes keys+label for all extra fields. */ - function fetch_name_optionals_label($elementtype,$forceload=false) + function fetch_name_optionals_label($elementtype, $forceload=false) { global $conf; @@ -783,8 +783,8 @@ class ExtraFields $sql = "SELECT rowid,name,label,type,size,elementtype,fieldunique,fieldrequired,param,pos,alwayseditable,perms,langs,list,fielddefault,fieldcomputed,entity,enabled"; $sql.= " FROM ".MAIN_DB_PREFIX."extrafields"; - $sql.= " WHERE entity IN (0,".$conf->entity.")"; - if ($elementtype) $sql.= " AND elementtype = '".$elementtype."'"; // Filed with object->table_element + //$sql.= " WHERE entity IN (0,".$conf->entity.")"; // Filter is done later + if ($elementtype) $sql.= " WHERE elementtype = '".$elementtype."'"; // Filed with object->table_element $sql.= " ORDER BY pos"; $resql=$this->db->query($sql); @@ -794,6 +794,16 @@ class ExtraFields { while ($tab = $this->db->fetch_object($resql)) { + if ($tab->entity != 0 && $tab->entity != $conf->entity) + { + // This field is not in current entity. We discard but before we save it into the array of mandatory fields if it is a mandatory field + if ($tab->fieldrequired) + { + $this->attributes[$tab->elementtype]['mandatoryfieldsofotherentities'][$tab->name]=$tab->type; + } + continue; + } + // We can add this attribute to object. TODO Remove this and return $this->attributes[$elementtype]['label'] if ($tab->type != 'separate') { From 032aa869e4124a4bee4752303690ba4090f0f82d Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Thu, 21 Mar 2019 11:54:14 +0100 Subject: [PATCH 2/2] FIX Can't insert if there is extrafields mandatory on another entity. FIX Can't set default value of extrafield of type varchar --- htdocs/core/class/commonobject.class.php | 16 +++++++++++----- htdocs/core/class/extrafields.class.php | 2 +- htdocs/core/db/mysqli.class.php | 2 +- htdocs/core/db/pgsql.class.php | 2 +- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 77e0a557c5d..90bd03513fe 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -4993,9 +4993,12 @@ abstract class CommonObject $sql.=",".$attributeKey; } // We must insert a default value for fields for other entities that are mandatory to avoid not null error - foreach($extrafields->attributes[$this->table_element]['mandatoryfieldsofotherentities'] as $tmpkey => $tmpval) + if (is_array($extrafields->attributes[$this->table_element]['mandatoryfieldsofotherentities'])) { - $sql.=",".$tmpkey; + foreach($extrafields->attributes[$this->table_element]['mandatoryfieldsofotherentities'] as $tmpkey => $tmpval) + { + $sql.=",".$tmpkey; + } } $sql .= ") VALUES (".$this->id; @@ -5016,10 +5019,13 @@ abstract class CommonObject } } // We must insert a default value for fields for other entities that are mandatory to avoid not null error - foreach($extrafields->attributes[$this->table_element]['mandatoryfieldsofotherentities'] as $tmpkey => $tmpval) + if (is_array($extrafields->attributes[$this->table_element]['mandatoryfieldsofotherentities'])) { - if (in_array($tmpval, array('int', 'double'))) $sql.=", 0"; - else $sql.=", ''"; + foreach($extrafields->attributes[$this->table_element]['mandatoryfieldsofotherentities'] as $tmpkey => $tmpval) + { + if (in_array($tmpval, array('int', 'double'))) $sql.=", 0"; + else $sql.=", ''"; + } } $sql.=")"; diff --git a/htdocs/core/class/extrafields.class.php b/htdocs/core/class/extrafields.class.php index 679352dde06..6e92509756f 100644 --- a/htdocs/core/class/extrafields.class.php +++ b/htdocs/core/class/extrafields.class.php @@ -797,7 +797,7 @@ class ExtraFields if ($tab->entity != 0 && $tab->entity != $conf->entity) { // This field is not in current entity. We discard but before we save it into the array of mandatory fields if it is a mandatory field - if ($tab->fieldrequired) + if ($tab->fieldrequired && is_null($tab->fielddefault)) { $this->attributes[$tab->elementtype]['mandatoryfieldsofotherentities'][$tab->name]=$tab->type; } diff --git a/htdocs/core/db/mysqli.class.php b/htdocs/core/db/mysqli.class.php index 23c19542ae6..738be0b2d86 100644 --- a/htdocs/core/db/mysqli.class.php +++ b/htdocs/core/db/mysqli.class.php @@ -835,7 +835,7 @@ class DoliDBMysqli extends DoliDB if ($field_desc['default'] != '') { if ($field_desc['type'] == 'double' || $field_desc['type'] == 'tinyint' || $field_desc['type'] == 'int') $sql.=" DEFAULT ".$this->escape($field_desc['default']); - elseif ($field_desc['type'] == 'text') $sql.=" DEFAULT '".$this->escape($field_desc['default'])."'"; // Default not supported on text fields + elseif ($field_desc['type'] != 'text') $sql.=" DEFAULT '".$this->escape($field_desc['default'])."'"; // Default not supported on text fields } dol_syslog(get_class($this)."::DDLUpdateField ".$sql,LOG_DEBUG); diff --git a/htdocs/core/db/pgsql.class.php b/htdocs/core/db/pgsql.class.php index 3d365142a0a..ffbabc24d79 100644 --- a/htdocs/core/db/pgsql.class.php +++ b/htdocs/core/db/pgsql.class.php @@ -1138,7 +1138,7 @@ class DoliDBPgsql extends DoliDB if ($field_desc['default'] != '') { if ($field_desc['type'] == 'double' || $field_desc['type'] == 'tinyint' || $field_desc['type'] == 'int') $sql.=" DEFAULT ".$this->escape($field_desc['default']); - elseif ($field_desc['type'] == 'text') $sql.=" DEFAULT '".$this->escape($field_desc['default'])."'"; // Default not supported on text fields + elseif ($field_desc['type'] != 'text') $sql.=" DEFAULT '".$this->escape($field_desc['default'])."'"; // Default not supported on text fields } dol_syslog($sql,LOG_DEBUG);