We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37523
    • 141 Posts
    How can I return a resource ID given its parent and menuindex?

    What I am trying to do is get a related resource that has the same menuindex as the current resource but a different parent.

    Any suggestion will be greatly appreciated.

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

      Glyn Szasz
      Sydney, Australia

      Happy to help (when can) and happy to learn

      If you are a Sydney based MODX developer would love to hear from you. Please contact me.
      • 37523
      • 141 Posts
      Sorry worked it out as below:

      <?php
      $parentID = $modx->resource->get('parent');
      $menuIndex = $modx->resource->get('menuindex');
      $theparent='';
      
      if ($parentID==19) {
          $theparent=21;
      } else {
          $theparent=19;
      }
      
      $resourceObject = $modx->getObject('modResource', array('parent'=>$theparent, 'menuindex'=>$menuIndex));
      $theID = $resourceObject->get('id');
      
      return $theID;


      Is there a more efficient way to do this?
        Glyn Szasz
        Sydney, Australia

        Happy to help (when can) and happy to learn

        If you are a Sydney based MODX developer would love to hear from you. Please contact me.
        • 22427
        • 793 Posts
        Slightly shorter:
        <?php
        $parentID = $modx->resource->get('parent');
        $menuIndex = $modx->resource->get('menuindex');
        $theparent='19';
         
        if ($parentID!==19) { $theparent=21; };
         
        $resourceObject = $modx->getObject('modResource', array('parent'=>$theparent, 'menuindex'=>$menuIndex));
        $theID = $resourceObject->get('id');
         
        return $theID;
          • 3749
          • 24,544 Posts
          This might be asking for trouble, imo. If even one resource is dragged to a new location in the tree (even by accident), all the menuindex values can be rewritten.

          You could use a TV instead of menuindex, but it would be faster if you could use the introtext or description field.
            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
            • 37523
            • 141 Posts
            Many thanks ottogal, I like my code to be as compact as possible.

            As always, a great idea Bob. I will use the introtext as a kind of serial number to keep track of all the pages.

            Quote from: BobRay at Aug 07, 2014, 05:04 PM
            This might be asking for trouble, imo. If even one resource is dragged to a new location in the tree (even by accident), all the menuindex values can be rewritten.

            You could use a TV instead of menuindex, but it would be faster if you could use the introtext or description field.
              Glyn Szasz
              Sydney, Australia

              Happy to help (when can) and happy to learn

              If you are a Sydney based MODX developer would love to hear from you. Please contact me.
              • 3749
              • 24,544 Posts
              As long as you don't need it for meta tags, getResources teasers, or something else. wink

              It should be very fast.

              PS: If you like short code, this single line might work (though it's really ugly):

              return $modx->getObject('modResource', array('parent'=>$modx->resource->get('parent') == 19? 21 : 19,'introtext'=>$modx->resource->get('introtext')))->get('id');  
              


              This two-line version might work (still ugly):

              $theparent = $modx->resource->get('parent') == 19? 21 : 19;
              return $modx->getObject('modResource', array('parent'=>$theparent,
                 'introtext'=>$modx->resource->get('introtext')))->get('id');  
              



              This two-line version should work:

              $obj = $modx->getObject('modResource', array('parent'=>$modx->resource->get('parent') == 19? 21 : 19,'introtext'=>$modx->resource->get('introtext')));
              return $obj->get('id');




              This three-line version would almost certainly work:

              $theparent = $modx->resource->get('parent') == 19? 21 : 19;
              $obj = $modx->getObject('modResource', array('parent'=>$theparent,
                 'introtext'=>$modx->resource->get('introtext')));
              return $obj->get('id');



              I'm not suggesting that you necessarily use these, but it's fun to write them. wink


              [ed. note: BobRay last edited this post 12 years, 1 month ago.]
                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
                • 22303 MODX Staff
                • 10,725 Posts
                If all you need is the id, I would recommend you avoid loading the entire object—this will be much faster and use less memory:
                $criteria = array(
                    'parent' => $modx->resource->get('parent') === 19? 21 : 19,
                    'introtext' => $modx->resource->get('introtext')
                );
                return $modx->getValue($modx->newQuery('modResource', $criteria)->prepare());
                • discuss.answer
                  • 22303 MODX Staff
                  • 10,725 Posts
                  Or even better:
                  $criteria = $modx->newQuery('modResource', array(
                      'parent' => $modx->resource->get('parent') === 19? 21 : 19,
                      'introtext' => $modx->resource->get('introtext')
                  ));
                  $criteria->select('id');
                  return $modx->getValue($criteria->prepare());


                  Also, by not loading the object, you can query the data without regard for access permissions, or worrying about what kind of Resource it is (unless you need to filter certain class_key values), etc.
                    • 3749
                    • 24,544 Posts
                    Very cool! And way less memory than pulling a monster modResource object.

                    If you were getting another column, would you have to specify the column number in getValue(), or would the select() make that unnecessary?



                      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
                      • 22303 MODX Staff
                      • 10,725 Posts
                      Quote from: BobRay at Aug 08, 2014, 05:52 PM
                      Very cool! And way less memory than pulling a monster modResource object.

                      If you were getting another column, would you have to specify the column number in getValue(), or would the select() make that unnecessary?
                      The select() would make that unnecessary...