Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would use an Iterator that iterates over another Iterator and asks for a next Iterator once it reaches the end of the previous Iterator... ok, sounds a mo complicated than it actually is:</p> <pre><code>&lt;?php $limit = 100; $offset = 0; $iter = new NextIteratorCallbackIterator(function($i) use ($orm, $limit, &amp;$offset) { printf("selecting next bunch at offset %d\n", $offset); $recs = $orm-&gt;select($filter, $sorting, $limit , $offset); $offset += $limit; if ($recs) { return new ArrayIterator($recs); } return null; // end reached }); foreach ($iter as $rec) { // do something with record } ?&gt; </code></pre> <p>And here is a sample Implementation of that NextIteratorCallbackIterator:</p> <pre><code>&lt;?php class NextIteratorCallbackIterator implements Iterator { private $_iterator = null; private $_count = 0; private $_callback; public function __construct($callback) { if (!is_callable($callback)) { throw new Exception(__CLASS__.": callback must be callable"); } $this-&gt;_callback = $callback; } public function current() { return $this-&gt;_iterator !== null ? $this-&gt;_iterator-&gt;current() : null; } public function key() { return $this-&gt;_iterator !== null ? $this-&gt;_iterator-&gt;key() : null; } public function next() { $tryNext = ($this-&gt;_iterator === null); do { if ($tryNext) { $tryNext = false; $this-&gt;_iterator = call_user_func($this-&gt;_callback, ++$this-&gt;_count); } elseif ($this-&gt;_iterator !== null) { $this-&gt;_iterator-&gt;next(); if ($this-&gt;_iterator-&gt;valid() == false) { $tryNext = true; } } } while ($tryNext); } public function rewind() { $this-&gt;_iterator = call_user_func($this-&gt;_callback, $this-&gt;_count = 0); } public function valid () { return $this-&gt;_iterator !== null; } } ?&gt; </code></pre> <p><strong>UPDATE:</strong> Your ORMPagedIterator can be implemented using NextIteratorCallbackIterator as easy as:</p> <pre><code>&lt;?php class ORMPagedIterator implements IteratorAggregate { function __construct($orm, $filter, $sorting, $chunksize = 100) { $this-&gt;orm = $orm; $this-&gt;filter = $filter; $this-&gt;sorting = $sorting; $this-&gt;chunksize = $chunksize; } function iteratorNext($i) { $offset = $this-&gt;chunksize * $i; $recs = $this-&gt;orm-&gt;select($this-&gt;filter, $this-&gt;sorting, $this-&gt;chunksize, $offset); if ($recs) { return new ArrayIterator($recs); } return null; // end reached } function getIterator() { return new NextIteratorCallbackIterator(array($this,"iteratorNext")); } } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload