We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44874
    • 11 Posts
    I have some code in my snippet:
    $childs = $modx->getCollectionGraph('modResource', '{"TemplateVarResources":{}}', array(
        'parent' => $parentId,
        'TemplateVarResources.tmplvarid:IN' => array(1, 2, 3)
    ));
    

    In each of $childs I get an array by key 'TemplateVarResources', that stores rows from site_tmplvar_contentvalues table.
    A problem in that, the keys of these 'TemplateVarResources' array are the ids of table row.
    What can I do to get tmplvarid as keys instead id?

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

      • 3749
      • 24,544 Posts
      I don't understand your question, but maybe this will help: There are only three fields in a modTemplateVarResource object:

      tmplvarid: The id of the Template Variable it points to
      contentid: The id of the Resource it points to
      value: the value of that TV for that Resource

      You might want something like this:

      foreach($childs as $child) {
      
          foreach($child->TemplateVarResources as $tv) {
      
              if ($tv->get('tmplvarid') == 1) {
                 $tv1_value = $tv->get('value');
              }
      
              if ($tv->get('tmplvarid') == 2) {
                 $tv2_value = $tv->get('value');
              }
          }
      }
        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
        • 44874
        • 11 Posts
        I made it simply:
        $child_array = $child->toArray('', true);
        foreach ($child->TemplateVarResources as $tv) {
            $child_array['tv'][$tv->get('tmplvarid')] = $tv->get('value');
        }
        
        • discuss.answer
          • 44874
          • 11 Posts
          Even so:
          $tvs = $modx->getCollection('modTemplateVar', array(
              'id:IN' => array(1, 2, 3)
          ));
          
          $tvs_array = array();
          foreach ($tvs as $tv_obj) {
              $tvs_array[$tv_obj->get('id')] = $tv_obj->get('name');
          }
          ...
          foreach ($childs as $child) {
              $child_array = $child->toArray('', true);
              foreach ($child->TemplateVarResources as $tv) {
                  $child_array['tv'][$tvs_array[$tv->get('tmplvarid')]] = $tv->get('value');
              }
          }
          
            • 3749
            • 24,544 Posts
            Is that working for you? If so, please mark this thread as answered.
              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