Channel-Data icon indicating copy to clipboard operation
Channel-Data copied to clipboard

Issue with models sans auto-incrementing primary key

Open notacouch opened this issue 10 years ago • 0 comments

One of my models is just keeping track of data in a third party service. The id field is set to theirs. Saving the model resets that id field to 0 because the insert_id is forced on it here: https://github.com/objectivehtml/Channel-Data/blob/c8ce9d94346588bec4cbb8131808d4371e53210c/channel_data/libraries/Channeldata/base/BaseModel.php#L240

Instead of editing that I had just copied the method and removed that bit of code for that particular model, but while documenting it hit me a condition could be used there instead, save the insert_id if it is not zero.

E.g. instead of:

    $this->setAttribute($this->idField, ee()->db->insert_id());

Perhaps change to (untested):

    $this->setAttribute($this->idField, ee()->db->insert_id() ? : $this->getAttribute($this->idField)); // double-checked, shorthand ternary is okay since Channel-Data requires PHP 5.3+ anyways 

notacouch avatar Jun 01 '14 22:06 notacouch