We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3634
    • 19 Posts
    I wonder if the following behavior is nomal:

    $parent = $modx->getOne('modDocument',$id); // parent with many Children already
    
    $doc = $modx->newObject('modDocument');
    
    $kids = array();
    $kids[] = $doc;
    
    $parent->addMany( $kids );
    
    $parent->save();
    
    $parent->getMany('Children');
    
    echo count($parent->Children); //this will return 1 though there are much more children
    


    If I do in a different session just:
    $parent = $modx->getOne('modDocument',$id);
    $parent->getMany('Children');
    echo count($parent->Children); //this will return the expected number
    


    So if I call getMany just before I did addMany, the count is 1. Is this a bug or it’s me not understanding what getMany should do?

    Thanks,
      • 3749
      • 24,544 Posts
      You need to get the original document with getObject() not getOne(). The getOne() method is for getting a related object of an object you already have and only in cases where there can be only one such related object -- e.g., $resource->getOne(’Parent’).

      The $modx object doesn’t have any related objects, so you never call $modx->getOne() or $modx->getMany() -- those methods are only used with objects that represent tables in the DB (modResource, modUser, modTemplate, etc.).


      I think your code will work with this instead of the first line.

      $parent = $modx->getObject('modResource',$id); // parent with many Children already
        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