0
votes

Can someone please provide an explanation of the difference between get() and get_iterated() in the context of CodeIgniter's DataMapper ORM referenced here:

http://datamapper.wanwizard.eu/pages/getalt.html#get_iterated

I've read through it multiple times, but do not understand. I've also output the result of queries using each method, but don't see the difference. I can confirm that get_iterated() definitely uses a lot less memory than get(), but I would like to understand what's actually happening.

I'm hoping someone can provide a clear explanation. Thanks!

1

1 Answers

1
votes

Here is the implementation of get_iterated() (comments mine):

public function get_iterated($limit = NULL, $offset = NULL)
{
     // get hold of object after it is cleared though call to get_row
    $object = $this->get_clone();
    // clears the query from the clone
    $object->db->dm_call_method('_reset_select');
    // clear the related query from the clone
    $object->_query_related = array();

    // REAL MAGIC starts here: Build iterator
    $this->_dm_dataset_iterator = new DM_DatasetIterator($object, $this->get_raw($limit, $offset, TRUE));
    return $this;
}

The real magic happens at the line:

$this->_dm_dataset_iterator = new DM_DatasetIterator($object, $this->get_raw($limit, $offset, TRUE));

The real speedy work is done by the DM_DatasetIterator class for the get_iterated() method because it implements an Iterator which are generally FAST. The class implements all the required methods that are required by the Iterator interface such as current(), key(), get(), etc

So overall there are two reasons why it is fast:

  • The actual object is cleaned (query and related info)
  • It is then passed to iterator which are fast

You might ask why iterators are fast? Well because they process data iteratively instead of buffering in memory.

Learn about here: