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

    I'm quite new to modx and I'm trying to bring an evolution site into the modx revolution era.

    I'm looking for the equivalent of the following:
    $boxids = $modx->getDocumentChildren($parentID,1,0,'id,pagetitle');


    $modx->getChildIds();
    is handy but I'm after more than just the id field.

    Any help would be great.

    Thanks.

    Chris
      • 33337
      • 3,975 Posts
      Please see http://bobsguides.com/revolution-objects.html

      Hope this helps.
        Zaigham R - MODX Professional | Skype | Email | Twitter

        Digging the interwebs for #MODX gems and bringing it to you. modx.link
        • 3749
        • 24,544 Posts
        Here are two quick and dirty methods:

        $children = $modx->getCollection('modResource', array('parent' => $parentId));
        
        foreach ($children as $child) {
           $pagetitle = $child->get('pagetitle');
           $id = $child->get('id');
        }
        
        


        If you already have the parent object (for example if it's the current resource), you can replace the first line with this:

        $children = $parentObj->getMany('Children');


        or

        $children = $modx->resource->getMany('Children');


        You can refine this by using $modx->newQuery() and $modx->getSelectColumns() to restrict the fields returned to just the id and pagetitle (but be sure to include the parent field if you're using the getCollection() version). The code above may be fast enough, though.


        $query = $modx->newQuery('modResource');
        $query->select($modx->getSelectColumns('modResource','modResource','',array('id','pagetitle','parent')));
        $query->where(array(
           'parent' => $parentId,
        ));
        
        $children = $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
          • 47842
          • 4 Posts
          Quote from: BobRay at Jun 02, 2014, 03:47 PM
          Here are two quick and dirty methods:

          $children = $modx->getCollection('modResource', array('parent' => $parentId));
          
          foreach ($children as $child) {
             $pagetitle = $child->get('pagetitle');
             $id = $child->get('id');
          }
          
          


          If you already have the parent object (for example if it's the current resource), you can replace the first line with this:

          $children = $parentObj->getMany('Children');


          or

          $children = $modx->resource->getMany('Children');


          You can refine this by using $modx->newQuery() and $modx->getSelectColumns() to restrict the fields returned to just the id and pagetitle (but be sure to include the parent field if you're using the getCollection() version). The code above may be fast enough, though.


          $query = $modx->newQuery('modResource');
          $query->select($modx->getSelectColumns('modResource','modResource','',array('id','pagetitle','parent')));
          $query->where(array(
             'parent' => $parentId,
          ));
          
          $children = $modx->getCollection('modResource',$query);



          Hi Bob,

          Thanks for the detailed answer. I found an answer in

          $sql = "SELECT id, pagetitle FROM modx_site_content WHERE parent = " .$parentID. " AND published=1 AND deleted=0";
          	$result = $modx->query($sql);
          	$boxids = $result->fetchAll(PDO::FETCH_ASSOC);


          It does feel a bit old-hat however so I'll try and utilise your methods.

          Thanks.

          Chris
            • 3749
            • 24,544 Posts
            The method you quote would probably be a little faster, though the xPDO caching might turn that around.
              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
              • 40045
              • 534 Posts
              Another way to do what

              $parentObj = $modx->getObject('modResource', array('id' => 189));
              $children = $parentObj->getMany('Children');
              


              does would be

              $parentObj = $modx->getObject('modResource', array('id' => 189));
              
              $children = $parentObj->Children;
              
              // or for the current resource
              
              $children = $modx->resource->Children;
              


              I actually came to know about this recently and asked myself which would be faster or if there's any difference? Anybody some insights on this?

              Actually just did a quick test and it seems ->getMany('Children') is almost double as fast as ->Children, any ideas why it seems to be like that?
                • 3749
                • 24,544 Posts
                I'm just guessing, but maybe ->children involves lazy loading and the children are retrieved one at a time as needed, versus getMany->('Children'), which gets them in a single query with array('parent' => $id).

                  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