We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 168
    • 15 Posts
    In Revo, i did the following.

    // Get the correct modResource by division
    $query = $modx->newQuery('modResource');
    $query->leftJoin('modTemplate', 'Template');
    $query->where(array(
    	'Template.templatename' => 'Division',
    	'modResource.pagetitle' => $division
    ));
    
    // Get the resources
    $resource = $modx->getObject('modResource', $query);
    
    // Get the correct template var
    $query = $modx->newQuery('modTemplateVarResource');
    $query->leftJoin('modTemplateVar', 'TemplateVar', '`TemplateVar`.`id` = `modTemplateVarResource`.`tmplvarid`');
    $query->where(array(
    	'`TemplateVar`.name' => "hometext_{$side}",
    	'`modTemplateVarResource`.contentid' => $resource->get('id')
    ));
    
    // Get the template var
    $tvar = $modx->getObject('modTemplateVarResource', $query);


    First getting a resource by type of template and name.
    After that, it’s getting a tvar (modTemplateVarResource) by tvar name and by the id of the resource (from the first step)

    Is there a way to do this in one step, e.g. by combining the 2 queries.

    TIA
      • 22303 MODX Staff
      • 10,725 Posts
      You can just do this:
      <?php
      $tv = $modx->getObject('modTemplateVar', array('name' => "hometext_{$side}"));
      $resource = $modx->getObjectGraph('modResource', array('Template' => array()), array(
          'Template.templatename' => 'Division',
          'modResource.pagetitle' => $division
      ));
      $tvOutput = '';
      if ($resource) {
          if ($processTV) {
              $tvOutput = $tv->outputContent($resource->get('id'));
          } else {
              $tvOutput = $tv->getValue($resource->get('id'));
          }
      }
      
      // etc., etc., etc.
      ?>


      Also, if you have another way of getting the Resource id, you don’t even need to query the modResource and it’s Template. This could reduce memory usage and optimize your Snippet execution time.
        • 168
        • 15 Posts
        Thanks allot though! laugh

        Works like a charm cool