We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19975
    • 429 Posts
    I am using MODX alongside an external API (database) to display property information, importing summary details using chunks fetched via xpath and MODX resources to store individual property details. The site imports data into the detailed property pages (resources) via XML with a TV to signal the individual property ID.

    With summary information being fed via the external database how do I interlink the system MODX resource pages with the summary information provided via the API?

    I have already considered adding a new field to the external database but this would require the site admin to add ID references to both MODX resources (for property ID) and the external database for MODX resource ID. As you may imagine this would be very error prone.

    Is there anyway to use a custom TV for the resource ID, or reference resource IDs by it's custom TV value?

    This question has been answered by BobRay. See the first response.

      Martin Sanders - Design & Web Development
      • 19975
      • 429 Posts
      I've created the following snippet to resolve my issue.

      If there are any PHP pros wishing to add improvements I would be grateful for suggestions.

      Thanks,

      <?php
      /************
      Return resource id based on TV value.
      Example: [[!parentIds?  &varName=`propertyID` &varValue=`[[+propertycode]]`]]
      ************/
      $c = $modx->newQuery('modResource');
      $c->innerJoin('modTemplateVarResource','TemplateVarResources');
      $c->innerJoin('modTemplateVar','TemplateVar','TemplateVarResources.tmplvarid = TemplateVar.id');
      $c->where(array(
          'TemplateVar.name' => $varName,
          'TemplateVarResources.value' => $varValue,
      ));
      foreach($modx->getCollection('modResource',$c) as $res) {
          $id = $res->get('id');
      }
      ($id ? $url = $modx->makeUrl($id) : '');
      return $url;
        Martin Sanders - Design & Web Development
        • 3749
        • 24,544 Posts
        It looks unnecessarily complicated, though maybe I'm misunderstanding what you're doing.

        If you're only getting a URL to the resource where the propertyID TV contains a particular value (you're only returning one URL), you only need to check the modTemplateVarResource -- all the info you need is there (even if you need more than one). This should do it:

        [[!parentIds?  &varValue=`[[+propertycode]]`]]


        $tvId = 12; /* set to ID of the PropertyId TV */
        $varValue = $modx->getOption('varValue', $scriptProperties);
        $res = $modx->getObject('modTemplateVarResource', array('tmplvarid' => $tvId, 'value' => $varValue));
        return $res? $modx->makeUrl($res->get('contentid'), "", "", "full"): '';
        


        If that works, this would be much faster and more efficient:

        $tvId = 12; /* set to ID of the PropertyId TV */
        $varValue = $modx->getOption('varValue', $scriptProperties);
        $query = $modx->newQuery('modTemplateVarResource', array(
            'value' => $varValue,
            'tmplvarid' => $tvId,
        ));
        $query->select('contentid');
        $docId = $modx->getValue($query->prepare());
        return $docId? $modx->makeUrl($docId, "", "", "full") : '';
        


        If you need to base it on more than one TV, you need an extra step at the top to get the TV's ID from the name (unless you send the TV's ID in the tag property, which would be faster). If you send the TV's ID as &tvId, you'd just need to replace the $tvId line with:

        $tvId = $modx->getOption('tvId', $scriptProperties);









          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
          • 19975
          • 429 Posts
          Thanks Bob,

          What does the
           $varValue = $modx->getOption('varValue', $scriptProperties);
          do?

          Could I simply use:
          $res = $modx->getObject('modTemplateVarResource', array('tmplvarid' => $tvId, 'value' => $tvValue));
          return $res? $modx->makeUrl($res->get('contentid'), "", "", "full"): '';
          ?
            Martin Sanders - Design & Web Development
          • discuss.answer
            • 3749
            • 24,544 Posts
            Funny you should ask. wink http://bobsguides.com/blog.html/2014/08/14/understanding-the-modx-getoption()-method/

            getOption() is a little safer from a sanity check standpoint so it's a good habit to use it, though you're free to do what you want. It keeps PHP from complaining about undefined variables if the properties are not set in the tag.

            Your way should work fine if you're sure the properties will always be set.
              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
              • 19975
              • 429 Posts
              Thanks for advise Bob, always highly appreciated smiley
                Martin Sanders - Design & Web Development