We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1829
    • 15 Posts
    Hello!

    I have MODX Revolution 2.2.2-pl (traditional)

    I need to create the resource outside the ModX, here is the code:
    <?php
    require_once 'W:/home/site.loc/www/config.core.php';
    require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
    $modx = new modX();
    $modx->initialize('web');
    $modx->getService('error', 'error.modError');
    
    $fields = array(
        'pagetitle' => 'Test',
        'template' => 2,
        'alias' => 'test-alias'
    );
    
    $response = $modx->runProcessor('resource/create', $fields);
    if ($response->isError()) {
        var_dump($modx->error->failure($response->getMessage()));
        die;
    }
    
    var_dump($response->response['object']);
    die;
    ?>


    Here is the result:
    array(5) {
    ['success']=>
    bool(false)
    ['message']=>
    array(0) {
    }
    ['total']=>
    int(0)
    ['errors']=>
    array(0) {
    }
    ['object']=>
    array(0) {
    }
    }


    Any ideas why an error occures?
    Thanks ia advance!
      • 34926
      • 87 Posts
      I am receiving the exact same problem. Did you manage to work around this in any way?
        • 3749
        • 24,544 Posts
        Are you logged in in the front end as a user with the proper permissions to create a resource?

        This might help you check:

          
        <?php
        if (!$tmodx->hasPermission('new_document')) {
               return ('no new_document permission');
        }
        


        ------------------------------------------------------------------------------------------
        PLEASE, PLEASE specify the version of MODX you are using.
        MODX info for everyone: http://bobsguides.com/modx.html
          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
          • 34926
          • 87 Posts
          It doesn't seem to be MODx permissions related. I gave up in the end and went down the route of creating an object instead.

          $doc = $modx->newObject('modDocument');
          $doc->fromArray($fields);
          $doc->save();
            • 3749
            • 24,544 Posts
            I wonder if you might need this instead:

            $modx->initialize('mgr');



            ------------------------------------------------------------------------------------------
            PLEASE, PLEASE specify the version of MODX you are using.
            MODX info for everyone: http://bobsguides.com/modx.html
              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
              • 22303 MODX Staff
              • 10,725 Posts
              Try adding a line after the getService() call to load modError:
              $modx->getService('error', 'error.modError');
              $modx->error->message = '';


              I ran into this problem the other day and the issue seems to be that $modx->error->message is being initialized as an empty array instead of an empty string, which makes it always evaluate to hasError() === true. Setting this when getting the service explicitly to an empty string fixes the issue.

              I'm looking into addressing this issue today, in core. [ed. note: opengeek last edited this post 14 years, 2 months ago.]
                • 3749
                • 24,544 Posts
                Does that happen only when running outside of MODX? NewsPublisher uses this code with no problems (running inside of MODX):

                if ($this->existing) {
                            $response = $this->modx->runProcessor('resource/update', $fields);
                        } else {
                                $response = $this->modx->runProcessor('resource/create', $fields);
                        }
                        /* @var $response modProcessorResponse */
                        if ($response->isError()) {
                            if ($response->hasFieldErrors()) {
                                $fieldErrors = $response->getAllErrors();
                                $errorMessage = implode("\n", $fieldErrors);
                            } else {
                                $errorMessage = 'An error occurred: ' . $response->getMessage();
                            }
                            $this->setError($errorMessage);
                            return '';
                
                        }



                ------------------------------------------------------------------------------------------
                PLEASE, PLEASE specify the version of MODX you are using.
                MODX info for everyone: http://bobsguides.com/modx.html
                  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
                  • 22303 MODX Staff
                  • 10,725 Posts
                  Quote from: BobRay at Jul 28, 2012, 12:29 AM
                  Does that happen only when running outside of MODX? NewsPublisher uses this code with no problems (running inside of MODX):
                  It does seem to be limited to running it externally.