We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44874
    • 11 Posts
    I have a snippet:
    <?php
    $childs = $modx->getCollection('modResource',array('parent' => $parentId));
    
    $chunk = $modx->getObject('modChunk', 1);
    
    $output = '';
    foreach ($childs as $child) {
        $output .= $chunk->process($child->toArray());
    }
    
    return $output;
    


    $childs have different values.

    Does anyone know, why $chunk->process() returns same value every time? And what I should do to get different values in the loop?
    • You need to make sure the Chunk is set non-cacheable on each loop like so:
      foreach ($childs as $child) {
          $chunk->setCacheable(false);
          $output .= $chunk->process($child->toArray());
      }


      However, I would recommend instead you use this within the loop:
      $modx->getChunk('chunkName', $child->toArray());
      This uses built-in element caching in MODX. It will not get the object from the db on each call, and if the page is cached, it does not even have to go to the database to get the Chunk object.

      Another alternative, is to use the following instead of getObject() to get the Chunk:
      $chunk = $modx->parser->getElement('modChunk', 'chunkName');

      This also will make use of the built-in element caching. However, this method also requires the call to
      $chunk->setCacheable(false);
      within the loop...
        • 44874
        • 11 Posts
        Unfortunately, using
        $chunk->setCacheable(false);
        

        separately or with
        $chunk = $modx->parser->getElement('modChunk', 'chunkName');
        

        has no effect.
        I don`t want to use
        $modx->getChunk('chunkName', $child->toArray());
        

        because is`s so slow, from my observations.
          • 36572
          • 20 Posts
          Just a quick question? Why don't you use getResources. I have the idea this snippet fits your needs.

            Developer @ Sterc bureau voor internet & marketing
            • 44874
            • 11 Posts
            Quote from: sterce-friso at Aug 23, 2013, 02:46 PM
            Just a quick question? Why don't you use getResources. I have the idea this snippet fits your needs.

            Cause I don't want use huge 3rd party snippet for my little needs. It's also a good opportunity to take deep into MODx.

            I found some solution in wayfinder class, and modify it bit:
            $useChunk = $modx->getObject('modChunk', 1)->get('content');
            $chunk = $modx->newObject('modChunk');
            foreach ($childs as $child) {
                $output .= $chunk->process($child->toArray(), $useChunk);
            }
            
              • 3749
              • 24,544 Posts
              FYI, if you can use the raw values of the chunk's fields, it may speed things up slightly to use:

              toArray('', true);


                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 44874
                • 11 Posts
                Thanks, it was faster by about 30%.
                But I have another problem. I need to get TV Value of each $child in loop.
                $child->getTVValue('tvname')

                again, very slowly.
                Does somebody know, how can I get it faster?
                  • 3749
                  • 24,544 Posts
                  You can speed it up slightly by getting the modTemplateVarResource object:

                  $tvr = $modx->getObject('modTemplateVarResource', array('tmplvarid' => $tvId, 'contentid' => $child->get('id')));
                  if ($tvr) {
                      $value = $tvt->get('value');
                  }



                  A faster method might be to do a join on the 'modTemplateVarResource' table so your getCollection() call would get the TV values in addition to the Resources, but that's significantly more complicated.
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 44874
                    • 11 Posts
                    I'm not afraid of difficulties, my goal - to better understand MODx
                      • 3749
                      • 24,544 Posts
                      Unfortunately, the docs are a little sparse. wink

                      This may help: https://www.google.com/search?q=xpdo+query+leftjoin&oq=xpdo+query+leftjoin
                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting