We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 47212
    • 40 Posts
    The
    $modx->getParentIds()
    method does not return an array with the parent ids of current resource if this resource was just created.
    (My need is to get the ultimate parent of the resource, so I can't use the $resource->get('parent') method.)

    On a plugin fired on the OnDocFormSave event :
    $parents = $modx->getParentIds($id, 20, array('context' => 'web'));
        return 'parents are : ' . print_r($parents );
    


    This returns this in log if the file was just created :
    [2015-11-26 21:20:23] (ERROR @ /sites/myproject/connectors/index.php) [OnDocFormSave]Array
    (
    )
    parents are : 1


    While the same code on a resource update returns this :
    [2015-11-26 21:27:28] (ERROR @ /sites/myproject/connectors/index.php) [OnDocFormSave]Array
    (
        [0] => 185
        [1] => 184
        [2] => 139
        [3] => 0
    )
    parents are : 1


    I thought that the OnDocFormSave event fired when the resource is fully saved in the database (to allow access of $id and $resource vars).

    Am I wrong ?

    -----------------------------------------------------------------------------------------------------------------------------
    Edit :

    While this question is pending, I found another way to get the ultimate parent without using the bugy getParentIds method, by making a loop this way :
        $parentId = $resource->get('parent');
        while ($parentId !== 0) {
            $parent = $modx->getObject('modResource', $parentId);
            $parentId = $parent->get('parent');
        }
        return 'ultimate parent is : ' . $parent->get('id');
    


    This returns correctly the ultimate parent, even if the resource was just created :
    [2015-11-26 21:54:32] (ERROR @ /sites/myproject/connectors/index.php) [OnDocFormSave]ultimate parent is : 6
    


    Thanks to me for this solution.
    - No problem bro.

    This question has been answered by romainfallet. See the first response.

    [ed. note: romainfallet last edited this post 10 years, 10 months ago.]
      • 3749
      • 24,544 Posts
      I think your original problem was with print_r(). It doesn't return a value unless you add true as a second argument:

      return 'parents are : ' . print_r($parents, true);


        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
      • discuss.answer
        • 47212
        • 40 Posts
        I didn't know that this parameter exists. Why do we learn poetries and songs at primary school instead of PHP reference manual ?

        Anyway, the parameter "true" changes nothing. The purpose of the parameter is to return the value instead of displaying it, and I did want to display it in logs.

        I updated a little my script that I put in a snippet, in replacement of UltimateParent that uses the $modx->getParentIds method :
        $parentId = $modx->getObject('modResource', $id)->get('parent');
        while ($parentId !== 0) {
            $parent = $modx->getObject('modResource', $parentId);
            $parentId = $parent->get('parent');
        }
        if(empty($parent)) { $ultimateParent = 0; }
        else { $ultimateParent = $parent->get('id'); }
        return $ultimateParent;
        


        I use it that way in plugins :
        $modx->runSnippet('UltimateParent', array('id' => $id))


        It also works in templates :
        [!UltimateParent?id=`[[*id]]`]]

        [ed. note: romainfallet last edited this post 10 years, 10 months ago.]
          • 3749
          • 24,544 Posts
          If you want to write the value to a log or 'return' it for MODX to display, you have to use the second argument. Otherwise, print_r() will always send its result to stdout and return 1. Without the second argument, you may see the array value on the screen, but in many cases MODX will swallow the stdout output and you won't see it.

          Your output:
          parents are : 1 

          was displaying the 1 returned from print_r(), so it didn't really tell you anything about what was in the array.

          At any rate, I'm glad you got it sorted. smiley

            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