We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 24629
    • 370 Posts
    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
      • 4172
      • 5,888 Posts
      * 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();
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 24629
        • 370 Posts
        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
          • 22303 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().
            • 24629
            • 370 Posts
            aha,

            So if i want a list printed can i do this?

            $coll = $modx->getIterator('modxResource, arra(some stuff in here));
            foreach($coll as $row){
              $output .= $row->get('whatever');
            }
            return $output;


            Or won't that work because not all rows are in memory?

            rdg
              • 22303 MODX Staff
              • 10,725 Posts
              Quote from: rdaneeel at Jun 20, 2012, 12:19 PM
              So if i want a list printed can i do this?

              $coll = $modx->getIterator('modxResource', array('published' => true));
              foreach($coll as $row){
                $output .= $row->get('pagetitle') . "\n";
              }
              return $output;

              Something like that would certainly work since all you are doing is accessing a single row at a time...