We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3232
    • 380 Posts
    I am just starting to wrap my head around xpdo and have a very simple question. I am retrieving data from a custom table and am only expecting 1 result. My code is as follows:

    $access = $modx->newQuery('IYK');
    $access->where(array('user_id' => $modx->getLoginUserID()));
    $vA = $modx->getIterator('IYK',$access);
    
    if(count($videoAccess) != 1 )
        return '';
    
    foreach ( $vA as $v ) {
        $output = $v->get('items');
    }


    Now user_id is a unique field and will only produce one row. Is there a simpler way to just retrieve the one row from the database rather then to a foreach for just one row?
      • 33976 ☆ A M B ☆
      • 571 Posts
      Why not make use of getObject ?

      $vA = $modx->getObject('IYK', array('user_id' => $modx->getLoginUserID()));
      if (!$vA) return '';
      $output = $vA->get('items');
      
        • 3749
        • 24,544 Posts
        There's some info here that might help: http://bobsguides.com/custom-db-tables.html.

        (And Romain is right, getObject() is definitely the way to go.)


        ------------------------------------------------------------------------------------------
        PLEASE, PLEASE specify the version of MODX you are using.
        MODX info for everyone: http://bobsguides.com/modx.html
          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
          • 3232
          • 380 Posts
          @romain

          That snippet of code worked great. I totally overlooked getObject().

          @BobRay

          This is a great resource, it has been bookmarked and I will be referring to it often.

          Thank you both for your help! Much appreciated!

          -Brian