We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 41931 ☆ A M B ☆
    • 38 Posts
    Hi,

    How create, update and delete doc via API, based on the code below:

    <?php
    // first, define what we want to create:
      $parent       = 42;
      $pagetitle    = 'Example Page';
      $longtitle    = 'Example - A New Document Created Through The Modx API';
      $alias        = 'example-page';
      $description  = 'Example description...';
      $template     = 5;
      $published    = 1;
      $content      = '<p>This is some sample content.</p>';
      $tv_img       = 'example.jpg';
      
    // secondly, create the new resource and save it:
      $doc = $modx->newObject('modDocument');
      $doc->set('parent',$parent);
      $doc->set('pagetitle',$pagetitle);
      $doc->set('longtitle',$longtitle);
      $doc->set('alias',$alias);
      $doc->set('description',$description);
      $doc->set('template',$template);
      $doc->set('published',$published);
      $doc->setContent($content);
      
      $doc->save();


    Thanks!

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

      • 3749
      • 24,544 Posts
      That should work fine (except for the TV - which needs to be handled separately), but a slightly better method would be to call the correct processor, since that will fire the appropriate System Events (e.g., OnDocFormSave).


      $fields = array(
        'parent'       => 42,
        'pagetitle'    => 'Example Page',
        'longtitle'    => 'Example - A New Document Created Through The Modx API',
        'alias'        => 'example-page',
        'description'  => 'Example description...',
        'template'     => 5,
        'published'    => 1,
        'content'      => '<p>This is some sample content.</p>',
      );
      
      $modx->runProcessor('resource/create', $fields);


      To update the resource, call:

      $modx->runProcessor('resource/update', $fields);
      [ed. note: BobRay last edited this post 11 years, 3 months 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
        • 41931 ☆ A M B ☆
        • 38 Posts
        Hi, Bob

        Thanks for the answer...

        I could not update the record, in fact I would not have to assign the Id that I need to update? Another issue in the example shown it can create a document in a folder, and in this instance you spent it creates at the root.

        Thanks
          • 3749
          • 24,544 Posts
          Oops, those should be strings on the left side of the array, not variables. See my corrected code above.

          Yes, you would need to send the ID to update an existing resource.
            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
            • 41931 ☆ A M B ☆
            • 38 Posts
            Hi,

            A practical example? I used the variable 'id' but does not work!
            $fields = array(
              'id'       => 42,
              'pagetitle'    => 'Example Page',
              'longtitle'    => 'Example - A New Document Created Through The Modx API',
              'alias'        => 'example-page',
              'description'  => 'Example description...',
              'template'     => 5,
              'published'    => 1,
              'content'      => '<p>This is some sample content.</p>',
            );
             
            $modx->runProcessor('resource/update', $fields);
              • 41931 ☆ A M B ☆
              • 38 Posts
              Hi, Bob

              I got the parameters:
              
              
              //Add
              $res = $modx->newObject('modDocument');
              $res->set('parent',42);// parent or no
              $res->set('pagetitle','New Title');
              $res->setContent('This is your long text explaining about your pagetitle.');
              $res->set('published',1);
              $res->save();
              
              //Update
              $res = $modx->getObject('modResource',96);
              $res->set('pagetitle','Change Title');
              $res->set('content','<p>This is some sample contents.</p>');
              $res->save();
              
              //Remove
              $res = $modx->getObject('modResource',96);
              $res->remove();
              


              My question is how to handle the error message for Error function is been added, updated or deleted. Can you help me?

              Error: Call to a member function remove() on a non-object [ed. note: rocaoliveira last edited this post 11 years, 3 months ago.]
              • discuss.answer
                • 3749
                • 24,544 Posts
                //Remove
                $res = $modx->getObject('modResource',96);
                if ($res) {
                    $res->remove();
                } else {
                   // optional
                   $modx->log(modX::LOG_LEVEL_ERRROR, 'Resource ' . 96 . ' not found.');
                }
                [ed. note: BobRay last edited this post 11 years, 2 months 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
                  • 41931 ☆ A M B ☆
                  • 38 Posts
                  Hi, Bob

                  Working fine! Thanks!
                    • 3749
                    • 24,544 Posts
                    Glad I could help. smiley
                      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