We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17959 ☆ A M B ☆
    • 57 Posts
    Magnatron [Maarten Wolzak] Reply #1, 14 years, 8 months ago
    Hi all,

    I ran into a bug(?) when trying to create a modResource programmatically.
    I use the following script:
    (
    user = logged in, check
    $parentId = set, check
    $templateId = set, check
    $rq['pagetitle'] = set, check
    )

    $page = $modx->newObject('modResource');
    $page->set('createdby', $modx->user->id);
    $page->set('createdon', time());
    $page->set('parent', $parentId);
    $page->set('template', $templateId);
    $page->set('alias', $page->cleanAlias($rq['pagetitle']));
    // $page->set('uri', $page->cleanAlias($rq['pagetitle']).".html");
    // $page->set('uri_override',1);
    $page->set('hidemenu',1);
    $page->set('menuindex',count($modx->getCollection('modResource',array('template'=>$templateId))));
    $page->set('pagetitle', $rq['pagetitle']);
    $saved = $page->save();


    it throws the following error:

    [21-Feb-2012 13:08:25] PHP Fatal error: Call to a member function getOption() on a non-object in /..../core/model/modx/modresource.class.php on line 853

    Using 2.2.0-pl2(traditional)

    Adding the uri & uri_override makes the modresource.class.php skip the routine containing line 853 and makes it possible to create a modResource just fine except that I do not want to override the default URI...

    Any ideas or might this be a bug?

    Thanks!
      • 3548
      • 102 Posts
      Is this inside snippet or are you using modx externally?

      If it is externally you should set/initialize context.
        Antonio Zdilar
        linearvector.com
        • 17959 ☆ A M B ☆
        • 57 Posts
        Magnatron [Maarten Wolzak] Reply #3, 14 years, 8 months ago
        Hi Blankcheck,

        thanks for your reply.

        This code is inside a snippet.
          • 3548
          • 102 Posts
          Well, this code works. Did you change something regarding contexts?
            Antonio Zdilar
            linearvector.com
            • 17959 ☆ A M B ☆
            • 57 Posts
            Magnatron [Maarten Wolzak] Reply #5, 14 years, 8 months ago
            Thanks for your replies.

            So you tested the code and it worked fine without setting the uri and uri_override?

            There's no changes in the context in my setup.

            So it might just be a bad migration... bummer.

              • 3548
              • 102 Posts
              Yes, everything works as expected.

              Regards
                Antonio Zdilar
                linearvector.com
                • 3749
                • 24,544 Posts
                I'd recommend creating the resource with the create processor. The createdon field will be set automatically and I think the pagetitle and alias will be cleaned automatically. the createdby field might also be set for you, I'm not sure.

                <?php 
                $fields['createdby'] = $modx->user->id;
                $fields['parent'] = $parentId;
                $fields['template'] = $templateId;
                $fields['alias'] = $rq['pagetitle'];  
                $fields['hidemenu'] = 1;
                $fields['pagetitle'] = $rq['pagetitle'];
                $response = $modx->runProcessor('resource/create', $fields);
                
                if ($response->isError()) {  /* An error occurred */
                    if ($response->hasFieldErrors()) {
                        $fieldErrors = $response->getAllErrors();
                        $errorMessage = implode("\n", $fieldErrors);
                    } else {
                        $errorMessage = 'An error occurred: ' . $response->getMessage();
                    }
                   $modx->log(xPDO::LOG_LEVEL_ERROR,$errorMessage);
                }
                
                /* if you need the ID */
                
                $object = $response->getObject();
                
                $docId = $object['id'];
                



                ---------------------------------------------------------------------------------------------------------------
                PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
                MODX info for everyone: http://bobsguides.com/modx.html [ed. note: BobRay last edited this post 14 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
                  • 17959 ☆ A M B ☆
                  • 57 Posts
                  Magnatron [Maarten Wolzak] Reply #8, 14 years, 7 months ago
                  Hi BobRay,

                  Thanks for your answer. I have to do some work on the code you suggested because it doesn't work as expected but it seems the right way to go.
                  [ed. note: Magnatron last edited this post 14 years, 7 months ago.]
                    • 3749
                    • 24,544 Posts
                    Done. Sorry about that. wink
                      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
                      • 38677
                      • 21 Posts
                      Sorry to have to bump this topic. I need some help on this though.

                      I have tried both variants to create resources through a snippet dynamically - and have issues with both of them. We are working with a 2.2.0pl here. Linux, PHP5. The snippet is called from a published resource which I preview/view.

                      Magnatron's solution basically work. When I run the snippet, then the resource is created with the general settings I give it. However, when I try to (pre)view it out of the manager, then nothing happens (blank page, no url in address bar). If I try to access the page through page ID or FURL, I get a 401 error. If I then save the resource (without making any changes) in the manager, it will work afterwards. I made sure all the resource settings are the same as manually created resources.

                      Bob's solution creates another set of problems. If I run the code as is, I get the following error:

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

                      If i change the code to

                      $response = $modx->runProcessor('resource/create', $fields);

                      Then I get

                      [2012-08-10 10:13:47] (ERROR @ /index.php) An error occurred: permission_denied

                      Any help is appreciated.