Hi,
I'm trying to loop through a collection of custom variables from a custom table.
but i cant figure out why the 'for' loop doesn't work.
reading up on this i get different solutions but what puzzles me most at this moment is
what is the difference between getIterator() and getCollection()?
neither seems to return a normal array
Rdg
* Use an xPDOIterator to loop over large result sets and work with one instance
* at a time. This greatly reduces memory usage over loading the entire collection
* of objects into memory at one time. It is also slightly faster.
In both cases you will get an xpdo-object in your loop.
getCollecton loads the entire object-collection.
getIterator loads the objects one by one at the time, when you need them.
To get an array from that object you will do:
$row = $object->toArray();
Hi,
So what would be the point for this? why would i choose foor either of them?
when i would do Collectionrow->get('pagetitle); its faster?
Or is it not possible to do IteratorRow->get('pagetitle') because the resource is not in memory?
RDG
-
MODX Staff
- 10,725 Posts
If you need the entire collection of objects in memory, e.g. for manipulating the order or referring to other rows when processing the collection, then getCollection() is what you would want to use.
If you only need access to each row individually as you iterate over the collection, then getIterator() is preferred.
The benefit of using getIterator() despite only being able to access one row at a time, is that you can iterate over as many rows as you want without using any more memory than that taken by one object instance. With getCollection(), if you load 100 rows, you use 99x more memory than with getIterator().