3

I'm getting errors when using JDatabaseQuery::union() to create a database query.

With Debug disabled I get this at the bottom of the page, presumably when trying to disconnect:

Warning: mysqli::stat(): Couldn't fetch mysqli in... /libraries/joomla/database/driver/mysqli.php on line 219

Warning: mysqli_close(): Couldn't fetch mysqli in... /libraries/joomla/database/driver/mysqli.php on line 226

With Debug enabled I get a further message when the query's being made:

Notice: unserialize(): Error at offset... in... /libraries/joomla/database/query.php on line 1472

That error refers to this line from JDatabaseQuery::__clone():

$this->{$k} = unserialize(serialize($v));

This issue is repeated across different MySQL, PHP 5.x and Joomla versions on XAMPP and Ubuntu Server. I originally discovered it while developing a component, but it shows up on a clean Joomla install with a clean database on a clean server. Caching is disabled, session times at 120mins. To repeat the issue, replace ContentModelArticles::getListQuery() in /administrator/components/com_content/models/articles.php with:

protected function getListQuery()
    {
        // Create a new query object.
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $sub = $db->getQuery(true);

        // Select the required fields from the first table.
        $query->select('id')
            ->from('#__content');

        // Select the required fields from the second table.
        $sub->select('content_id')
            ->from('#__content_rating');

        $query->union($sub);

        return $query;
    }

If I make the subquery a string rather than a JDatabaseQueryMysqli object, however, there are no problems:

protected function getListQuery()
    {
        // Create a new query object.
        $db = $this->getDbo();
        $query = $db->getQuery(true);

        // Select the required fields from the first table.
        $query->select('id')
            ->from('#__content');

        $query->union('SELECT content_id FROM #__content_rating');

        return $query;
    }

If I'm doing anything wrong, it's with the query objects. I've had a look at the relevant docs, found this forum post, and followed the advice to copy the example from /administrator/components/com_finder/helpers/indexer/query.php, which clones and modifies the first query object rather than creating a new one. All to no avail. There's some indication in that forum thread and in other places online that this may be a bug, but everything I've found is fairly old and that bug was apparently addressed.

Is this an issue with Joomla or me?

  • Try die($query->dump()); after the union and compare the results – fruppel Sep 13 '16 at 17:44
1

I've had some weird experiences with Jdatabase and union queries. Sometimes, if I can't manage to get the syntax correct for that, what I do is just something like $query="[write your query manually here]" and then $db->setQuery($query), etc. Probably not much help, but figured I'd add that...

  • You could have gone into a bit more details and given a short code example but this is a workable method. – Terry Carter May 11 '17 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.