diff --git a/CHANGELOG.md b/CHANGELOG.md index 4370626e..c33b7a3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fix crash when injection model has no mandatory fields defined - Fix models created on parent entities can't be used on child entites +- Fix association of imported data with non-existing groups when the group column is empty in the import file ## [2.15.4] - 2026-03-16 diff --git a/inc/commoninjectionlib.class.php b/inc/commoninjectionlib.class.php index 8980b4c1..2c7b6afd 100644 --- a/inc/commoninjectionlib.class.php +++ b/inc/commoninjectionlib.class.php @@ -28,6 +28,7 @@ * ------------------------------------------------------------------------- */ use Glpi\Exception\Http\HttpException; +use Glpi\Features\AssignableItem; use function Safe\preg_match; use function Safe\preg_replace; @@ -963,8 +964,14 @@ private function setValueForItemtype($itemtype, $field, $value, $fromdb = false) ]; if (empty($value) && $value !== 0 && $value !== '0') { if (isForeignKeyField($field) || (str_contains($field, 'is_')) || (method_exists($injectionClass, 'isNullable') && !$injectionClass->isNullable($field))) { - // If the field is an id, we set it to 0 - $this->values[$itemtype][$field] = self::DROPDOWN_EMPTY_VALUE; + //If the field concernes groupds, unseting it instead of setting it to 0 in order to avoid associating the item to a non existing group (id 0) + $group_fields = ['groups_id_tech', 'groups_id', 'groups_id_normal']; + if (in_array($field, $group_fields) && Toolbox::hasTrait($itemtype, AssignableItem::class)) { + unset($this->values[$itemtype][$field]); + } else { + // If the field is an id, we set it to 0 + $this->values[$itemtype][$field] = self::DROPDOWN_EMPTY_VALUE; + } } else { // Else we set it to NULL $this->values[$itemtype][$field] = self::EMPTY_VALUE;