IcingaObject: add new static helper, expose...

...existing one

refs #842
This commit is contained in:
Thomas Gelf 2017-03-13 22:03:02 +01:00
parent a36ed2c23d
commit cb9fff90ef
1 changed files with 28 additions and 1 deletions

View File

@ -2214,7 +2214,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
}
}
protected static function classByType($type)
public static function classByType($type)
{
// allow for icinga_host and host
$type = lcfirst(preg_replace('/^icinga_/', '', $type));
@ -2306,6 +2306,33 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
}
}
/**
* @param $type
* @param Db $db
* @return IcingaObject[]
* @throws ProgrammingError
*/
public static function loadAllExternalObjectsByType($type, Db $db)
{
/** @var IcingaObject $class */
$class = self::classByType($type);
$dummy = $class::create();
if (is_array($dummy->getKeyName())) {
throw new ProgrammingError(
'There is no support for loading external objects of type "%s"',
$type
);
} else {
$query = $db->getDbAdapter()
->select()
->from($dummy->getTableName())
->where('object_type = ?', 'external_object');
return $class::loadAll($db, $query, 'object_name');
}
}
public static function fromJson($json, Db $connection = null)
{
return static::fromPlainObject(json_decode($json), $connection);