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

    I've got very strang error with xPDO today.

    This simple code doesn't work

    $res = $modx->newObject('modResource');
    $res->set('parent', 0);
    $res->set('pagetitle', 'AAAAA');
    $res->set('template', 46);
    $res->save(); 
    


    If I comment $res->save() it works, else I see blank page and nothing in MODx log...

    What's happened?
      • 22303 MODX Staff
      • 10,725 Posts
      First, this is not an xPDO issue. You need to instantiate a new modResource object using the modDocument, modWebLink, modSymLink, or modStaticResource classes. modResource is the base class representing all of these subclasses and is used only when retrieving instances of all of the subclasses, but is not a valid class when saving a new instance.
        • 3749
        • 24,544 Posts
        I get that you should use:

        newObject('modDocument');


        rather than

        $resource->set('class_key, 'modDocument');


        But shouldn't the class_key default to 'modDocument' when you save a modResource object and the class_key field is not set? The OP's code has always worked for me when I tested it, and I think it's in some of my add-on components (not to mention the book sad ).


        I wonder if this might be related to user reports of getting a blank page when saving a resource in the Manager in the latest versions of MODX. [ed. note: BobRay last edited this post 14 years, 10 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
          • 22303 MODX Staff
          • 10,725 Posts
          I'm saying to use the actual class you are creating an instance of. If you just set the class_key, it may work, but it will not have any additional validation rules or other overridden behavior defined in the class itself. Consider if you override the save() method in say modStaticResource; if you don't create a static resource using this class, it will not save properly.

          Though the instance will be correct when you load the instance properly in future code or requests, by just setting the class_key, I certainly do not recommend doing so. modResource should actually be abstract for all practical purposes in the model.
            • 3749
            • 24,544 Posts
            That makes perfect sense, but my main point is really about error handling when the user instantiates a modResource object and saves it.

            Here's the resource/create processor code:

            /* get the class_key to determine resourceClass ... */
            $resourceClass = !empty($scriptProperties['class_key']) ? 
                  $scriptProperties['class_key'] 
                  : 'modDocument';


            Shouldn't the resource->save() code do the same thing?


            If modResource is really meant to be an abstract class as far as the DB is concerned, should we ever allow users to save a modResource object with a class_key of 'modResource'?

            [Update] Having looked at the code, I guess what I'm actually suggesting is this change to the save() method of modResource (unless there's some legitimate reason for creating a resource with a class_key of 'modResource' ):

                public function save($cacheFlag= null) {
                    if ($this->isNew()) {
            
                     +++if ($this->get('class_key') == 'modResource') $this->set('class_key', 'modDocument');
            
                        if (!$this->get('createdon')) $this->set('createdon', time());
                        if (!$this->get('createdby') && $this->xpdo instanceof modX) $this->set('createdby', $this->xpdo->getLoginUserID());
                    }
            [ed. note: BobRay last edited this post 14 years, 10 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
              • 3749
              • 24,544 Posts
              Bump . . . I'm concerned about this because several of my components, the MODX sample site, and I think some other components as well, create resources with a class_key of modResource.

              We can change the components, but that won't help people who've already installed them and renamed or duplicated the resources.

              I'm wondering if maybe the resource create and update processors and/or the resource->save() method should just change a class_key of modResource to modDocument before saving a 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