We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42682
    • 20 Posts
    I have a snippet that I use to obtain information from child resources to populate the immediate parent of those resources:

    <?php
    /* @var $modx modX */
    
    
    
    
    $c = $modx->newQuery('modResource');
    $c->where(array(
        'hidemenu' => 0,
        'published' => 1,
    ));
     
    $sortBy = isset($scriptProperties['sortBy'])? $scriptProperties['sortBy'] : 'menuindex';
    $sortDir = isset($scriptProperties['sortDir'])? $scriptProperties['sortDir'] : 'ASC';
    $c->sortby($sortBy, $sortDir);
    $output = '';
    $children = $modx->resource->getMany('Children', $c);
    
    $i = 0; 
     
    foreach($children as $child) {
        /* @var $child modResource */
        $fields = $child->toArray();
        $fields['imgSource'] = $child->getTVValue('ProductImage');
    
        $output .= $modx->getChunk('navListTpl', $fields);
       
       }
    
    return $output;


    This snippet returns the image and text for the child resources which I use to populate a menu on the parent page and use to list items.

    Can anybody tell me how I can use this snippet to point to a specific page and return the child resources from that page instead of the child resources of whichever page I am on?
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Is there some reason why you don't want to use getResources or pdoResources?
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 42682
        • 20 Posts
        Quote from: sottwell at Mar 31, 2014, 03:52 PM
        Is there some reason why you don't want to use getResources or pdoResources?

        No reason, I'm a little unfamiliar with modx code library and the snippet above is one I've used and has worked for me and almost does what I want it to.
          • 3749
          • 24,544 Posts
          I think this is what you want:

          [[!MySnippet? $docId=`12`]]



          $resource = $modx->getObject('modResource', $scriptProperties['docId']);
          
          /* ... */
          
          $children = $resource->getMany('Children');
          

            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
            • 42682
            • 20 Posts
            That throws me the error message:

            Fatal error: Call to a member function where() on a non-object. Here is the snippet as I have it:

            /* @var $modx modX */
            
            
            
            $resource = $modx->getObject('modResource', $scriptProperties['docId']);
            
            $resource->where(array(
                'hidemenu' => 0,
                'published' => 1,
            ));
             
            $sortBy = isset($scriptProperties['sortBy'])? $scriptProperties['sortBy'] : 'menuindex';
            $sortDir = isset($scriptProperties['sortDir'])? $scriptProperties['sortDir'] : 'ASC';
            $resource ->sortby($sortBy, $sortDir);
            $output = '';
            $children = $resource->getMany('Children');
            
            $i = 0; 
             
            foreach($children as $child) {
                /* @var $child modResource */
                $fields = $child->toArray();
                $fields['imgSource'] = $child->getTVValue('ProductImage');
            
                $output .= $modx->getChunk('navListTpl', $fields);
               
               }
            
            return $output;
              • 3749
              • 24,544 Posts
              Possibly the getObject() call is failing. It will return null on failure.

              That's usuallly because either the ID you're sending doesn't point to a real resource or docId is spelled differently between the tag and the snippet.

              In addition, though, there is no where() method of the modResource object.

              If you want to apply those criteria to getting the children, you'd do this:

              $c = $modx->newQuery('modResource');
              $c->where(
                  'hidemenu' => 0,
                  'published' => 1,
              );
              
              /* ... */
              
              $children = $resource->getMany('Children', $c);
              

                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