We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14883 ☆ A M B ☆
    • 450 Posts
    I’m trying to create a TV that lets you select from all published resource IDs that use a given template. I’ve tried figuring this out by looking at a few code samples, and I’m sure it is very simple. But I’m flat out lost. My best guess is
    $c = $modx->newQuery('modResource');  // or modResourceGroup? 
    $c->leftJoin(... //something to establish that I want to get at the templates associated with the resources
    $c->where(... // where the resource uses template $x)
           
    $resourceArray = $modx->getCollection('modResource',$c);


    Could you help me get on the right track? And, is there a good resource page for various examples of working with the API to do similar things?
      • 28215
      • 4,149 Posts
      Quote from: jrotering at May 20, 2010, 10:41 AM

      I’m trying to create a TV that lets you select from all published resource IDs that use a given template. I’ve tried figuring this out by looking at a few code samples, and I’m sure it is very simple. But I’m flat out lost. My best guess is
      $c = $modx->newQuery('modResource');  // or modResourceGroup? 
      $c->leftJoin(... //something to establish that I want to get at the templates associated with the resources
      $c->where(... // where the resource uses template $x)
      $resourceArray = $modx->getCollection('modResource',$c);

      Could you help me get on the right track? And, is there a good resource page for various examples of working with the API to do similar things?

      $c = $modx->newQuery('modResource');
      $c->leftJoin('modTemplate','Template');
      $c->where(array(
          'published' => true,
      ));
      if (is_numeric($template)) {
          $c->where(array('template' => $template));
      } else {
          $c->where(array('Template.templatename' => $template));
      }
      $resources = $modx->getCollection('modResource',$c);
      


      Should work, with $template being either the ID or name of the Template.
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 14883 ☆ A M B ☆
        • 450 Posts
        Perfect! Thanks.
          • 34178
          • 212 Posts
          Is there also a way in the modx-backend to show all resources which use a certain template (except scanning the database-table of the modx-entries)?

          Or do we have to create a snippet for this like suggested above?

          Thanks a lot in advance!
          Letti
            • 37242 ☆ A M B ☆
            • 339 Posts
            The package "batcher" can do all kind of things: http://rtfm.modx.com/display/ADDON/Batcher
            I used it to change all of my 700+ resources to new templates.
              • 34178
              • 212 Posts
              You are completely right! I forgot 'batcher' which I never used since transferring our former site to modx ...

              Thanks a again!
              Letti