Issues

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

Issue Type: Bug Created: 2012-08-20T20:20:27.000+0000 Last Updated: 2012-10-08T20:14:56.000+0000 Status: Closed Fix version(s): Reporter: Rob Allen (rob) Assignee: Ralph Schindler (ralph) Tags: - Zend\Db

Related issues: Attachments:

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

Posted by Maks 3w (maks3w) on 2012-08-28T22:49:38.000+0000

Fixed with PR https://github.com/zendframework/zf2/pull/2247

Posted by Ricardo Maia (ricardomaia) on 2012-09-02T06:39:05.000+0000

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"

<pre class="highlight">
$table = new BooksTable($adapter);
$resultSet = $table->fetchAll();

$current = $resultSet->current();

echo "After \$resultSet->current() <br></br>";
echo "Current key: {$resultSet->key()} <br></br>";
echo "Current value: {$current->title} <br></br>";

$resultSet->next();
$current = $resultSet->current();

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

$resultSet->rewind();
$current = $resultSet->current();

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

$resultSet->next();
$current = $resultSet->current();

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

$resultSet->next();
$current = $resultSet->current();

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

$resultSet->rewind();
$current = $resultSet->current();

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



<pre class="highlight">
  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.

Posted by Koen Pieters (koen) on 2012-09-07T13:27:26.000+0000

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:

<pre class="highlight">
$table = new BooksTable($adapter);
$resultSet = $table->fetchAll();

$resultSet->buffer();

$current = $resultSet->current();

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

Posted by Ralph Schindler (ralph) on 2012-10-08T20:14:56.000+0000

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

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts