We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 16815
    • 49 Posts
    I have created a Custom Manager Page (following the instructions in the documentation), where I try to list a set of resources which are children of another resource. So before the RETURN statement in the controller file I try to list all ids using getChildIds() and passing in the parent ID. But when I var_dump() the array it’s empty. I have done a little testing in the getChildIds() function to see where it breaks, and its in the following code (the bold line):

    if ($id !== null && intval($depth) >= 1) {
    $id= intval($id);
    if (isset ($this->resourceMap["{$id}"])) {
    if ($children= $this->resourceMap["{$id}"]) {
    foreach ($children as $child) {

    Anyone who has a solution or tip? Is there a way to set the resourceMap before using the getChildIds()? Or is there a better way to search for children ID’s?

    Thanks
      • 26903
      • 1,336 Posts
      Try this before you call the getChildIds :-
      $this->modx->switchContext($document[’context_key’]);

      In your 3PC component your context is ’mgr’ so your resource map doesn’t exist. You need to switch to the context the resource is in as set in the resource itself.

      Depending on exactly what you are doing you may need to switch back to ’mgr’ after the call to getChildIds.
        Use MODx, or the cat gets it!
        • 22303 MODX Staff
        • 10,725 Posts
        I really don’t recommend using it for that purpose. It’s designed for front-end context usage, not for use in a CMP. Also, the manager has different permissions than the front-end so the behavior would be unpredictable.

        Do you only need the id’s? And only of the immediate children? Some statement of your goals might help us direct you to the best way to approach it...
          • 16815
          • 49 Posts
          The goal of the CMP is to let the client generate pdf’s of the resources. All the resources are children of the same parent. So if I can fetch the ID’s of all children to that parent, then I can loop through that array and get the information I need from each children.

          I just need the ID’s of all children, when I have the ID’s I use $modx->getObject(’modResource’, array(’published’ => 1, ’id’ => $id)); to retrieve the information I need from that specific resource.

          Do I need to query the database, or maybe I can use the getResource snippet in some way?

          Hope this clarifies a bit smiley
            • 3749
            • 24,544 Posts
            Try this:

            $resource = $modx->getObject('modResource', 12);
            
            if ($resource->hasChildren()) {
                $childen = $modx->resource->getMany('Children');
            }
            


            Change 12 to the id of the parent.
              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
              • 16815
              • 49 Posts
              Thanks BobRay! The following code works perfectly:

              $resource = $modx->getObject('modResource', 2);
              if($resource->hasChildren()) {
              	$children = $resource->getMany('Children');
              }
              $resourceIds = array();
              foreach ($children as $child) {
              	$resourceIds[] = $child->get('id');
              }


              If you see any optimization possibilities please let me know smiley

              Is there a list of parameters that the getMany() function will accept? Why do I need to write ’Children’ with a capitalized ’C’?

              Thanks again smiley
                • 22303 MODX Staff
                • 10,725 Posts
                Quote from: leaase at May 16, 2010, 05:26 AM

                If you see any optimization possibilities please let me know smiley
                Here is an optional approach that is slightly more optimal. Since you don’t actually use the parent Resource, no reason to query and load it from the database. Also, if you select() only the columns you need from the object, you optimize the query and the amount of memory used by each object.
                <?php
                /* build a query to get Resources with a parent of 2 that are published and not deleted */
                $query = $modx->newQuery('modResource', array('parent' => 2, 'deleted' => false, 'published' => true));
                
                /* to optimize, you can select only the columns you need */
                $query->select($modx->getSelectColumns('modResource', 'modResource', '', array('id', 'class_key', 'context_key')));
                
                /* get the modResource objects with a parent of 2 */
                $children = $modx->getCollection('modResource', $query);
                
                /* loop through the children and push the id onto $resourceIds */
                $resourceIds = array();
                foreach ($children as $child) {
                    $resourceIds[] = $child->get('id');
                }
                ?>



                Quote from: leaase at May 16, 2010, 05:26 AM

                Is there a list of parameters that the getMany() function will accept? Why do I need to write ’Children’ with a capitalized ’C’?
                It takes whatever relation aliases are defined for the object you are working with, or rather, all the ones with a cardinality of many. In this case, modResource defines the following relationships to other objects in the MODx model:
                        <aggregate alias="Parent" class="modResource" local="parent" foreign="id" cardinality="one" owner="foreign" />
                        <aggregate alias="Children" class="modResource" local="id" foreign="parent" cardinality="many" owner="local" />
                        <aggregate alias="CreatedBy" class="modUser" local="createdby" foreign="id" cardinality="one" owner="foreign" />
                        <aggregate alias="EditedBy" class="modUser" local="editedby" foreign="id" cardinality="one" owner="foreign" />
                        <aggregate alias="DeletedBy" class="modUser" local="deletedby" foreign="id" cardinality="one" owner="foreign" />
                        <aggregate alias="PublishedBy" class="modUser" local="publishedby" foreign="id" cardinality="one" owner="foreign" />
                        <aggregate alias="Template" class="modTemplate" local="template" foreign="id" cardinality="one" owner="foreign" />
                        <aggregate alias="TemplateVars" class="modTemplateVar" local="id:template" foreign="contentid:templateid" cardinality="many" owner="local" />
                        <aggregate alias="TemplateVarTemplates" class="modTemplateVarTemplate" local="template" foreign="templateid" cardinality="many" owner="local" />
                        <aggregate alias="ContentType" class="modContentType" local="content_type" foreign="id" owner="foreign" cardinality="one" />
                        <aggregate alias="Context" class="modContext" local="context_key" foreign="key" owner="foreign" cardinality="one" />
                        <composite alias="TemplateVarResources" class="modTemplateVarResource" local="id" foreign="contentid" cardinality="many" owner="local" />
                        <composite alias="ResourceGroupResources" class="modResourceGroupResource" local="id" foreign="document" cardinality="many" owner="local" />
                        <composite alias="ResourceKeywords" class="modResourceKeyword" local="id" foreign="content_id" cardinality="many" owner="local" />
                        <composite alias="ResourceMetatags" class="modResourceMetatag" local="id" foreign="content_id" cardinality="many" owner="local" />
                        <composite alias="Acls" class="modAccessResource" local="id" foreign="target" owner="local" cardinality="many" />
                

                The relationships defined with a cardinality of one would be loaded using $resource->getOne(), e.g.
                <?php
                $resource->getOne('Context');
                ?>

                Also note that you can access the related objects loaded with getOne()/getMany() directly from the $resource:
                <?php
                if ($resource->getOne('Context')) {
                    print_r($resource->Context->toArray());
                }
                
                if ($resource->getMany('Children')) {
                    foreach ($resource->Children as $child) {
                        print_r($child->toArray());
                    }
                }
                ?>
                  • 16815
                  • 49 Posts
                  Thanks for the code and answers OpenGeek. It’s really helpfull in my quest to understand what modx is capable to do smiley