diff --git a/library/Director/CustomVariable/CustomVariables.php b/library/Director/CustomVariable/CustomVariables.php index 35c55f3b..848adb90 100644 --- a/library/Director/CustomVariable/CustomVariables.php +++ b/library/Director/CustomVariable/CustomVariables.php @@ -181,7 +181,7 @@ class CustomVariables implements Iterator, Countable, IcingaConfigRenderer $vars->vars[$row->varname] = CustomVariable::fromDbRow($row); } $vars->refreshIndex(); - $vars->setUnmodified(); + $vars->setBeingLoadedFromDb(); return $vars; } @@ -192,7 +192,7 @@ class CustomVariables implements Iterator, Countable, IcingaConfigRenderer $vars->vars[$row->varname] = CustomVariable::fromDbRow($row); } $vars->refreshIndex(); - $vars->setUnmodified(); + $vars->setBeingLoadedFromDb(); return $vars; } @@ -237,7 +237,7 @@ class CustomVariables implements Iterator, Countable, IcingaConfigRenderer } } - $this->setUnmodified(); + $this->setBeingLoadedFromDb(); } public function get($key) @@ -264,14 +264,16 @@ class CustomVariables implements Iterator, Countable, IcingaConfigRenderer return false; } - public function setUnmodified() + public function setBeingLoadedFromDb() { $this->modified = false; $this->storedVars = array(); foreach ($this->vars as $key => $var) { $this->storedVars[$key] = clone($var); $var->setUnmodified(); + $var->setLoadedFromDb(); } + return $this; } diff --git a/library/Director/IcingaConfig/ExtensibleSet.php b/library/Director/IcingaConfig/ExtensibleSet.php index f8c96bae..f5b49847 100644 --- a/library/Director/IcingaConfig/ExtensibleSet.php +++ b/library/Director/IcingaConfig/ExtensibleSet.php @@ -288,8 +288,6 @@ class ExtensibleSet array_merge($props, array('property' => $value)) ); } - - $this->fromDb['override'] = $this->ownValues; } if (! empty($this->plusValues)) { @@ -300,8 +298,6 @@ class ExtensibleSet array_merge($props, array('property' => $value)) ); } - - $this->fromDb['extend'] = $this->ownValues; } if (! empty($this->minusValues)) { @@ -312,9 +308,18 @@ class ExtensibleSet array_merge($props, array('property' => $value)) ); } - - $this->fromDb['blacklist'] = $this->ownValues; } + + $this->setBeingLoadedFromDb(); + } + + public function setBeingLoadedFromDb() + { + $this->fromDb = [ + 'override' => $this->ownValues ?: [], + 'extend' => $this->plusValues ?: [], + 'blacklist' => $this->minusValues ?: [], + ]; } public function override($values) diff --git a/library/Director/Objects/IcingaArguments.php b/library/Director/Objects/IcingaArguments.php index 1b85a7dd..e87cc18c 100644 --- a/library/Director/Objects/IcingaArguments.php +++ b/library/Director/Objects/IcingaArguments.php @@ -341,6 +341,15 @@ class IcingaArguments implements Iterator, Countable, IcingaConfigRenderer return $arguments->loadFromDb(); } + public function setBeingLoadedFromDb() + { + foreach ($this->arguments as $argument) { + $argument->setBeingLoadedFromDb(); + } + $this->refreshIndex(); + $this->cloneStored(); + } + /** * @return $this * @throws \Icinga\Module\Director\Exception\DuplicateKeyException @@ -353,8 +362,14 @@ class IcingaArguments implements Iterator, Countable, IcingaConfigRenderer if ($argument->shouldBeRemoved()) { $deleted[] = $key; } else { - $argument->set('command_id', $this->object->get('id')); - $argument->store($db); + if ($argument->hasBeenModified()) { + if ($argument->hasBeenLoadedFromDb()) { + $argument->setLoadedProperty('command_id', $this->object->get('id')); + } else { + $argument->set('command_id', $this->object->get('id')); + } + $argument->store($db); + } } } diff --git a/library/Director/Objects/IcingaObject.php b/library/Director/Objects/IcingaObject.php index 729ccdf2..77aff428 100644 --- a/library/Director/Objects/IcingaObject.php +++ b/library/Director/Objects/IcingaObject.php @@ -1489,6 +1489,35 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer && $this->get('object_type') === 'apply'; } + public function setBeingLoadedFromDb() + { + if ($this instanceof ObjectWithArguments && $this->gotArguments()) { + $this->arguments()->setBeingLoadedFromDb(); + } + if ($this->supportsImports() && $this->gotImports()) { + $this->imports()->setBeingLoadedFromDb(); + } + if ($this->supportsCustomVars() && $this->vars !== null) { + $this->vars()->setBeingLoadedFromDb(); + } + if ($this->supportsGroups() && $this->groups !== null) { + $this->groups()->setBeingLoadedFromDb(); + } + if ($this->supportsRanges() && $this->ranges === null) { + $this->ranges()->setBeingLoadedFromDb(); + } + + foreach ($this->loadedRelatedSets as $set) { + $set->setBeingLoadedFromDb(); + } + + foreach ($this->loadedMultiRelations as $multiRelation) { + $multiRelation->setBeingLoadedFromDb(); + } + + parent::setBeingLoadedFromDb(); + } + /** * @throws NotFoundError * @throws \Icinga\Module\Director\Exception\DuplicateKeyException diff --git a/library/Director/Objects/IcingaObjectGroups.php b/library/Director/Objects/IcingaObjectGroups.php index 8a8510d4..76b98cec 100644 --- a/library/Director/Objects/IcingaObjectGroups.php +++ b/library/Director/Objects/IcingaObjectGroups.php @@ -274,7 +274,7 @@ class IcingaObjectGroups implements Iterator, Countable, IcingaConfigRenderer $class = $this->getGroupClass(); $this->groups = $class::loadAll($connection, $query, 'object_name'); - $this->cloneStored(); + $this->setBeingLoadedFromDb(); return $this; } @@ -314,12 +314,12 @@ class IcingaObjectGroups implements Iterator, Countable, IcingaConfigRenderer ) ); } - $this->cloneStored(); + $this->setBeingLoadedFromDb(); return true; } - protected function cloneStored() + public function setBeingLoadedFromDb() { $this->storedGroups = array(); foreach ($this->groups as $k => $v) { @@ -341,7 +341,7 @@ class IcingaObjectGroups implements Iterator, Countable, IcingaConfigRenderer if (PrefetchCache::shouldBeUsed()) { $groups->groups = PrefetchCache::instance()->groups($object); - $groups->cloneStored(); + $groups->setBeingLoadedFromDb(); } else { $groups->loadFromDb(); } diff --git a/library/Director/Objects/IcingaObjectImports.php b/library/Director/Objects/IcingaObjectImports.php index 6591a2fb..75279683 100644 --- a/library/Director/Objects/IcingaObjectImports.php +++ b/library/Director/Objects/IcingaObjectImports.php @@ -308,7 +308,7 @@ class IcingaObjectImports implements Iterator, Countable, IcingaConfigRenderer $this->imports = array_combine($keys, $keys); } - $this->cloneStored(); + $this->setBeingLoadedFromDb(); return $this; } @@ -355,12 +355,12 @@ class IcingaObjectImports implements Iterator, Countable, IcingaConfigRenderer ]); } - $this->cloneStored(); + $this->setBeingLoadedFromDb(); return true; } - protected function cloneStored() + public function setBeingLoadedFromDb() { $this->storedNames = $this->listImportNames(); $this->modified = false; diff --git a/library/Director/Objects/IcingaObjectMultiRelations.php b/library/Director/Objects/IcingaObjectMultiRelations.php index 2d36d9d1..a3d1d519 100644 --- a/library/Director/Objects/IcingaObjectMultiRelations.php +++ b/library/Director/Objects/IcingaObjectMultiRelations.php @@ -323,7 +323,7 @@ class IcingaObjectMultiRelations implements Iterator, Countable, IcingaConfigRen $class = $this->getRelatedClassName(); $this->relations = $class::loadAll($connection, $query, 'object_name'); - $this->cloneStored(); + $this->setBeingLoadedFromDb(); return $this; } @@ -331,13 +331,10 @@ class IcingaObjectMultiRelations implements Iterator, Countable, IcingaConfigRen public function store() { $db = $this->getDb(); - $stored = array_keys($this->stored); $relations = array_keys($this->relations); $objectId = $this->object->id; - $type = $this->getType(); - $type = $this->getType(); $objectCol = $type . '_id'; $relationCol = $this->getRelationIdColumn() . '_id'; @@ -369,12 +366,12 @@ class IcingaObjectMultiRelations implements Iterator, Countable, IcingaConfigRen ) ); } - $this->cloneStored(); + $this->setBeingLoadedFromDb(); return true; } - protected function cloneStored() + public function setBeingLoadedFromDb() { $this->stored = array(); foreach ($this->relations as $k => $v) { diff --git a/library/Director/Objects/IcingaScheduledDowntimeRanges.php b/library/Director/Objects/IcingaScheduledDowntimeRanges.php index ba2ec0d3..d283a8e5 100644 --- a/library/Director/Objects/IcingaScheduledDowntimeRanges.php +++ b/library/Director/Objects/IcingaScheduledDowntimeRanges.php @@ -211,13 +211,18 @@ class IcingaScheduledDowntimeRanges implements Iterator, Countable, IcingaConfig ->order('o.range_key'); $this->ranges = IcingaScheduledDowntimeRange::loadAll($connection, $query, 'range_key'); - $this->storedRanges = array(); + $this->setBeingLoadedFromDb(); + + return $this; + } + + public function setBeingLoadedFromDb() + { + $this->storedRanges = []; foreach ($this->ranges as $key => $range) { $this->storedRanges[$key] = clone($range); } - - return $this; } public function store() diff --git a/library/Director/Objects/IcingaTimePeriodRanges.php b/library/Director/Objects/IcingaTimePeriodRanges.php index 7ffb117b..7f017bf1 100644 --- a/library/Director/Objects/IcingaTimePeriodRanges.php +++ b/library/Director/Objects/IcingaTimePeriodRanges.php @@ -216,13 +216,18 @@ class IcingaTimePeriodRanges implements Iterator, Countable, IcingaConfigRendere /** @var IcingaTimePeriodRange $class */ $class = $this->getClass(); $this->ranges = $class::loadAll($connection, $query, 'range_key'); - $this->storedRanges = array(); + $this->setBeingLoadedFromDb(); + + return $this; + } + + public function setBeingLoadedFromDb() + { + $this->storedRanges = []; foreach ($this->ranges as $key => $range) { $this->storedRanges[$key] = clone($range); } - - return $this; } public function store()