Sync: respect null properties on merge

fixes #2623
This commit is contained in:
Thomas Gelf 2022-10-07 10:05:01 +02:00
parent 82fbd5359e
commit 9a2c0162d2

View File

@ -46,6 +46,9 @@ class Sync
/** @var IcingaObject[] Objects to work with */
protected $objects;
/** @var array<mixed, array<int, string>> key => [property, property]*/
protected $setNull = [];
/** @var bool Whether we already prepared your sync */
protected $isPrepared = false;
@ -513,7 +516,7 @@ class Sync
}
$object = $objects[$key];
$this->prepareNewObject($row, $object, $sourceId);
$this->prepareNewObject($row, $object, $key, $sourceId);
}
}
@ -527,7 +530,7 @@ class Sync
* @throws \Icinga\Exception\NotFoundError
* @throws \Icinga\Module\Director\Exception\DuplicateKeyException
*/
protected function prepareNewObject($row, DbObject $object, $sourceId)
protected function prepareNewObject($row, DbObject $object, $objectKey, $sourceId)
{
foreach ($this->syncProperties as $propertyKey => $p) {
if ($p->get('source_id') !== $sourceId) {
@ -539,7 +542,6 @@ class Sync
}
$prop = $p->get('destination_field');
$val = SyncUtils::fillVariables($p->get('source_expression'), $row);
if ($object instanceof IcingaObject) {
@ -561,15 +563,23 @@ class Sync
$this->wantArray($val)
);
} else {
$object->vars()->$varName = $val;
if ($val === null) {
$this->setNull[$objectKey][] = $prop;
} else {
$object->vars()->$varName = $val;
}
}
} else {
if ($val !== null) {
if ($val === null) {
$this->setNull[$objectKey][] = $prop;
} else {
$object->set($prop, $val);
}
}
} else {
if ($val !== null) {
if ($val === null) {
$this->setNull[$objectKey][] = $prop;
} else {
$object->set($prop, $val);
}
}
@ -776,6 +786,12 @@ class Sync
}
}
}
if (isset($this->setNull[$key])) {
foreach ($this->setNull[$key] as $property) {
$this->objects[$key]->set($property, null);
}
}
}
/**