We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23214
    • 36 Posts
    I’m trying to build a snippet which can process the children of a document. However, I’m finding that the $modx->resource->getMany() function is behaving very strangely. Perhaps I’m missing something glaringly obvious, but any suggestions/feedback would be welcome at this point.

    To demonstrate the issue I’m having with getMany() I’ve written a simple snippet as follows:
    $output  = "";
    
    // getMany call #1
    $criteria1 = $modx->newQuery('modResource');
    $criteria1->where(array(
       'parent' => $id1
    ));
    $output .= "<p>document $id1 has ".sizeof($modx->resource->getMany('Children',$criteria1))." children</p>";
    
    // getMany call #2
    $criteria2 = $modx->newQuery('modResource');
    $criteria2->where(array(
       'parent' => $id2
    ));
    $output .= "<p>document $id2 has ".sizeof($modx->resource->getMany('Children',$criteria2))." children</p>";
    
    return $output;


    I call this snippet from a simple template which contains only one line: [[!getManyTest?id1=`11`&id2=`12`]].

    I should point out that document #11 has 7 children, and document #12 as 2 children in my document/resource tree. These documents are siblings in the tree (at the same level) and do not have a parent-child relationship, not that it should matter.

    Now, when I run the snippet with both getMany calls I get:
    document 11 has 7 children -- this is correct
    document 12 has 7 children -- this is incorrect

    If I reverse the order of the calls I get:
    document 12 has 2 children -- this is correct
    document 11 has 2 children -- this is incorrect

    Additionally, commenting out either of the getMany() calls returns the correct answer every time.

    Can anyone explain why this is happening? It appears to me that once getMany() has been called, the criteria remains the same for every subsequent call. Perhaps I should be fetching child documents in an entirely different way?

    Any ideas would be greatly apprieciated.

    I’m using MODx Revolution 2.0.0-pl rev7212 (basic installation) on a Linux server with PHP 5.2.13 and MySQL 5.0.91.
      • 22840
      • 1,572 Posts
      Can’t help with the code but if all you are wanting to do is display the amount of docs under a container GetChildCount used with GetResources counts the children in a container. I use it on my site.

      Simply install GetResources and in the chunk it calls add something like:

      [[+pagetitle]] has [[getChildCount? &parent=`[[+id]]`]]) children


      If that’s not what your after perhaps looking through the GetResources package would give you a heads up in the code to use.

      [[!getManyTest?id1=`11`&id2=`12`]]

      Are you not missing a & of id1=`11` ??
        • 23214
        • 36 Posts
        Thanks for your reply.

        I’m actually looking to pull out various fields of the child documents, rather than just a count.

        However, GetResources looks like a suitable alternative to try. I just figured that in Revo, traversing through the documents could be done programmatically with the $modx object.

        The code I was using in my snippet was based on an example I found here: http://bobsguides.com/revolution-objects.html
        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);
          • 23214
          • 36 Posts
          On further experimentation, I’ve found that using getCollection() works.

          $modx->resource->getCollection('modResource',$criteria1)


          Still trying to get my head around all the revo changes, I’m still not sure what the difference between getMany/getCollection is.