ZF2-482: Unable to iterate over a database ResultSet twice

Description

When you try to iterate over a result set a second time, you get an exception:

This result is a forward only result set, calling rewind() after moving forward is not supported' in /www/zendframework/zf2/library/Zend/Db/Adapter/Driver/Pdo/Result.php on line 149

Comments

I see the same error here. That issue persists, I believe. I got a fresh copy from Git master repository, downloaded in september 2.

I made tests with a database like this:

1;"The Tell-Tale Heart" 2;"Little Women" 3;"The Velveteen Rabbit" 4;"Goodnight Moon" 5;"Bartholomew and the Oobleck" 6;"The Cat in the Hat" 7;"Dynamic Anatomy" 8;"2001: A Space Odyssey" 9;"Dune" 10;"The Shining" 11;"Franklin in the Dark" 12;"Practical PostgreSQL" 13;"Programming Python" 14;"Learning Python" 15;"Perl Cookbook"


$table = new BooksTable($adapter);
$resultSet = $table->fetchAll();

$current = $resultSet->current();

echo "After \$resultSet->current() 
"; echo "Current key: {$resultSet->key()}
"; echo "Current value: {$current->title}
"; $resultSet->next(); $current = $resultSet->current(); echo "After \$resultSet->next()
"; echo "Current key: {$resultSet->key()}
"; echo "Current value: {$current->title}
"; $resultSet->rewind(); $current = $resultSet->current(); echo "After \$resultSet->rewind()
"; echo "Current key: {$resultSet->key()}
"; echo "Current value: {$current->title}
"; $resultSet->next(); $current = $resultSet->current(); echo "After \$resultSet->next()
"; echo "Current key: {$resultSet->key()}
"; echo "Current value: {$current->title}
"; $resultSet->next(); $current = $resultSet->current(); echo "After \$resultSet->next()
"; echo "Current key: {$resultSet->key()}
"; echo "Current value: {$current->title}
"; $resultSet->rewind(); $current = $resultSet->current(); echo "After \$resultSet->next()
"; echo "Current key: {$resultSet->key()}
"; echo "Current value: {$current->title}
";

  After $resultSet->current()Current key:  
  Current value: The Tell-Tale Heart 
  After $resultSet->next()Current key: 1 
  Current value: Little Women 
  After $resultSet->rewind()Current key: 0 
  Current value: The Velveteen Rabbit 
  After $resultSet->next()Current key: 1 
  Current value: Goodnight Moon 
  After $resultSet->next()Current key: 2 
  Current value: Bartholomew and the Oobleck 

  Fatal error: Uncaught exception 'Zend\Db\Adapter\Exception\RuntimeException' with message 'This result is a forward only result set, calling rewind() after moving forward is not supported' in /home/xxxxxxxxxxxxx/zend-framework/zf2/library/Zend/Db/Adapter/Driver/Pdo/Result.php on line 159

  Zend\Db\Adapter\Exception\RuntimeException: This result is a forward only result set, calling rewind() after moving forward is not supported in /home/xxxxxxxxxxxxx/zend-framework/zf2/library/Zend/Db/Adapter/Driver/Pdo/Result.php on line 159

  Call Stack:
      0.0003     349320   1. {main}() /var/www/zf-2.0.0rc7/index.php:0
      0.1675    1595740   2. Zend\Db\ResultSet\AbstractResultSet->rewind() /var/www/zf-2.0.0rc7/index.php:85
      0.1675    1595740   3. Zend\Db\Adapter\Driver\Pdo\Result->rewind() /home/xxxxxxxxxxxxx/zend-framework/zf2/library/Zend/Db/ResultSet/AbstractResultSet.php:228

At the first time the key value is NULL. So, changes on keys almost working, but the values are moving forward only, aparently caused by cursor settings.

I've did a try to fix this error when you use a buffer. That also still had an error in it.

You have to change your test code to:


$table = new BooksTable($adapter);
$resultSet = $table->fetchAll();

$resultSet->buffer();

$current = $resultSet->current();

pull request: https://github.com/zendframework/zf2/pull/2314

This issue has been closed on Jira and moved to GitHub for issue tracking. To continue following the resolution of this issues, please visit: https://github.com/zendframework/zf2/issues/2532