diff --git a/dblayeroo.php b/dblayeroo.php index 4b670ec..1c65b34 100644 --- a/dblayeroo.php +++ b/dblayeroo.php @@ -1577,6 +1577,12 @@ class dblayeroo // expression, if not already done if ($this->groupByExpression === null) { + // Set to empty array to demonstrate that a GROUP BY is needed, but + // there is no already defined displayed Columns. + // Used if the group by is called from join object + if ($this->displayColumn === null) + $this->groupByExpression = array (); + else $this->groupByExpression = $this->displayColumn; } if ($func === "GROUP_CONCAT" && ($pos = strpos ($name, ",'"))) @@ -2099,11 +2105,35 @@ class dblayeroo public function groupByGet ($full=false) /* {{{ */ { - $groupBy = array (); + $groupBy = null; if ($this->joinObject) $full = true; + if ($this->joinObject) + { + foreach ($this->joinObject as $obj) + { + $ext = $obj->groupByGet (true); + if ($ext !== null) + { + if (! is_array ($groupBy)) + $groupBy = array (); + $groupBy = array_merge ($groupBy, $ext); + } + } + + } + // If there is some GROUP BY in join Object, but not in this object, all + // the displayed fields must be added to be added in the GROUP BY + if (is_array ($groupBy) && + (is_null ($this->groupByExpression) || + (is_array ($this->groupByExpression) && + empty ($this->groupByExpression)) + )) + $this->groupByExpression = $this->displayColumn; if (is_array ($this->groupByExpression)) { + if (! is_array ($groupBy)) + $groupBy = array (); foreach ($this->groupByExpression as $pos=>$o) { if ($full !== false) @@ -2113,16 +2143,8 @@ class dblayeroo $groupBy[$pos] = $o; } } - if ($this->joinObject) - { - foreach ($this->joinObject as $obj) - { - $ext = $obj->groupByGet (true); - if ($ext !== null) - $groupBy = array_merge ($groupBy, $obj->groupByGet (true)); - } - } - ksort ($groupBy, SORT_NATURAL); + if (is_array ($groupBy)) + ksort ($groupBy, SORT_NATURAL); return $groupBy; } /* }}} */