I have a simple model, just a few columns, but it uses an autoincrementing id that my database backend generated as needed.
What I need is a way to get that id, after I call ->save() on a new instance of my model.
$a = new Address;
$a->street=$_POST["popup_street"];
$a->city=$_POST["popup_city"];
$a->state=$_POST["popup_state"];
$a->zip=$_POST["popup_zip"];
$a->save();
$a->id; // this is still null.
I thought about this:
Generating a big 'ole complex GUID for the street field,
Calling ->save(),
Do a lookup on the GUID I used for the street,
Now that I have a "real" record in my model object,
Push my real data into the model object,
Call ->update();
But that makes me feel dirty.
Is there another way to do this that seems less… hacky?
I had some code that was processing all the property values in the array before saving. it was also ‘processing’ the ‘id’ field (for no good reason other than it was included in all the other fields), which means the id field was getting set to an empty string (or null? not sure). The subsequent save() method worked no problem - and set the primary key ID field to the the next integer value (it was set to auto-increment) in the database. However, it did not replace the id value in the object in my php code with the new ID after the save. It was still set to the empty string (or maybe null - I can’t recall what value what in there) that I had put in there.
This was tough because the database values were correct, and there were no errors. But, I had no access (even after refresh) to the ID value.
Spent 20 minutes trying to find this, and thought it might help someone down the line.