We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42887
    • 19 Posts
    Hi everyone,

    Today I have noticed that I am unable to use a CMP to publish a modResource. I have a CMP that is basically a form for coworkers to fill out and when they hit 'save', it takes the data and saves it to my custom table and creates a resource with it.

    However, even though I have 'published_default' set to yes, the resource will not publish (even when viewed from front end) until I SAVE the resource manually in manager.

    Here is my code:

    $doc = $modx->newObject('modResource');
    $doc->set('createdby', $user);
    $doc->set('template', 2);
    $doc->set('published', 1);
    $doc->set('alias', $newtime);
    $doc->set('pagetitle', $_POST['title']);
    $doc->set('parent', 161);
    $doc->set('content', $_POST['body']);
    $doc->save();


    I have tried setting the 'publishedon', 'editedon', etc. with no luck. It will only be viewable from the front end AFTER I find the resource in the file tree, open it, make a change to it and then hit 'save'. Any ideas what I am missing?

    And I found this article was helpful: http://bobsguides.com/published-on-publish-date-confusion.html (as always...Bob's Guides is a huge help!)

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

    [ed. note: modx125 last edited this post 13 years, 1 month ago.]
    • discuss.answer
      • 3749
      • 24,544 Posts
      I think you might be seeing an alias map issue. The resource is published and exists, but won't show up because it's not in the alias map yet. When you save a resource in the Manager, the cache is cleared automatically and the alias map is updated, but not when you do it in code. BTW, $user is an ID right? (It needs to be.) You might consider calling it $userId to make that clearer. If you're going to write much MODX code, it will make your life easier if you use 'Obj' and 'id' as suffixes on any MODX object variables.

      The first thing I'd try is putting this at the end of the code:

      $modx->reloadContext('web');


      That will recreate the alias map.
      If that doesn't work, try this instead:

      $cm = $modx->getCacheManager();
      $cm->refresh();


      If that doesn't work, try both. wink

      BTW, the preferred way create resources now is with the 'resource/create' processor. It will handle some housekeeping for you and check permissions (though I think you'd still need to update the alias map).


      $fields = array(
          'createdby' => $userId,
          'template' => 2,
          'published' => 1,
          'alias' => $newtime,
          'pagetitle' => $_POST['title'],
          'parent' => 161,
          'content' => $_POST['body'],
      );
      
      $modx->runProcessor('resource/create', $fields);
        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
        • 42887
        • 19 Posts
        Quote from: BobRay at Sep 14, 2013, 11:08 PM
        I think you might be seeing an alias map issue....

        The first thing I'd try is putting this at the end of the code:

        $modx->reloadContext('web');


        That will recreate the alias map.

        Once again, Bob to the rescue! I added that line at the end of my code and this cured my issue.

        Quote from: BobRay at Sep 14, 2013, 11:08 PM
        BTW, the preferred way create resources now is with the 'resource/create' processor.

        Thank you for this, I will update my code. I wasn't aware of the preferred way...seems easier as well!

        Quote from: BobRay at Sep 14, 2013, 11:08 PM
        BTW, $user is an ID right? (It needs to be.) You might consider calling it $userId to make that clearer. If you're going to write much MODX code, it will make your life easier if you use 'Obj' and 'id' as suffixes on any MODX object variables.

        Yes, $user is an ID. I will take your suggestion into play because I see how it would help in the future. I do plan to be writing MODX code for a while so little suggestions like this make a big difference to me. Thank you for all of your help and for your great website full of hints. Really appreciate it!

          • 3749
          • 24,544 Posts
          Thanks for the kind words, and for reporting back. I'm glad you got it sorted. 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