IcingaObjectMultiRelations: fix failing tests

This commit is contained in:
Thomas Gelf 2016-03-16 21:48:02 +01:00
parent b188b9c18d
commit 05f745c3dc

View File

@ -2,6 +2,7 @@
namespace Icinga\Module\Director\Objects; namespace Icinga\Module\Director\Objects;
use Exception;
use Icinga\Exception\ProgrammingError; use Icinga\Exception\ProgrammingError;
use Iterator; use Iterator;
use Countable; use Countable;
@ -90,7 +91,11 @@ class IcingaObjectMultiRelations implements Iterator, Countable, IcingaConfigRen
public function set($relation) public function set($relation)
{ {
if (! is_array($relation)) { if (! is_array($relation)) {
$relation = array($relation); if ($relation === null) {
$relation = array();
} else {
$relation = array($relation);
}
} }
$existing = array_keys($this->relations); $existing = array_keys($this->relations);
@ -98,17 +103,17 @@ class IcingaObjectMultiRelations implements Iterator, Countable, IcingaConfigRen
$class = $this->getRelatedClassName(); $class = $this->getRelatedClassName();
$unset = array(); $unset = array();
foreach ($relation as $k => $g) { foreach ($relation as $k => $ro) {
if ($g instanceof $class) { if ($ro instanceof $class) {
$new[] = $g->object_name; $new[] = $ro->object_name;
} else { } else {
if (empty($g)) { if (empty($ro)) {
$unset[] = $k; $unset[] = $k;
continue; continue;
} }
$new[] = $g; $new[] = $ro;
} }
} }
@ -180,26 +185,24 @@ class IcingaObjectMultiRelations implements Iterator, Countable, IcingaConfigRen
} elseif (is_string($relation)) { } elseif (is_string($relation)) {
$connection = $this->object->getConnection(); $connection = $this->object->getConnection();
// TODO: fix this, prefetch or whatever - this is expensive try {
$query = $this->getDb()->select()->from( $relation = $class::load($relation, $connection);
$this->getRelatedTableName() } catch (Exception $e) {
)->where('object_name = ?', $relation);
$relations = $class::loadAll($connection, $query, 'object_name');
if (! array_key_exists($relation, $relations)) {
switch ($onError) { switch ($onError) {
case 'autocreate': case 'autocreate':
$relations[$relation] = $class::create(array( $relation = $class::create(array(
'object_type' => 'object', 'object_type' => 'object',
'object_name' => $relation 'object_name' => $relation
)); ));
$relations[$relation]->store($connection); $relation->store($connection);
// TODO // TODO
case 'fail': case 'fail':
throw new ProgrammingError( throw new ProgrammingError(
'The related %s "%s" doesn\'t exists.', 'The related %s "%s" doesn\'t exists: %s',
$this->getRelatedTableName(), $this->getRelatedTableName(),
$relation $relation,
$e->getMessage()
); );
break; break;
case 'ignore': case 'ignore':
@ -213,7 +216,7 @@ class IcingaObjectMultiRelations implements Iterator, Countable, IcingaConfigRen
); );
} }
$this->relations[$relation] = $relations[$relation]; $this->relations[$relation->object_name] = $relation;
$this->modified = true; $this->modified = true;
$this->refreshIndex(); $this->refreshIndex();
@ -310,10 +313,13 @@ class IcingaObjectMultiRelations implements Iterator, Countable, IcingaConfigRen
$toDelete = array_diff($stored, $relations); $toDelete = array_diff($stored, $relations);
foreach ($toDelete as $relation) { foreach ($toDelete as $relation) {
// We work with cloned objects. (why?)
// As __clone drops the id, we need to access original properties
$orig = $this->stored[$relation]->getOriginalProperties();
$where = sprintf( $where = sprintf(
$objectCol . ' = %d AND ' . $relationCol . ' = %d', $objectCol . ' = %d AND ' . $relationCol . ' = %d',
$objectId, $objectId,
$this->stored[$relation]->id $orig['id']
); );
$db->delete( $db->delete(