We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 31037
    • 358 Posts
    I’ve tried to get all user ids into an array using xpdo queries but everything I try fail. Tried to follow the examples but no luck. Good side is that I’ve read the rtfm many times now. tongue

    Problem is that I can’t get the whole object for 28000 users as it gives memory errors and takes for ever, so getCollection isn’t an option.

    Tried to search (I think I read about it before but can’t find it). Everything leads to the getValue snippet which doesn’t work as it retrieves the whole object.

    So, how can I in a Revo style get all values for a single column in a fast and low memory query?

    Thanks!
      • 22303 MODX Staff
      • 10,725 Posts
      Quote from: Uncle68 at Nov 06, 2010, 09:03 AM

      I’ve tried to get all user ids into an array using xpdo queries but everything I try fail. Tried to follow the examples but no luck. Good side is that I’ve read the rtfm many times now. tongue

      Problem is that I can’t get the whole object for 28000 users as it gives memory errors and takes for ever, so getCollection isn’t an option.

      Tried to search (I think I read about it before but can’t find it). Everything leads to the getValue snippet which doesn’t work as it retrieves the whole object.

      So, how can I in a Revo style get all values for a single column in a fast and low memory query?

      Thanks!
      Very easily...
      <?php
      // build a query using the API
      $query = $modx->newQuery('modUser');
      $query->select($modx->getSelectColumns('modUser', '', '', array('id')));
      $query->sortby($modx->escape('id'), 'ASC');
      
      // now prepare a PDOStatement and execute it
      $userIds = array();
      if ($query->prepare() && $query->stmt->execute()) {
          // xPDOQuery (and it's parent class, xPDOCriteria) wrap a PDOStatement
          //  and you can use it like any PDOStatement, e.g. PDO::FETCH_COLUMN
          $userIds = $query->stmt->fetchAll(PDO::FETCH_COLUMN);
      } else {
          // echo an error to the output in HTML format
          $modx->log(modX::LOG_LEVEL_ERROR, "Error executing query:\n" . $query->toSQL(), 'HTML');
      }
      ?>


      See http://www.php.net/manual/en/pdostatement.fetchall.php for details on fetch formats in PDO.
        • 31037
        • 358 Posts
        Ahhh then I had it right all the time, it was just the "$query->stmt->fetchAll(PDO::FETCH_COLUMN);" i missed! And yet I sat reading the online php manual about PDO just the other day! tongue

        It was by reading that manual (and the very good xPDO rtfm) I finally got the pieces about how MODx is working in place. But oviously that didn’t help as I sat some hours trying to get the above to work lol... good side is I now have a pretty good understanding about PDO, xPDO, xPDOcriteria and that stuff! tongue

        Thanks!