akeeba / fof

Rapid Application Development framework for Joomla!™ 3 and 4
0 stars 0 forks source link

Generator in DataModel #613

Closed nikosdion closed 6 years ago

nikosdion commented 8 years ago

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 the foreach that loops through it? Do I have zombie database cursors?

nikosdion commented 6 years ago

Already completed in 3.3. It seems to work quite reliably when iterating thousands of records.