fat-free-framework

Getting `id` of newly created record after `save()`


As per save Cursor Method documentation

This method saves data to the database. The Cursor class automatically determines if the record should be updated or inserted as a new entry, based on the return value of dry.

In case a new record is created how do I retrieve its auto-increment primary key id (this is how my tables are defined) ?

I'm used to look for the mapper instance propery _id but this is undocumented (afaik).

I suppose I can set up an afterinsert() event handler but I was looking for a more straightforward approach. Is there one?


Solution

  • It is very easy and straighforward:

    after a save() on a "dry" record (a new record has been created) autoincrement primary keys are updated on the mapper object, so in my case:

    $mapper->save();
    $id = $mapper->id;