Using JDatabaseQuery::union() causes errors - bug or stupidity?

44 views
Skip to first unread message

Gloop

unread,
Aug 5, 2016, 4:24:16 PM8/5/16
to Joomla! General Development
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?

Walt Sorensen aka photodude

unread,
Aug 6, 2016, 11:14:57 AM8/6/16
to Joomla! General Development
At a quick glance, is there a reason you are using a union rather than join?

"UNION combines the results of two or more queries into a single result set that includes all the rows that belong to all queries in the union. By using JOINs, you can retrieve data from two or more tables based on logical relationships between the tables"

Gloop

unread,
Aug 8, 2016, 11:29:40 PM8/8/16
to Joomla! General Development
It was a dirty, temporary paste of some data from an old component I wanted to test with a new one during a Frankenbranch. Same number of columns, same datatypes, just different names. It was the quickest, laziest method at the time.

I ended up getting far too sidetracked trying to work out why it wouldn't work.
Reply all
Reply to author
Forward
0 new messages