We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4172
    • 5,888 Posts
    Quote from: splittingred at Dec 08, 2010, 02:42 PM

    In 2.0.5 you can just do:

    $response = $modx->runProcessor('resource/create',array(
       'pagetitle' => 'Fun Page',
       'published' => true,
       'content' => 'stuff here',
       /* etc */
    ));
    if ($response->isError()) {
       if ($response->hasFieldErrors()) {
           $fieldErrors = $response->getAllErrors();
           $errorMessage = implode("\n",$fieldErrors);
       } else {
           $errorMessage = 'An error occurred: '.$response->getMessage();
       }
       return $errorMessage;
    }
    return 'Success!';
    




    great! I will try that with formit2resource.
      -------------------------------

      you can buy me a beer, if you like MIGX

      http://webcmsolutions.de/migx.html

      Thanks!
      • 3749
      • 24,544 Posts
      How can I get the ID of the newly created object following this code?

      I can use getObject() with the name, but if there are duplicate names, I’m in big trouble.

          $response = $this->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();
               }
               $this->setError($errorMessage);
                     return '';
          }
      


      I tried these without success:
          $this->resource = $response->getobject();
          $this->resource = $this->modx->error->getobject();


      Is lastInsertId() reliable on a busy site?
        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
        • 21872
        • 95 Posts
        I don’t know that this is right or anything, but this seems to get the id:

        $resource = $modx->runProcessor('resource/create', $site_content);
        $resource->response['object']['id']; 
          • 3749
          • 24,544 Posts
          Thanks. That looks right. I’ll give it a shot.
            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
            • 28215
            • 4,149 Posts
            Quote from: BobRay at Dec 10, 2010, 11:19 PM

            How can I get the ID of the newly created object following this code?

            $object = $response->getObject();
            $id = $object['id'];


            Note that it’s getObject, not getobject.
              shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
              • 3749
              • 24,544 Posts
              Quote from: splittingred at Dec 13, 2010, 09:27 AM


              $object = $response->getObject();
              $id = $object['id'];


              Note that it’s getObject, not getobject.

              Ouch. tongue
                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
                • 3749
                • 24,544 Posts
                No, I had it right in the code. The call to the processor is successful and the new resource is created, but I’m not getting the resource object with $response->getObject().
                I’m getting back the equivalent of resource->toArray().

                This works:

                $objectArray = $response->getObject();
                $this->resource = $this->modx->getObject('modResource',$objectArray['id']);
                

                Shouldn’t getObject() just return the object if there is one and null if not rather than an array of the objects fields?

                If not, maybe it should be called getObjectArray(), getObjectFields() or getObjectToArray() to avoid confusion.
                  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
                  • 34017
                  • 898 Posts
                  Quote from: splittingred at Dec 08, 2010, 02:42 PM

                  In 2.0.5 you can just do:

                  $response = $modx->runProcessor('resource/create',array(
                     'pagetitle' => 'Fun Page',
                     'published' => true,
                     'content' => 'stuff here',
                     /* etc */
                  ));
                  if ($response->isError()) {
                     if ($response->hasFieldErrors()) {
                         $fieldErrors = $response->getAllErrors();
                         $errorMessage = implode("\n",$fieldErrors);
                     } else {
                         $errorMessage = 'An error occurred: '.$response->getMessage();
                     }
                     return $errorMessage;
                  }
                  return 'Success!';
                  





                  Shaun,

                  I just think I fell in love all over again. runProcessor opens up a world of possibilities and is just sick awesome. Devs everyone should love this.

                  Im speechless. Is this stable? Does digitalbutter know about this since they’re about have a hackathon for a frontend editing CMP?
                    Chuck the Trukk
                    ProWebscape.com :: Nashville-WebDesign.com
                    - - - - - - - -
                    What are TV's? Here's some info below.
                    http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
                    http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008
                    • 3749
                    • 24,544 Posts
                    Good point. In all my whining, I forgot to mention how cool this is.

                    It saved me hours of messing with permission issues in NewsPublisher.

                    Based on discussions with Shaun and Jason elsewhere, I would say that it’s not totally stable at this point. The way it is now, values in the $_POST array will take precedence over those you send in the $scriptProperties and that may change. If you make the $_POST match your $scriptProperties (if there’s any potential issue there) before calling it, though, it should work fine in the future.
                      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
                      • 34017
                      • 898 Posts
                      There’s 2 things I think would be useful for devs:
                      - 1) preprocess the $_POST and send the results to runProcessor so runProcessor
                      - 2) A flag to tell it not check permissions (It might be rare but I can see it coming in handy)

                      Answer 1) I could extend modx::runProcessor to fix #1
                      Answer 2) It would be nice if action resource/create.php was a class so it could be extended. However, for #2, it should be just as to copy/rename/edit this action to remove the permissions.

                      Are these how you see revo being used Shaun, Jason?
                        Chuck the Trukk
                        ProWebscape.com :: Nashville-WebDesign.com
                        - - - - - - - -
                        What are TV's? Here's some info below.
                        http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
                        http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008