diff --git a/library/Icinga/Data/Db/DbQuery.php b/library/Icinga/Data/Db/DbQuery.php index 94765d98f..56118edcf 100644 --- a/library/Icinga/Data/Db/DbQuery.php +++ b/library/Icinga/Data/Db/DbQuery.php @@ -72,6 +72,12 @@ class DbQuery extends SimpleQuery parent::init(); } + public function setUseSubqueryCount($useSubqueryCount = true) + { + $this->useSubqueryCount = $useSubqueryCount; + return $this; + } + public function where($condition, $value = null) { // $this->count = $this->select = null; @@ -259,7 +265,7 @@ class DbQuery extends SimpleQuery */ public function getCountQuery() { - // TODO: there may be situations where we should clone the "select" + // TODO: there may be situations where we should clone the "select" $count = $this->dbSelect(); $this->applyFilterSql($count); diff --git a/library/Icinga/Data/PivotTable.php b/library/Icinga/Data/PivotTable.php index ab6fab13d..0a1f009fe 100644 --- a/library/Icinga/Data/PivotTable.php +++ b/library/Icinga/Data/PivotTable.php @@ -69,11 +69,13 @@ class PivotTable protected function prepareQueries() { $this->xAxisQuery = clone $this->baseQuery; - $this->xAxisQuery->distinct(); + $this->xAxisQuery->group($this->xAxisColumn); $this->xAxisQuery->columns(array($this->xAxisColumn)); + $this->xAxisQuery->setUseSubqueryCount(); $this->yAxisQuery = clone $this->baseQuery; - $this->yAxisQuery->distinct(); + $this->yAxisQuery->group($this->yAxisColumn); $this->yAxisQuery->columns(array($this->yAxisColumn)); + $this->yAxisQuery->setUseSubqueryCount(); return $this; } @@ -85,24 +87,14 @@ class PivotTable */ protected function adjustSorting() { - $currentOrderColumns = $this->baseQuery->getOrder(); - $xAxisOrderColumns = array(array($this->baseQuery->getMappedField($this->xAxisColumn), SimpleQuery::SORT_ASC)); - $yAxisOrderColumns = array(array($this->baseQuery->getMappedField($this->yAxisColumn), SimpleQuery::SORT_ASC)); - - foreach ($currentOrderColumns as $orderInfo) { - if ($orderInfo[0] === $xAxisOrderColumns[0][0]) { - $xAxisOrderColumns[0] = $orderInfo; - } elseif ($orderInfo[0] === $yAxisOrderColumns[0][0]) { - $yAxisOrderColumns[0] = $orderInfo; - } else { - $xAxisOrderColumns[] = $orderInfo; - $yAxisOrderColumns[] = $orderInfo; - } + if (false === $this->xAxisQuery->hasOrder($this->xAxisColumn)) { + $this->xAxisQuery->order($this->xAxisColumn, 'ASC'); } -//TODO: simplify this whole function. No need to care about mapping -// foreach ($xAxisOrderColumns as -// $this->xAxisQuery->setOrder($xAxisOrderColumns); -// $this->yAxisQuery->setOrder($yAxisOrderColumns); + + if (false === $this->yAxisQuery->hasOrder($this->yAxisColumn)) { + $this->yAxisQuery->order($this->yAxisColumn, 'ASC'); + } + return $this; }