We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    Given a TV name or ID (could be either) I need to check whether the TV is attached to a given template. Unfortunately, I need to process the TVs one at a time.

    Currently, I’m getting the array of modTemplateVarTemplates with this (which only executes once):

    $c = $this->modx->newQuery('modTemplateVarTemplate');
    $where = array('templateid'=>$this->template);
    $c->where($where);
    $tvTemplates = $this->modx->getCollection('modTemplateVarTemplate',$c);


    Then I get the TV with getObject() and get its ID with $tv->get(’id);
    I already know the template ID.

    I walk through the array of $tvTemplate objects (actually modTemplateVarTemplate objects) and "get" the tmplvarid and the templateid of each and look for a match with the TV ID and the template ID.

    It seems there should be a faster way with fewer queries.
      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
      • 22303 MODX Staff
      • 10,725 Posts
      By id is simple:
      <?php
      $hasTV = $modx->getCount('modTemplateVarTemplate', array('templateid' => $this->template, 'tmplvarid' => $tvId));
      if ($hasTV > 0) {
          // tmpl haz tv
      }


      By name is a little more complex, but this could also handle either approach by making the ’name’ => $tvName part dynamic:
      <?php
      $query = $modx->newQuery('modTemplateVar', array('name' => $tvName));
      $query->join('modTemplateVarTemplate', 'Template', array(
          "Template.tmplvarid = modTemplateVar.id",
          "Template.templateid" => $this->template
      ));
      $hasTV = $modx->getCount('modTemplateVar', $query);
      if ($hasTV > 0) {
          // tmpl haz tv
      }[


      If you have a list of TV ids or names, you can check them all at once with a little more complex of a query.

      I smell a new convenience method being born; or maybe it’s just me... grin
        • 3749
        • 24,544 Posts
        I noticed that same familiar scent myself. grin

        Actually, I was thinking of two, since you might have either object already:

        $tv->hasTemplate($tvId)
        
        $template->hasTv($templateID)



        I don’t think either needs to handle an array argument or names since it’s easy enough for the user to do the conversion before calling them; though I suppose they could be a tour-de-force methods with is_numeric() and is_array().

        In my snippet, I’m actually allowing a mixed comma-separated list containing the names of resource fields, TV names, and TV IDs. I wouldn’t think that kind of thing would be appropriate in the core, though.

          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
          • 28215
          • 4,149 Posts
          BobRay, those are good API method suggestions. We are approaching 2.1/2.2, and API method suggestions are welcome. You can file them here: http://bugs.modx.com/ tongue
            shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
            • 3749
            • 24,544 Posts
            Done.
              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