Commit 689200e4 authored by Ian Jenkins's avatar Ian Jenkins

Add rewindable generator in order fix the issue with mutliple iterations.

Only added to mysqli for now, can add to other drivers (assuming they
support reseting the pointer in some way) after feedback.
parent a6af8a7e
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
namespace Doctrine\DBAL\Driver\Mysqli; namespace Doctrine\DBAL\Driver\Mysqli;
use Doctrine\DBAL\Driver\Statement; use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\RewindableGenerator;
use PDO; use PDO;
/** /**
...@@ -406,8 +407,11 @@ class MysqliStatement implements \IteratorAggregate, Statement ...@@ -406,8 +407,11 @@ class MysqliStatement implements \IteratorAggregate, Statement
*/ */
public function getIterator() public function getIterator()
{ {
while ($row = $this->fetch()) { return new RewindableGenerator(function() {
yield $row; $this->_stmt->data_seek(0);
} while ($row = $this->fetch()) {
yield $row;
}
});
} }
} }
<?php
namespace Doctrine\DBAL;
class RewindableGenerator implements \IteratorAggregate
{
private $generator;
/**
* @param callable $generator
*/
public function __construct(callable $generator)
{
$this->generator = $generator;
}
public function getIterator()
{
$g = $this->generator;
return $g();
}
}
...@@ -879,7 +879,7 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -879,7 +879,7 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase
} }
$j = 0; $j = 0;
foreach ($stmt as $row) { foreach ($stmt as $row2) {
$j++; $j++;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment