We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44976
    • 6 Posts
    Description of Problem:
    Project plan is to have child resources dragged and dropped in order via the manager resource tab (no menu index value set for example). How do I access order of resources as used in manager resource tab given a parent object.
    Using menu index is sufficiently inconvenient at the moment (and I'll use the comparison in class later).
    Steps to Reproduce:
    Currently using:
    $mySiblingObjs=$myParentObj->getMany('Children');
    
    foreach($mySiblingObjs as $mySibObj){
          print("<hr>Sib from foreach loop<br>");
          print_r($mySibObj->toArray());
    }
    

    Expected Outcome:
    Not finding a sort field used by the manager interface resources tab in the underlying tables. A change in the manager does seem to persist.
    I am fine with writing a query if that is the appropriate level of abstraction. I understand getMany() can sort but am not seeing an appropriate sort key.

    • MODX Version:
    • 2.2.7-pl (traditional) (non production hosting quick install)
    • PHP Version:
    • PHP Version 5.2.17
    • Database (MySQL, SQL Server, etc) Version:
    • MySQL 5.5.30-30.2 utf8

    Much thanks for any assistance. Also open to alternatives or other advice (but will use alternatives at this time iff my objective is not possible -even if not practical or advisable).

    This question has been answered by multiple community members. See the first response.

    [ed. note: blancha last edited this post 13 years, 1 month ago.]
    • discuss.answer
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Dragging and dropping to reorder the resources does change the Menu Index. That would be the field you would want to sort by.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 44976
        • 6 Posts
        Quote from: sottwell at Sep 01, 2013, 05:12 PM
        Dragging and dropping to reorder the resources does change the Menu Index. That would be the field you would want to sort by.
        Great! I over-contrived and under-tested by zeroing them then only moving them up smiley
        • discuss.answer
          • 3749
          • 24,544 Posts
          FYI, you can also provide criteria as a second argument to the getMany() call (or any xPDO get() method that gets multiple objects).

          $query = $modx->newQuery('modResource'); /* any class key here */
          
          $query->where(array(
              'published' => 1,
              'deleted' => 0,
          ));
          
          $query->sortby('menuindex', 'ASC');  // or 'DESC'
          
          $mySiblingObjs = $myParentObj->getMany('Children', $query);



          You could also do this, which might be faster since there's no query to get the parent object:

          $parentId = $modx->resource->get('parent');
          $query = $modx->newQuery('modResource'); 
          
          $query->where(array(
              'published' => 1,
              'deleted' => 0,
              'parent' => $parentId,
          ));
          
          $query->sortby('menuindex', 'ASC');  // or 'DESC'
          
          $mySiblingObjs = $modx->getCollection('modResource', $query);
            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
            • 44976
            • 6 Posts
            Quote from: BobRay at Sep 01, 2013, 09:53 PM
            FYI, you can also provide criteria as a second argument to the getMany() call (or any xPDO get() method that gets multiple objects).

            ...
            $mySiblingObjs = $myParentObj->getMany('Children', $query);



            You could also do this, which might be faster since there's no query to get the parent object:

            $parentId = $modx->resource->get('parent');
            $query = $modx->newQuery('modResource'); 
            
            $query->where(array(
                'published' => 1,
                'deleted' => 0,
                'parent' => $parentId,
            ));
            
            I did get those working in part referencing Bob's Guide (that's you), but in my haste I omitted deleted. It will take me a few more weeks to get back up to speed and I am very appreciative of both you and Shawn Wilkerson in text and online. I am getting new folks up to speed so I actually have to relearn a bit including 2.x updates. We'll walk through both approaches- I'm partly trying to identify which will maintain best with high turnover staff (they keep graduating) and my tendency to only focus on PHP and MODX every two years (when the related course is offered). Thanks for your followup!
            With changes mentioned above (minimal testing)
            $myQuery = $modx->newQuery('modResource'); 
            $myQuery->where(array(
                'published' => 1,
                'deleted' => 0,
                'parent' => $modx->resource->get('parent')
            ));
            $myQuery->sortby('menuindex', 'ASC');
            $mySiblingObjs = $modx->getCollection('modResource', $myQuery);
            
            I do like the contrast between walking the object with $myParentObj->getMany() and a composed but otherwise (semi) independent query with $modx->getCollection(). Happy to hear advice, preferences etc. as each seems a reasonable approach.
            [ed. note: blancha last edited this post 13 years, 1 month ago.]