diff --git a/library/Icingadb/Model/Behavior/HasRootProblem.php b/library/Icingadb/Model/Behavior/HasRootProblem.php index ed26c88a6..71d0d09a9 100644 --- a/library/Icingadb/Model/Behavior/HasRootProblem.php +++ b/library/Icingadb/Model/Behavior/HasRootProblem.php @@ -29,33 +29,35 @@ public function setQuery(Query $query): self return $this; } - public function rewriteColumn($column, ?string $relation = null) + public function rewriteColumn($column, ?string $relation = null): ?AliasedExpression { - if ($this->isSelectableColumn($column)) { - $path = 'from.dependency_node'; - $subQueryRelation = $relation !== null ? $relation . $path : $path; - $subQuery = $this->query->createSubQuery(new DependencyEdge(), $subQueryRelation) - ->limit(1) - ->columns([new Expression('1')]); + if (! $this->isSelectableColumn($column)) { + return null; + } - $subQuery->getSelectBase()->join( - ['root_dependency' => 'dependency'], - [$subQuery->getResolver()->getAlias($subQuery->getModel()) . '.dependency_id = root_dependency.id'] - )->join( - ['root_dependency_state' => 'dependency_state'], - ['root_dependency.id = root_dependency_state.dependency_id'] - )->where(new Expression("root_dependency_state.failed = 'y'")); + $path = 'from.dependency_node'; + $subQueryRelation = $relation !== null ? $relation . $path : $path; + $subQuery = $this->query->createSubQuery(new DependencyEdge(), $subQueryRelation) + ->limit(1) + ->columns([new Expression('1')]); - $column = $relation !== null ? str_replace('.', '_', $relation) . "_$column" : $column; + $subQuery->getSelectBase()->join( + ['root_dependency' => 'dependency'], + [$subQuery->getResolver()->getAlias($subQuery->getModel()) . '.dependency_id = root_dependency.id'] + )->join( + ['root_dependency_state' => 'dependency_state'], + ['root_dependency.id = root_dependency_state.dependency_id'] + )->where(new Expression("root_dependency_state.failed = 'y'")); - $alias = $this->query->getDb()->quoteIdentifier([$column]); + $column = $relation !== null ? str_replace('.', '_', $relation) . "_$column" : $column; - list($select, $values) = $this->query->getDb() - ->getQueryBuilder() - ->assembleSelect($subQuery->assembleSelect()); + $alias = $this->query->getDb()->quoteIdentifier([$column]); - return new AliasedExpression($alias, "($select)", null, ...$values); - } + list($select, $values) = $this->query->getDb() + ->getQueryBuilder() + ->assembleSelect($subQuery->assembleSelect()); + + return new AliasedExpression($alias, "($select)", null, ...$values); } public function isSelectableColumn(string $name): bool