We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36782
    • 109 Posts
    Hi there,
    I'm still learning to write snippets, and I was hoping to get a bit of critique.

    Very simple, I want to efficiently collect the basic fields of mod_resource's by 'id' and also one of the tpl vars which is associated w/ each - called 'image' in this case.

    Here's snippet code:
    // init output
    $output = '';
    
    // get collection of res. by id's (set in snpptcall)
    $resourceCollection = $modx->getCollection(
            'modResource', 
            "`id` IN (" .$bannerItems. ")"
    );
    
    // loop through collection, array-ify each...
    foreach($resourceCollection as $resource){
    $fields = $resource->toArray();
    
    // ... and add tv "image" to each arrayed resource
    $fields['image'] = $resource->getTVValue('image');
    
    // add chunk call to output for each arrayed resource
    $output .= $modx->getChunk('banner.item',$fields);
    };
    
    // return output
    return $output;
    


    So, it works. And I suppose that in many cases that's the important thing. But I would like to know if this is also a decent snippet, and not just a working snippet. In particular, I'd like to know if it's going to give me a worthwhile performance boost over getResources? Is it worth the custom snippet?

    Thanks for any comments you can give.

    -gabriel [ed. note: unsub777 last edited this post 12 years, 5 months ago.]
      • 3749
      • 24,544 Posts
      It should be faster than getResources. You could speed it up slightly by using the ID of the TV rather than the name in getTVValue():

      $resource->getTVValue(12);


      Be aware that it will not respect security permissions, or published, or deleted status as getResources does. You could add conditions to your criteria to skip deleted and unpublished resources, but respecting the permissions would make it more complicated and if you need to do that, you might want to use getResources().

      BTW, if you want even more speed, you could put the image in an unused resource field (e.g., summary), at the loss of some convenience.
        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
        • 36782
        • 109 Posts
        Hello Bob,
        Thanks for that; good to know about the id being faster. I'll switch it to that.
        As far as deleted, unpublished, etc., I'll definitely keep it in mind for other applications; for this project only published, non-deleted resources will be available to the selection. The permissions, though - I hadn't even thought of that.

        I like the idea of using 'summary' or somesuch; in this case my user needs a small bit of hand-holding, so I have to use the image uploader field. But I can think of other ideas which could benefit, so thanks for that.

        Cheers

        PS: Your guides are brilliant; now that i've got into snippets and permissions a bit, they have been a lifeline. Definitely checking out the book! smiley