From c7bf68a70ad515e82c595f2739c547c91e31c7d4 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Wed, 16 Mar 2016 13:53:34 +0100 Subject: [PATCH] IcingaObjectMultiRelations: fix store and render --- library/Director/Objects/IcingaObject.php | 44 ++++++++++++++++--- .../Objects/IcingaObjectMultiRelations.php | 17 +++---- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/library/Director/Objects/IcingaObject.php b/library/Director/Objects/IcingaObject.php index d47b5dfb..a6f83e6f 100644 --- a/library/Director/Objects/IcingaObject.php +++ b/library/Director/Objects/IcingaObject.php @@ -121,11 +121,21 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer private function loadMultiRelation($property) { - $this->loadedMultiRelations[$property] = new IcingaObjectMultiRelations( - $this, - $property, - $this->multiRelations[$property] - ); + if ($this->hasBeenLoadedFromDb()) { + $rel = IcingaObjectMultiRelations::loadForStoredObject( + $this, + $property, + $this->multiRelations[$property] + ); + } else { + $rel = new IcingaObjectMultiRelations( + $this, + $property, + $this->multiRelations[$property] + ); + } + + $this->loadedMultiRelations[$property] = $rel; } private function hasLoadedMultiRelation($property) @@ -938,6 +948,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer $this ->storeCustomVars() ->storeGroups() + ->storeMultiRelations() ->storeImports() ->storeRanges() ->storeRelatedSets() @@ -979,6 +990,15 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer return $this; } + protected function storeMultiRelations() + { + foreach ($this->loadedMultiRelations as $rel) { + $rel->store(); + } + + return $this; + } + protected function storeRanges() { if ($this->supportsRanges()) { @@ -1136,6 +1156,19 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer } } + /** + * @return string + */ + protected function renderMultiRelations() + { + $out = ''; + foreach ($this->loadAllMultiRelations() as $rel) { + $out .= $rel->toConfigString(); + } + + return $out; + } + /** * @return string */ @@ -1247,6 +1280,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer $this->renderArguments(), $this->renderRelatedSets(), $this->renderGroups(), + $this->renderMultiRelations(), $this->renderCustomExtensions(), $this->renderCustomVars(), $this->renderAssignments(), diff --git a/library/Director/Objects/IcingaObjectMultiRelations.php b/library/Director/Objects/IcingaObjectMultiRelations.php index 773d457f..ce8fe5d9 100644 --- a/library/Director/Objects/IcingaObjectMultiRelations.php +++ b/library/Director/Objects/IcingaObjectMultiRelations.php @@ -227,7 +227,8 @@ class IcingaObjectMultiRelations implements Iterator, Countable, IcingaConfigRen protected function getTableName() { - return $this->object->getTableName() . '_' . $this->getPropertyName(); + $class = $this->getRelatedClassName(); + return $this->object->getTableName() . '_' . $class::create()->getShortTableName(); } protected function getRelatedTableName() @@ -282,14 +283,10 @@ class IcingaObjectMultiRelations implements Iterator, Countable, IcingaConfigRen sprintf('r.%s = ro.id', $relationIdCol), '*' )->where( - sprintf('o. = ?', $objectIdCol), - $this->object->object_name + sprintf('r.%s = ?', $objectIdCol), + (int) $this->object->id )->order('ro.object_name'); - // TODO: Test only, remove - echo $query; - exit; - $class = $this->getRelatedClassName(); $this->relations = $class::loadAll($connection, $query, 'object_name'); $this->cloneStored(); @@ -362,9 +359,9 @@ class IcingaObjectMultiRelations implements Iterator, Countable, IcingaConfigRen return $this->db; } - public static function loadForStoredObject(IcingaObject $object) + public static function loadForStoredObject(IcingaObject $object, $propertyName, $relatedObjectClass) { - $relations = new static($object); + $relations = new static($object, $propertyName, $relatedObjectClass); return $relations->loadFromDb(); } @@ -376,7 +373,7 @@ class IcingaObjectMultiRelations implements Iterator, Countable, IcingaConfigRen return ''; } - return c::renderKeyValue('groups', c::renderArray($relations)); + return c::renderKeyValue($this->propertyName, c::renderArray($relations)); } public function __toString()