We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 13481
    • 97 Posts
    Right now I’m fetching related objects with getMany, but I need them ordered by a certain column. Is there an example somewhere on how to do this? It looks like I can use a criteria object to do it, but I’m stuck and can’t figure it out. So if someone can show me how to do it, or let me know if there is a better/easier way to do this then that would be awesome.

    Thanks,

    James
      • 3749
      • 24,544 Posts
      Quote from: devtrench at Jun 19, 2010, 11:21 PM

      Right now I’m fetching related objects with getMany, but I need them ordered by a certain column. Is there an example somewhere on how to do this? It looks like I can use a criteria object to do it, but I’m stuck and can’t figure it out. So if someone can show me how to do it, or let me know if there is a better/easier way to do this then that would be awesome.

      Thanks,

      James

      Courtesy of bobsguides.com (http://bobsguides.com/revolution-objects.html) smiley

      To get the children of a document, sorted by Title:

      $criteria = $modx->newQuery('modResource');
      $criteria->where(array(
         'parent' => $modx->resource->get('id'),
      ));
      $criteria->sortby('pagetitle','ASC');
      $children = $modx->resource->getMany('Children',$criteria);
        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
        • 13481
        • 97 Posts
        Quote from: BobRay at Jun 20, 2010, 02:20 AM

        Courtesy of bobsguides.com (http://bobsguides.com/revolution-objects.html) smiley

        Thanks a ton Bob, just what I was looking for - and thanks for the link to your tutorial on revolution objects, great work!

        James
          • 13481
          • 97 Posts
          Quote from: BobRay at Jun 20, 2010, 02:20 AM

          To get the children of a document, sorted by Title:

          $criteria = $modx->newQuery('modResource');
          $criteria->where(array(
             'parent' => $modx->resource->get('id'),
          ));
          $criteria->sortby('pagetitle','ASC');
          $children = $modx->resource->getMany('Children',$criteria);


          Turns out this wasn’t exactly what I needed but it’s very close. What Bob’s code does is get all MODx resources sorted by page title along with their children. What I needed is all children sorted by a particular field (note these aren’t modx resources, they’re actually wordpress comments)

          // Create a query for comments
          $criteria = $wp->newQuery('Comments');
          $criteria->where(array(
                  'comment_post_ID' => $post->get('ID'),
          ));
          // Here is where to set the sort order
          $criteria->sortby('comment_ID','ASC');
          
          // $post is a single post object, analogous to a single MODx resource object
          // Here we tell xpdo to get all comments of this post, using our custom criteria
          $comments = $post->getMany('Comments',$criteria);
          


          Thanks again bob for leading me in the right direction. The key is knowing how to build custom query objects.