Unable to iterate over a database ResultSet twice #2532
Comments
(Originally posted by: maks3w on 08/28/12) Fixed with PR #2247 |
(Originally posted by: ricardomaia on 09/02/12) I see the same error here. That issue persists, I believe. I made tests with a database like this: 1;"The Tell-Tale Heart"
At the first time the key value is NULL. |
(Originally posted by: Koen Pieters on 09/07/12) 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:
pull request: #2314 |
This issue was ported from the ZF2 Jira Issue Tracker at Known GitHub users mentioned in the original message or comment: |
I get this same error with a \Zend\Db\ResultSet\ResultSet initialized with a PDO statement running ZF2 at commit 700e799. I am not sure how to mock the statement itself, but here is a test case that should fail with any database table. public function testResultSetDoubleIteration()
{
$dbName = 'mydb';
$dbHost = 'localhost';
$dbUsername = 'myusername';
$dbPassword = 'mypassword';
$table = 'mytable';
$connection = new \Zend\Db\Adapter\Driver\Pdo\Connection(array(
'driver' => 'pdo',
'dsn' => 'mysql:dbname=' . $dbName . ';host=' . $dbHost,
'database' => $dbName,
'username' => $dbUsername,
'password' => $dbPassword,
'hostname' => $dbHost,
));
$driver = new \Zend\Db\Adapter\Driver\Pdo\Pdo($connection);
$adapter = new \Zend\Db\Adapter\Adapter($driver);
$sql = new \Zend\Db\Sql\Sql($adapter, $table);
$select = $sql->select();
$statement = $sql->prepareStatementForSqlObject($select);
$resultSet = new \Zend\Db\ResultSet\ResultSet();
$resultSet->initialize($statement->execute());
foreach ($resultSet as $result) {
}
foreach ($resultSet as $result) {
}
} The error should be as follows:
/var/www/sites/example.com/vendor/ZF2/library/Zend/Db/Adapter/Driver/Pdo/Result.php:159 |
Once you get a |
Please reopen if its still an issue. |
Reopening as per request of Jesse Taylor. |
I'm still getting the same exception after updating to ZF2.1 - I have tried $resultSet->buffer(); eg: in my table class: $statement = $this->adapter->createStatement($sql); My table class is Extending AbstractTableGateway eg:
config:
|
What driver are you using, @jessertaylor? |
hi @ralphschindler, 'driver' => 'pdo', |
pdo_?, but what platform, postgres? |
mysql |
Same problem here (also with MySQL). \Zend\Db\Adapter\Driver\Pdo\Result Haven't iterated through it....just giving the ResultSet directly to a view and then tried to do "foreach" loop. |
$sql = new \Zend\Db\Sql\Sql($this->getServiceLocator()->get('dbLisp'));
$select = $sql->select();
// $select = new \Zend\Db\Sql\Select();
$select->from(array(
'mpp' => 'macMachineProductOperation'
));
$select->join(array(
'pro' => 'macProduct'
), 'pro.proId = mpp.macProduct_proId');
$select->order('pro.proNumber');
$select->order('mpp.mppOperation');
$where = new \Zend\Db\Sql\Where();
$where->equalTo('mpp.macMachine_macId', (int) $dataMachine->macId);
$where->equalTo('mpp.mppDeleted', 'n');
$select->where($where);
$statement = $sql->prepareStatementForSqlObject($select);
$result = $statement->execute(); (Also tried with "buffer()" |
you can use $resultSet->buffer(); |
Same result: This result is a forward only result set, calling rewind() after moving forward is not supported Even if that would work....this is a little bit to much, to just iterate through a result in a view.... |
The Result object cannot perform buffering if the underlying driver does not support it, in that case, what you want is Zend\Db\ResultSet (userland) buffering. There are two ways to buffer result sets:
$dbconfig = array(
'driver' => 'Mysqli',
'hostname' => 'localhost',
'username' => 'developer',
'password' => 'developer',
'database' => 'zend_db_example',
'options' => array('buffer_results' => true)
);
break;
|
getting same exception, from rewind() function of if ( $this->statementMode == self::STATEMENT_MODE_FORWARD && $this->position > 0) { tried both options for, buffer_results = true |
The way ralph posted it works. I've solved it "indirectly" over a fetch abstraction (like it was in ZF1). I just fetch the result complete and have it in an array. |
@bridge-rabish buffering is a feature of Zend\Db\ResultSet\ResultSet, which you wrap a driver Result with. $rs = new Zend\Db\ResultSet\ResultSet();
$rs->initialize($zendDbPdoDriverResult);
$rs->buffer(); // if you must
foreach ($rs as $r) {}
foreach ($rs as $r) {}
foreach ($rs as $r) {} I am closing this as its not an issue. |
For paginatorAdapter, you should add buffering to the Iterator, eg. //buffering set for 'This result is a forward only result set, calling rewind() after moving forward is not supported ' |
@ralphschindler , I use the PDO/MySQL driver and buffer does not work. I checked the code and noticed that the buffer method does nothing in the Pdo Result file:
Is it something that should be implanted or I need to pass some kind of flag? |
Jira Information
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
The text was updated successfully, but these errors were encountered: