0

Hit a brick wall with this now. I have a query, with some joins so running it through this (TableGateway will not play with fields from other tables):

$sql = new Sql($adapter);
$select = getMySelect();

echo $select->getSqlString($this->adapter->getPlatform());
//ok - this, if I run it on the database MYSELF, yields 2 of rows with the current data. Correct.

$statement = $this->adapter->query($select->getSqlString($this->adapter->getPlatform())); 

$res = $statement->execute();
$resultSet = new ResultSet;
$resultSet->initialize($res);

echo $resultSet->count(); // yields 2. Correct, but wait...

foreach($resultSet as $row)
{
   //do stuff with $row .... but only happens once! wtf.
}

print_r($resultSet->toArray()); //only one array element, with nested row data - wtf.

So, in short. The sql, run through MySQL manually, gives two rows (or more, if I add things into the db) . The Zend\Db\ResultSet itself reports that it has two rows. But it won't yield them both to a foreach loop.

I don't get it. Is there some ZendFrameworky2 magic dance I missed out? Just giving it one more chance before going back to trusty plain ol' PDO.

1 Answer 1

2

I tracked the problem down to the tablegateway and the example code used by Zend for checking that there is a valid result set.

$row = $resultSet->current();
if (!$row) {
    throw new Exception("no row found");
}
return $resultSet;

which has already moved the pointer in the result set to index 1 by getting the current row, which then becomes the starting point for the foreach.

To fix, change to

$row = $resultSet->count();
if (!$row) {
    throw new Exception("no row found");
}
return $resultSet;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.