0

I'm using TableGateway's selectWith function to return a HydratingResultSet of entities. I need to iterate through each of the entities of the result set - not sure how I'm to do it, but using a foreach gives the error "This result is a forward only result set, calling rewind() after moving forward is not supported".

What I was trying to do is basically:

$res = $this->tableGateway->selectWith($query);
foreach($res as $r) {...} 

What am I doing wrong? We're using Zend Framework 2.3. Thanks in advance!

riyunoa
  • 205
  • 1
  • 9
  • Might this be a solution to your problem? Check the edit. http://stackoverflow.com/questions/18567219/this-result-is-a-forward-only-result-set-calling-rewind-after-moving-forward – Mernayi Nov 05 '14 at 08:34

2 Answers2

2

You have to buffer() result before itarate or get an array - toArray()

$res = $this->tableGateway->selectWith($query);

$res->buffer(); // you need to buffer result first
//$res = $res->toArray(); // or transform resultset to array but I prefere more to buffer

foreach($res as $r) {...} // first iterate 
foreach($res as $r) {...} // second iterate 

but be careful this isn't advisable on the large results.

tasmaniski
  • 4,767
  • 3
  • 33
  • 65
0

You are using $res before reaching the foreach loop...

HydratingResultSet is forward only result set(data has not been loaded from the database) and once you iterate throw it , you can't do it again.

to use the Resultset multiple times, you need to load all the data at once. $res->buffer() or $res = $res->toArray()

what code do you have between $res and foreach ???

edigu
  • 9,878
  • 5
  • 57
  • 80
Exlord
  • 5,009
  • 4
  • 31
  • 51
  • Thanks for your reply. That's the weird thing though. I have absolutely nothing between the selectWith and the foreach! It doesn't make sense. – riyunoa Nov 05 '14 at 22:07
  • @riyunoa are u 100% sure that `$res` has never been used before reaching `foreach` ? can you post the full code from starting of `select`? – Exlord Nov 06 '14 at 05:14