We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17906
    • 18 Posts
    Hi,

    I’m using xPDO for a new project I’m working on. I’d like to know how I can issue SQL ORDER statements ? Does xPDOCriteria support this or do I have to create an SQL statement ? Could you give me some simple examples of both of these ?

    Thanks in advance,

    MaDrense
      • 22303 MODX Staff
      • 10,725 Posts
      If you have the latest xPDO from SVN, the new xPDOQuery class provides the sortBy() function for building queries with an ORDER BY clause via the API. Here is an example:

      <?php
      $query= $xpdo->newQuery('MyClass');
      $query->sortBy('sort_field', 'ASC');
      $myObject= $xpdo->getObject('MyClass', $query);
      


      You can of course just write your SQL directly via xPDOCriteria:

      <?php
      $criteria= new xPDOCriteria($xpdo, "SELECT * FROM {$xpdo->getTableName('MyClass')} ORDER BY sort_field ASC");
      $myObject= $xpdo->getObject('MyClass', $criteria);