joomla-framework / database

Joomla Framework Database Package
GNU General Public License v2.0
28 stars 34 forks source link

[2.0] setQuery calls free_result on the statement #251

Closed HLeithner closed 3 years ago

HLeithner commented 3 years ago

It's not possible to use an iterator query and a normal at the same time.

Issue: https://github.com/joomla/joomla-cms/issues/34696

Steps to reproduce the issue

Sample code from joomla issue:

public function checkDatabase()
{
    $db    = $this->getDbo();

    $query = $db->getQuery(true)
        ->select($db->quoteName('id'))
        ->from($db->quoteName('#__content'));
    $db->setQuery($query);
    $records = $db->getIterator();
    $items = $records->count();

    $query->clear()
        ->select($db->quoteName('id'))
        ->from($db->quoteName('#__categories'));
    $db->setQuery($query);
    $db->loadResult();

    return $items === $records->count();
}

Expected result

returns true

Actual result

returns false

System information (as much as possible)

mysql

Additional comments

Works in Joomla 3

The reason is that setQuery closes the old statement:

https://github.com/joomla-framework/database/blob/2.0-dev/src/DatabaseDriver.php#L1848 https://github.com/joomla-framework/database/blob/2.0-dev/src/DatabaseDriver.php#L777

richard67 commented 3 years ago

Possible solution see https://github.com/joomla/joomla-cms/issues/34696#issuecomment-879100523 .