We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19599
    • 33 Posts
    Hi

    I have a resourceGroup called "testmember" that contains 4 resources. With the following script I would like to retrieve all resources in the resourceGroup to iterate over them, however $resources always comes back as an empty array. What am I overseeing?

    <?php
    if(!$modx->user->hasSessionContext($modx->context->get('key'))){
    	return;
    }
    $output 		= "Output";
    $tpl 			= $modx->getParser()->getElement('modChunk', $modx->getOption('tpl', $scriptProperties, 'videoItem'));
    $resourceGroup 	= $modx->getObject('modResourceGroup',array('name' => $modx->getLoginUserName($modx->context->get('key'))));
    
    $resources 	= $resourceGroup->getResources();
    
    $tpl->setCacheable(false);
    
    foreach ($resources as $resource) {
    	$resourceArray = $resource->toArray();
    	$tpl->_processed = false;
    	$output .= $tpl->process($resourceArray);
    }
    
    return $output;
    
    


    The resource group is being found, but getResources always returns an empty array. [ed. note: minauen last edited this post 10 years, 3 months ago.]
      • 19599
      • 33 Posts
      I figured that $resource->toArray() is causing my code to break.
      I don't quiet understand why $resource is of class modDocument_mysql at that point. It seems that it should inherit from modResource, however the ->toArray() method fails.

      thanks for your wisdom!
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        I struggled with this myself a while back, BobRay came up with this solution where "3" is the ID of the resource group in question
        <?php
        $object = $modx->getObject('modResourceGroup',3);
        $pages = $object->getResources();
        $output = '';
        foreach($pages as $value) {
          $output .= $value->pagetitle . "<br>";
        }
        return $output;

        There was also a solution from Bruno17 using MIGX's migxLoopCollection:
        <ul>
        [[migxLoopCollection?
        &classname = `modResource`
        &joins=`[{"alias":"ResourceGroupResources"}]`
        &where=`{"ResourceGroupResources.document_group":"3"}`
        &tpl=`@CODE:<li>[[+pagetitle]]([[+id]])</li>`
        ]]
        </ul>

        https://forums.modx.com/thread/85863/proper-way-to-find-all-resources-in-not-in-given-group
          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
          • 19599
          • 33 Posts
          Hi thanks for your reply,

          As mentioned in my second post, the issue is not the retrieval but that in the foreach loop the resource can not be turned into an array in order to be used in a chunk.
            • 4172
            • 5,888 Posts
            are your sure, you get the correct resourceGroup - object?
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 19599
              • 33 Posts
              Quote from: Bruno17 at Jun 23, 2016, 07:59 AM
              are your sure, you get the correct resourceGroup - object?

              Yes I'm sure. Can log it out.
                • 19599
                • 33 Posts
                The following works for me now:
                <?php
                if(!$modx->user->hasSessionContext($modx->context->get('key'))){
                	return;
                }
                $tpl 			= $modx->getOption('tpl', $scriptProperties, 'videoItem');
                $resourceGroup 	= $modx->getObject('modResourceGroup',array('name' => $modx->getLoginUserName($modx->context->get('key'))));
                
                if(!$resourceGroup) { return; }
                
                $resources 	= $resourceGroup->getResources();
                $ids = [];
                foreach ($resources as $resource) {
                	$ids[] = $resource->get('id');
                }
                
                return $modx->runSnippet('getResources', array(
                	'parents' => -1,
                	'resources' => implode(',',$ids),
                	'limit' => 0,
                	'tpl' => $tpl,
                	'includeTVs' => 1,
                	'tvPrefix' => ''
                ));



                Since I need to render everything in the end with chunks including template variables, I figured why not use getResources that already does it all for me. I'm sure the way to get the resource Ids could be simplified with a simple sql query...

                However I'm not very sure how to use the xpdo then. Once I have my resourceGroup object I run getResources on it, which is:
                public function getResources() {
                        $c= $this->xpdo->newQuery('modResource');
                        $c->innerJoin('modResourceGroupResource', 'ResourceGroupResources');
                        $c->where(array ('ResourceGroupResources.document_group' => $this->get('id')));
                        return $this->xpdo->getCollection('modResource', $c);
                    }


                So it makes a query for modResource objects and to me it looks like it will return modResource objects. However what I get in return is a collection of modDocument_mysql objects. That I can not wrap my head around.
                  • 3749
                  • 24,544 Posts
                  If you query on modResource, you'll get all its descendants (modDocument, modWeblink, modSymlink, etc.). You can base your query on modDocment, or you can add class_key => modDocument to your where clause if you just want documents.

                  BTW, you *are* already getting the MODX modResource objects and can get any of their fields in a foreach loop with $resource->get('fieldname'). You can also use fromArray() to get all their fields.

                    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
                    • 19599
                    • 33 Posts
                    Quote from: BobRay at Jun 23, 2016, 03:54 PM
                    If you query on modResource, you'll get all its descendants (modDocument, modWeblink, modSymlink, etc.). You can base your query on modDocment, or you can add class_key => modDocument to your where clause if you just want documents.

                    BTW, you *are* already getting the MODX modResource objects and can get any of their fields in a foreach loop with $resource->get('fieldname'). You can also use fromArray() to get all their fields.


                    Hi bob,
                    Thank you for your reply, that's what I thought. But for some reason I was not able to push my results direktly into a chunk, or us toArray() on the modResources. Can you tell me how this would work then:
                    $resources  = $resourceGroup->getResources();
                     
                    $tpl->setCacheable(false);
                     
                    foreach ($resources as $resource) {
                        $resourceArray = $resource->toArray();
                        $tpl->_processed = false;
                        $output .= $tpl->process($resourceArray);
                    }
                     
                    return $output;



                      • 3749
                      • 24,544 Posts
                      Is $tpl a chunk object you've just created, or is it the name of a real chunk? If it's the latter, you can just do this:

                      $output .= $modx->getChunk($tpl, $resourceArray);


                      If $tpl is a temporary chunk you're creating, I don't know why your code wouldn't work unless the chunk has no content or doesn't contain the correct placeholders.

                        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