We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17016
    • 138 Posts
    Hi Bob,
    thanks a lot for your corrections of the code.

    We have a folder which contains always exactly one child. How can we assign the ID of the child to the variable $childid?

    We tried this by the code written above but we don´t know how to extract the ID from $children

    $resource = $modx->getObject('modResource', 1158);
    $children = $resource->getMany('Children');
    
    $childid = ???


      • 33968
      • 863 Posts
      Hi Letti, I think you still have to use getMany even if you are sure there is only one child. That’s just because the Parent/Child relationship defined in the schema is One-to-Many.

      You can get the child id (and any other field) like this:
      <?php
      $resource = $modx->getObject('modResource', 3);
      $c = $modx->newQuery('modResource');  // limit the query results to the first child
      $c->limit(1);                         // object found, just in case there are more ;)
      $children = $resource->getMany('Children', $c);
      
      if (!empty($children)) {
          foreach ($children as $child) {
              $childid = $child->get('id');
          }
      }
      
      return $childid;
      


      Does that work for you? Not sure if there’s a more efficient way to do that...
        • 17016
        • 138 Posts
        Hi lucas,
        thanks a lot. That works!

        Do you maybe also know how to get the id of the resource from which a Custom Manager Page was startet/called and how to return to this site after the Script has done?

        I tried to realize it by these lines but it didn´t work:
        $query = parse_url($_SERVER["HTTP_REFERER"] , PHP_URL_QUERY);
        parse_str($query, $parameters);
        return $parameters[id];

        Do you have any idea what I did wrong?
          • 17016
          • 138 Posts
          I finally managed to get the ID from the site on which the Custom Manager Page was started by these lines but I doubt there are no better ways to manage it:
          $query = parse_url($_SERVER["HTTP_REFERER"] , PHP_URL_QUERY);
          parse_str($query, $parameters);
          return $parameters["amp;id"];

          Is this method OK, or is there better way to realize this?
            • 3749
            • 24,544 Posts
            Quote from: lucas at Aug 06, 2011, 10:29 AM

            Hi Letti, I think you still have to use getMany even if you are sure there is only one child. That’s just because the Parent/Child relationship defined in the schema is One-to-Many.

            You can get the child id (and any other field) like this:
            <?php
            $resource = $modx->getObject('modResource', 3);
            $c = $modx->newQuery('modResource');  // limit the query results to the first child
            $c->limit(1);                         // object found, just in case there are more ;)
            $children = $resource->getMany('Children', $c);
            
            if (!empty($children)) {
                foreach ($children as $child) {
                    $childid = $child->get('id');
                }
            }
            
            return $childid;
            


            Does that work for you? Not sure if there’s a more efficient way to do that...

            That’s a good way to go if you need to do something with the child object (which you probably do). You can get any of it’s fields with $resource->get(’fieldName’); There may be no need to get the ID since you already have the child object from the getMany() call.


            If all you really want is the ID, though, you can do this:

            $children = $modx->getChildIds($modx->resource->get('id'));


            That will get you an array containing the child ids.

            I think if there is only one, the id will be available this way:

            $childId =  $children[0];


              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
              • 17016
              • 138 Posts
              Quote from: BobRay at Aug 07, 2011, 03:12 AM

              If all you really want is the ID, though, you can do this:
              $children = $modx->getChildIds($modx->resource->get('id'));

              That will get you an array containing the child ids.
              I think if there is only one, the id will be available this way:
              $childId =  $children[0];

              Hi Bob, thanks a lot. We only need the id because of transfering a site by a API-connection to our newsletter-tool what we realize by $urlofthefinishedmailing = $modx->makeUrl($idofthemailinginmodx); and $mailing = file_get_contents($urlofthefinishedmailing) and transferring afterwards the variable $mailing by our API-Connection to the newsletter-tool.

              On your site: http://bobsguides.com/revolution-objects.html you mentioned concerning "Creating New objects" that this "should be done with care". When we create a new mailing from MODx via API-connetion in our newsletter-tool we also have to create a new document in MODx containing the mailing and amongst others the mailing-id of the mailing in our newsletter-tool. For this we were thinking to realize this by this code. Is this OK or are there any pitfalls we are not aware of:

              $object = $modx->newObject('modResource');
              $object->set('pagetitle', $mailing-name);
              $object->set('alias', $mailing-name);
              $object->set('template', '51');
              $object->set('OurTemplateVariableForTheMailingID', '2982882'); // To set the Mailing-ID 2982882 to our TV
              $object->save();
              


              The resource should be in one case a modDocument and in another case a modSymLink. Is it possible to realize this by something like $object->set(’class_key’, ’modDocument’); I couldn´t find anything in the list at the bottom of your page "Understanding MODx Revolution Objects" concerning this point. Or do we have to realize this in another way?

              The same question for defining the ’parent’ where the resource should be a child of. Is it possible to add something like this: $object->set(’parent’, ’1299’);

              After created this new resource we would get the id of this new resource by this:

              $doc = $modx->getObject('modResource', array('pagetitle'=>'thenewgivenpagetitle'));
              $idofnewresource = $doc->get('id');
              $modx->sendRedirect("https://www.thedomain.com/manager/?a=30&id=".$idofnewresource);
              


              Is this way OK? Or does maybe exist a better one??

              Thanks again for your great work concerning MODx

              Letti
                • 3749
                • 24,544 Posts
                Most of that would work, but you can’t set a TV that way. You can also call the MODX create processors directly, which might be easier. They’ll use the defaults for hidemenu, published, searchable, cacheable, etc. That would look something like this:

                <?php
                $fields = array();
                
                $fields['pagetitle'] = $mailing-name;
                $fields['alias'] = $mailing-name;
                $fields['template'] = '51';
                $fields['parent'] = '1299';
                
                $idOfMailingTv = ##;  // replace with the ID of the TV.
                        
                $fields['tvs'] = true;
                $fields['tv' . $idOfMailingTv] = '2982882';
                
                
                if ($doSymlink){
                    $response = $modx->runProcessor('symlink/create', $fields);
                } else {
                    $response = $modx->runProcessor('resource/create', $fields);
                }
                    
                if ($response->isError()) {
                    if ($response->hasFieldErrors()) {
                        $fieldErrors = $response->getAllErrors();
                        $errorMessage = implode("\n", $fieldErrors);
                    } else {
                        $errorMessage = 'An error occurred: ' . $response->getMessage();
                    }
                    return $errorMessage;
                
                } else {
                    $object = $response->getObject();
                
                    $idofnewresource = $object['id'];
                
                    $modx->sendRedirect("https://www.thedomain.com/manager/?a=30&id=".$idofnewresource);;
                }
                
                  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
                  • 17016
                  • 138 Posts
                  Hi Bob,
                  thanks a lot for your help. This looks very comfortable. Unfortunately we get an error at the moment which I couldn´t resolve by myself:

                  Fatal error: Using $this when not in object context concerning this line:

                  $response = $this->modx->runProcessor(’symlink/create’, $fields);
                  or when trying to create a resource:
                  $response = $this->modx->runProcessor(’resource/create’, $fields);

                  Do you see what we maybe did wrong?
                    • 3749
                    • 24,544 Posts
                    Sorry, I cribbed the code from NewsPublisher where it’s inside a class.

                    Just change "$this->modx->runSnippet(" to "$modx->runSnippet(" on both lines.

                    I’ve fixed it in the code above if you want to re-paste it.

                      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
                      • 17016
                      • 138 Posts
                      Ok, this line seems to work but now it stops on:

                      Fatal error: Call to a member function isError() on a non-object in line:
                      if ($response->isError()) {

                      For some reason $response seems not to contain the right content or at least not in the correct manner. Do you have any idea what could the reason for this?