IcingaObjectMultiRelations: fix store and render

This commit is contained in:
Thomas Gelf 2016-03-16 13:53:34 +01:00
parent f78c858fe7
commit c7bf68a70a
2 changed files with 46 additions and 15 deletions

View File

@ -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(),

View File

@ -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()