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) private function loadMultiRelation($property)
{ {
$this->loadedMultiRelations[$property] = new IcingaObjectMultiRelations( if ($this->hasBeenLoadedFromDb()) {
$this, $rel = IcingaObjectMultiRelations::loadForStoredObject(
$property, $this,
$this->multiRelations[$property] $property,
); $this->multiRelations[$property]
);
} else {
$rel = new IcingaObjectMultiRelations(
$this,
$property,
$this->multiRelations[$property]
);
}
$this->loadedMultiRelations[$property] = $rel;
} }
private function hasLoadedMultiRelation($property) private function hasLoadedMultiRelation($property)
@ -938,6 +948,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
$this $this
->storeCustomVars() ->storeCustomVars()
->storeGroups() ->storeGroups()
->storeMultiRelations()
->storeImports() ->storeImports()
->storeRanges() ->storeRanges()
->storeRelatedSets() ->storeRelatedSets()
@ -979,6 +990,15 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
return $this; return $this;
} }
protected function storeMultiRelations()
{
foreach ($this->loadedMultiRelations as $rel) {
$rel->store();
}
return $this;
}
protected function storeRanges() protected function storeRanges()
{ {
if ($this->supportsRanges()) { 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 * @return string
*/ */
@ -1247,6 +1280,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
$this->renderArguments(), $this->renderArguments(),
$this->renderRelatedSets(), $this->renderRelatedSets(),
$this->renderGroups(), $this->renderGroups(),
$this->renderMultiRelations(),
$this->renderCustomExtensions(), $this->renderCustomExtensions(),
$this->renderCustomVars(), $this->renderCustomVars(),
$this->renderAssignments(), $this->renderAssignments(),

View File

@ -227,7 +227,8 @@ class IcingaObjectMultiRelations implements Iterator, Countable, IcingaConfigRen
protected function getTableName() protected function getTableName()
{ {
return $this->object->getTableName() . '_' . $this->getPropertyName(); $class = $this->getRelatedClassName();
return $this->object->getTableName() . '_' . $class::create()->getShortTableName();
} }
protected function getRelatedTableName() protected function getRelatedTableName()
@ -282,14 +283,10 @@ class IcingaObjectMultiRelations implements Iterator, Countable, IcingaConfigRen
sprintf('r.%s = ro.id', $relationIdCol), sprintf('r.%s = ro.id', $relationIdCol),
'*' '*'
)->where( )->where(
sprintf('o. = ?', $objectIdCol), sprintf('r.%s = ?', $objectIdCol),
$this->object->object_name (int) $this->object->id
)->order('ro.object_name'); )->order('ro.object_name');
// TODO: Test only, remove
echo $query;
exit;
$class = $this->getRelatedClassName(); $class = $this->getRelatedClassName();
$this->relations = $class::loadAll($connection, $query, 'object_name'); $this->relations = $class::loadAll($connection, $query, 'object_name');
$this->cloneStored(); $this->cloneStored();
@ -362,9 +359,9 @@ class IcingaObjectMultiRelations implements Iterator, Countable, IcingaConfigRen
return $this->db; 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(); return $relations->loadFromDb();
} }
@ -376,7 +373,7 @@ class IcingaObjectMultiRelations implements Iterator, Countable, IcingaConfigRen
return ''; return '';
} }
return c::renderKeyValue('groups', c::renderArray($relations)); return c::renderKeyValue($this->propertyName, c::renderArray($relations));
} }
public function __toString() public function __toString()