We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22303 MODX Staff
    • 10,725 Posts
    You don't need lastinsertid() when using xPDO in most cases. The primary key is updated automatically in the in-memory object on save(). It might help if you post specific questions and we can better identify holes in documentation that needs to be addressed.

    That said, the way things were done in the legacy MODX system (Evo) do not always translate one for one to the new ways of doing things. I find it easiest to learn by example; reading the code written by others. You might also want to reference the API documentation at http://api.modx.com/revolution/2.2/ which is much more up-to-date than the class reference section at rtfm.
      • 14372
      • 49 Posts
      Well ... haven't actually written the code yet but its pretty easy to explain what I wanted to do ...


      • create new object - newObject()
      • assign data to various fields of the object - set()
      • save the object - save()

      Now ... the object has an id (auto-incremented in the DB) there I don't actually choose what it is ...

      I was looking for a method that returns the value that was assigned to the id (primary key if you wish)
        • 22303 MODX Staff
        • 10,725 Posts
        As I said, when you save the object, the primary key field is updated automatically, so just get() the field value or call getPrimaryKey() from the object you saved, e.g.

        <?php
        $object = $modx->newObject('Class', $fieldData);
        $object->save();
        /* get the autoincrement field by name */
        $pk = $object->get('id');
        /* or get it via getPrimaryKey 
        (NOTE: may be an array if multiple fields are included in PK) */
        $pk = $object->getPrimaryKey();
          • 14372
          • 49 Posts
          Thanks for the prompt responses. Actually I agree that your method works. The issue in my view was where is it located in the docs? ... because there were several methods suggested here and I wanted to update the docs with what I was sure is the right method ...

          Requested for this access a while ago and got it ... I'd seen earlier that I have the edit button on the top right when on the xPDO docs ...