We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44195
    • 293 Posts
    Is there a way to refer to the id of a record during the creation phase?
    I ask this as obviously the database will automatically increment the id with each new insertion... so I would assume that since it hasn't been created yet I can't get the id value.

    That is unless xpdo checks what the highest value is and lets you use the id value that WILL be created. Is this the case?

    I have a directory being created whenever I create a new gallery object. Up until now I've been using the 'galleryname' but would prefer to use the id as the directory name.

    What's the recommended course of action in this case?

    Cheers!

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

      I'm lead developer at Digital Penguin Creative Studio in Hong Kong. https://www.digitalpenguin.hk
      Check out the MODX tutorial series on my blog at https://www.hkwebdeveloper.com
    • discuss.answer
      • 4172
      • 5,888 Posts
      you can get the id in the function afterSave

      public function afterSave() { 
          $id = $this->object->get('id');
          // do something with the id
      
          return true; 
      }


        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 44195
        • 293 Posts
        Thanks again Bruno.

        I just donated to your beer fund wink
          I'm lead developer at Digital Penguin Creative Studio in Hong Kong. https://www.digitalpenguin.hk
          Check out the MODX tutorial series on my blog at https://www.hkwebdeveloper.com
          • 44195
          • 293 Posts
          Is there a way of saving a new value from the afterSave() function?

          I want to append the id onto the end of the current parent directory which works but I want to save the full dir path for later when removing or updating.

          If not, I'm sure I can recalculate the path when performing a different action but it would make it much easier and would be good to know!
            I'm lead developer at Digital Penguin Creative Studio in Hong Kong. https://www.digitalpenguin.hk
            Check out the MODX tutorial series on my blog at https://www.hkwebdeveloper.com
            • 4172
            • 5,888 Posts
            Hey Muzz, thanks for the beers!

            Is there a way of saving a new value from the afterSave() function?

            you can, of course.

            Example:
            public function afterSave() {
                $id = $this->object->get('id');
                // do something with the id
            
                $this->object->set('fullpath',$this->object->get('path') . $id . '/' );
                $this->object->save();
             
                return true;
            }
            

              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 44195
              • 293 Posts
              As easy as that! Cheers!
                I'm lead developer at Digital Penguin Creative Studio in Hong Kong. https://www.digitalpenguin.hk
                Check out the MODX tutorial series on my blog at https://www.hkwebdeveloper.com
                • 3749
                • 24,544 Posts
                When using a processor for creation, you can also use the return value from runProcessor() (assuming that it's being called that way). I generally prefer to do something with the ID at that higher level, rather than in the processor itself, since you don't want to rewrite the processor every time you need to change what you do with the object's ID.

                $response = $modx->runProcessor('element/chunk/create',array(
                   'name' => 'NewChunk',
                   'description' => 'A test Chunk made with runProcessor.',
                   'snippet' => '<h3>Chunkify!</h3>',
                ));
                if ($response->isError()) {
                    return $response->getMessage();
                }
                $chunkArray = $response->getObject();
                return 'The chunk "'.$chunkArray['name'].' was created with ID '.$chunkArray['id'];


                Note that $response->getObject() does not return an object, despite its name. It returns an array.
                  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
                  • 44195
                  • 293 Posts
                  Ah excellent. Thanks Bob!
                  How would you use runProcessor with your own custom processor? Would you need to set a different namespace somehow?
                  For example if I wanted to do something like
                  $modx->runProcessor('mgr/example/myProcessor',array(...));
                  

                  in my own component?
                    I'm lead developer at Digital Penguin Creative Studio in Hong Kong. https://www.digitalpenguin.hk
                    Check out the MODX tutorial series on my blog at https://www.hkwebdeveloper.com
                    • 3749
                    • 24,544 Posts
                    You can send an array as a third argument to runProcessor.

                    $options = array(
                       'processors_path' => 'path/to/your/processors/dir/',
                    )
                      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
                      • 44195
                      • 293 Posts
                      Great! Thanks again! smiley
                        I'm lead developer at Digital Penguin Creative Studio in Hong Kong. https://www.digitalpenguin.hk
                        Check out the MODX tutorial series on my blog at https://www.hkwebdeveloper.com