We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37475
    • 25 Posts
    After doing a lot of search I still dont get how to in the easiest way do a select statement on modx database tables from a snippet. I found a lot of xPDO. I didnt get it to work. Is it usefull here?

    The statement I want to do in a snippet is:

    "SELECT contentid, value FROM modx_site_tmplvar_contentvalues WHERE tmplvarid = XX"

    Happy for all the help I can get.
      • 3749
      • 24,544 Posts
      xPDO would be *really* useful here. I'm not sure, but I think this is what you want:

      <?php
      /* Get all TemplateVarResource objects where the TV ID is 12 */
      $tvrs = $modx->getCollection('modTemplateVarResource', array('templvarid' => '12'));
      
      if (empty($tvrs)) {
          return 'No TVRs found';
      }
      
      $output = "";
      
      /* output the resource ID and Value for each one */
      foreach ($tvrs as $tvr) {
        $output .= '<br />Resource ID: ' . $tvr->get('contentid');
        $output .= ' ----   Value: ' . $tvr->get('value');
      }
      
      return $output;
        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
        • 3749
        • 24,544 Posts
        xPDO would be *really* useful here. I'm not sure, but I think this is what you want:

        <?php
        /* Get all TemplateVarResource objects where the TV ID is 12 */
        $tvrs = $modx->getCollection('modTemplateVarResource', array('templvarid' => '12'));
        
        if (empty($tvrs)) {
            return 'No TVRs found';
        }
        
        $output = "";
        
        /* output the resource ID and Value for each one */
        foreach ($tvrs as $tvr) {
          $output .= '<br />Resource ID: ' . $tvr->get('contentid');
          $output .= ' ----   Value: ' . $tvr->get('value');
        }
        
        return $output;
          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
          • 37475
          • 25 Posts
          Thank you. It got me in the right direction.

          How I solved it:

          $c = $modx->newQuery('modTemplateVarResource');
          $c->select($modx->getSelectColumns('modTemplateVarResource','','',array('id','contentid','value')));
          
          $c->where(array(
                'tmplvarid' => '25',
          ));
          $collection = $modx->getCollection('modTemplateVarResource', $c);
          
          foreach($collection as $item) {
            $test[] = $item->get('value');
            $test[] = $item->get('contentid');
          }
            • 3749
            • 24,544 Posts
            Looks good. BTW, the select() is kind of overkill because the object only has three fields (other than the id) and you're getting almost all of them -- that's why I left it out, and since you're searching by tmplvarid, you may need to select it too, I'm not sure. [ed. note: BobRay last edited this post 12 years, 5 months ago.]
              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
              • 37475
              • 25 Posts
              It should be:
              $c->select($modx->getSelectColumns('modTemplateVarResource','','','')));
              

              then?
                • 3749
                • 24,544 Posts
                No, I'd leave out the select line entirely. I think it will save you a tiny bit of memory, but cost you speed. I could be wrong.
                  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
                  • 3749
                  • 24,544 Posts
                  No, I'd leave out the select line entirely. I think it will save you a tiny bit of memory, but cost you speed. I could be wrong.
                    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
                    • 37475
                    • 25 Posts
                    Ok, understand. Thank you again for your help.