We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 31442
    • 10 Posts
    Hi, this is weird. I'm pretty sure that
    $modx->resource->getMany('Children')
    should return children of the current resource, and
    $modx->resource->get('id')
    should return the ID of the current resource, but for some reason on my development project, this is not the case. I get the children of ID#1 instead, even with a simple test:

    <?php
    // Get the children
    error_log(print_r($modx->resource->get('id'), 1)); // prints 1
    $children = $modx->resource->getMany('Children'); // returns children of 1
     
    if ($children) {
      // Parse children
      $retVal = '<ul>';
      foreach  ($children as $child) {
        $retVal .= '<li>Resource ' . $child->get('id') . ', child of resource ' . $child->get('parent') . '</li>';
      }
      $retVal .= '</ul>';
      return $retVal;
    }
    ?>
    


    Output, when run on page #3:

    • Resource 15, child of resource 1
    • Resource 16, child of resource 1
    • Resource 17, child of resource 1
    • Resource 18, child of resource 1

    Surely I've done something wrong. Help?

    MODX Revolution 2.2.5-pl (traditional)
      • 31442
      • 10 Posts
      Update: I took MODX Revolution to current 2.2.6-pl (traditional) and the problem persists.
        • 11055 ☆ A M B ☆
        • 3,112 Posts
        You should get the object of the current resource object, not directly from modx object.
        <?php
        $currentId = $modx->resource->get('id');
        $currentResource = $modx->getObject('modResource', $currentId);
        $children = $currentResource->getMany('Children');
        $output = '';
        if ($children) {
            $kids = array();
            foreach ($children as $child) {
                $kids[] = $child->get('pagetitle') . ' ('. $child->get('id') . ')';
            }
            $output = '<h2>' . $currentResource->get('pagetitle') . ' (' . $currentResource->get('id') . ')</h2>';
            $output .= '<ul><li>' . @implode('</li><li>', $kids) . '</li></ul>';
        }
        return $output;
        [ed. note: goldsky last edited this post 13 years, 9 months ago.]
          Rico
          Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
          MODx is great, but knowing how to use it well makes it perfect!

          www.virtudraft.com

          Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

          Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

          Maintainter/contributor of Babel

          Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
          • 31442
          • 10 Posts
          Thank you, Goldsky. The problem I'm experiencing is that this code
          $currentId = $modx->resource->get('id');
          returns a value of 1 even when the resource ID of the page containing the snippet is not 1. Given that, it doesn't matter whether you try to getMany the children directly as I did or via a getObject response. Both of them on my setup return the children of resource #1, even when run within a different resource.
            • 11055 ☆ A M B ☆
            • 3,112 Posts
            This works as expected on my installation.
            What is your resource tree structure?
            And in which resource is the snippet placed?
            +web
            ++ home
            ++ ... ???


            PS:
            I modified the code slightly.
            You should get the same ID from
            $currentId
            and the
            $currentResource->get('id')
              Rico
              Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
              MODx is great, but knowing how to use it well makes it perfect!

              www.virtudraft.com

              Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

              Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

              Maintainter/contributor of Babel

              Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
              • 31442
              • 10 Posts
              My tree looks roughly like this:

              + Home (1)
              ..- Child (15)
              ..- Child (16)
              ..- Child (17)
              ..- Child (18)

              + Patients (3)
              ..- Child (19)
              ..- Child (20)
              ..- ...
              ..- Child (26)

              So if I place the sample snippet on the page Patients (3) document, I still get the children of Home (1), 15 - 18. In fact, I copied your code fresh from above and put it into a snippet, and then called the snippet from my page template for Patients (3). I get the four children of Home (1), not the nine children of Patients (3).
                • 11055 ☆ A M B ☆
                • 3,112 Posts
                Quote from: version2beta at Dec 23, 2012, 11:38 PM
                then called the snippet from my page template for Patients (3)
                Try to put that not on template first, but in the *content area.

                And if you put that on the template, try to call it uncached using exclamation mark
                [[!testSnippet]]
                  Rico
                  Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
                  MODx is great, but knowing how to use it well makes it perfect!

                  www.virtudraft.com

                  Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

                  Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

                  Maintainter/contributor of Babel

                  Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
                  • 31442
                  • 10 Posts
                  I have placed it (uncached) in [[*content]] and added [[*content]] to the template. (It's a navigation page and doesn't otherwise show content.) The result is the same - the four children displayed are the children of Home (1), not Patient (3).
                    • 11055 ☆ A M B ☆
                    • 3,112 Posts
                    Ok, this is getting too weird for me.
                    If you don't mind, just mail (goldsky.milis at gmail dot com) me a manager access, i'll look into it.
                      Rico
                      Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
                      MODx is great, but knowing how to use it well makes it perfect!

                      www.virtudraft.com

                      Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

                      Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

                      Maintainter/contributor of Babel

                      Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
                      • 31442
                      • 10 Posts
                      I appreciate the offer. It's very generous of your time. Unfortunately, this is not my site and I have an NDA, so it's not my place to offer anyone else access.

                      My code right now for the snippet looks like this:

                      <?php
                      $currentId = $modx->resource->get('id');
                      $currentResource = $modx->getObject('modResource', $currentId);
                      $children = $currentResource->getMany('Children');
                      $output = '';
                      if ($children) {
                          $kids = array();
                          foreach ($children as $child) {
                              $kids[] = $child->get('pagetitle') . ' ('. $child->get('id') . ')';
                          }
                          $output = '<h2>' . $currentResource->get('pagetitle') . ' (' . $currentResource->get('id') . ')</h2>';
                          $output .= '<ul><li>' . @implode('</li><li>', $kids) . '</li></ul>';
                      }
                      return $output;
                      


                      As you can see, that is your code. This snippet is called as [[!getIdTest]] from the content field on document ID #3. The output is the page title and ID of the children of document ID #1.

                      I've gotten around the issue by explicitly passing the ID to the snippet call as '&start', then filtering:

                      $parent = $modx->getObject('modResource', array('id' => $start, 'published' => True));
                      $children = $parent->getMany('Children');
                      ...
                      


                      In this manner, the snippet is returning the children of the page passed in through the &start parameter.