Исправление метода union класса JDatabaseQuery
В этом методе допущена ошибка, по этому он не работает вплоть до версии joomla 3.3.
Для исправления этого бага надо создать потомок класса JDatabaseQuery, переопределить в нем методы __toString и union.
Вот собственно код переопределения класса.
class JDatabaseQueryUnion extends JDatabaseQuery { /** * Magic function to convert the query to a string. * * @return string The completed query. * * @since 11.1 */ public function __toString() { $query = ''; switch ($this->type) { case 'element': $query .= (string) $this->element; break; case 'select': $query .= (string) $this->select; $query .= (string) $this->from; if ($this->join) { // special case for joins foreach ($this->join as $join) { $query .= (string) $join; } } if ($this->where) { $query .= (string) $this->where; } if ($this->group) { $query .= (string) $this->group; } if ($this->having) { $query .= (string) $this->having; } if ($this->order) { $query .= (string) $this->order; } break; case 'union': $query .= (string) $this->union; if ($this->order) { $query .= (string) $this->order; } break; case 'delete': $query .= (string) $this->delete; $query .= (string) $this->from; if ($this->join) { // special case for joins foreach ($this->join as $join) { $query .= (string) $join; } } if ($this->where) { $query .= (string) $this->where; } break; case 'update': $query .= (string) $this->update; if ($this->join) { // special case for joins foreach ($this->join as $join) { $query .= (string) $join; } } $query .= (string) $this->set; if ($this->where) { $query .= (string) $this->where; } break; case 'insert': $query .= (string) $this->insert; // Set method if ($this->set) { $query .= (string) $this->set; } // Columns-Values method elseif ($this->values) { if ($this->columns) { $query .= (string) $this->columns; } $query .= ' VALUES '; $query .= (string) $this->values; } break; } return $query; } /** * Add a query to UNION with the current query. * Multiple unions each require separate statements and create an array of unions. * * Usage: * $query->union('SELECT name FROM #__foo') * $query->union('SELECT name FROM #__foo','distinct') * $query->union(array('SELECT name FROM #__foo','SELECT name FROM #__bar')) * * @param mixed $query The JDatabaseQuery object or string to union. * @param boolean $distinct True to only return distinct rows from the union. * @param string $glue The glue by which to join the conditions. * * @return mixed The JDatabaseQuery object on success or boolean false on failure. * * @since 12.1 */ public function union($query, $distinct = false, $glue = '') { $this->type = 'union'; // Clear any ORDER BY clause in UNION query // See http://dev.mysql.com/doc/refman/5.0/en/union.html if (!is_null($this->order)) { $this->clear('order'); } // Set up the DISTINCT flag, the name with parentheses, and the glue. if ($distinct) { $name = '()'; $glue = ')' . PHP_EOL . 'UNION ('; } else { $glue = ')' . PHP_EOL . 'UNION ALL('; $name = '()'; } // Get the JDatabaseQueryElement if it does not exist if (is_null($this->union)) { $this->union = new JDatabaseQueryElement($name, $query, "$glue"); } // Otherwise append the second UNION. else { $glue = ''; $this->union->append($query); } return $this; } }
Для создания объекта этого класса в коде надо воспользоваться следующей конструкцией.
// Create a new query object. $db = $this->getDbo(); $query = New JDatabaseQueryUnion($db);