Get and clone a database cursor. Iterate through the cursor one row at a time and yield() the Model object loaded with that record.
This acts similar to the forward-only iterator of FOF 2.x which allowed us to do a forward-only iteration of datasets too large to fit into memory. Unlike the FOF 2.x way, using a Generator does not require us to have a forward-only Iterator class. We can implement this with just a simple method.
Things to consider
Does that cause problems with PDO if we have multiple cursors open at the same time?
Should I fork the DB connection when using a generator?
What happens if the Generator is abandoned, e.g. I use a break in the foreach that loops through it? Do I have zombie database cursors?
PHP 5.5+ feature. Throws RuntimeException in older versions of PHP.
Reference: Generators
Get and clone a database cursor. Iterate through the cursor one row at a time and yield() the Model object loaded with that record.
This acts similar to the forward-only iterator of FOF 2.x which allowed us to do a forward-only iteration of datasets too large to fit into memory. Unlike the FOF 2.x way, using a Generator does not require us to have a forward-only Iterator class. We can implement this with just a simple method.
Things to consider
Does that cause problems with PDO if we have multiple cursors open at the same time? Should I fork the DB connection when using a generator? What happens if the Generator is abandoned, e.g. I use a
break
in theforeach
that loops through it? Do I have zombie database cursors?