Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Expected behavior when forging a Temporal Model with an existing ID
  • I was messing with a temporal model and noticed that when I forge a new instance of one with an ID that already exists in the corresponding table, and then save it, it stores it in the table without a problem. I know this is expected behavior of the database since the primary key is id, temporal_start, temporal_end and the temporal_start would be different for this new instance. But i was wondering if the temporal model itself was expected to just let it happen or if it should have stopped it since that ID already exists in the table.

    Notes:
    I'm talking about forging new entries, not finding an existing one, making a change, and saving it back.
    The id in my model is not being auto incremented and I am setting it manually via passing it in with the array of data to the forge function.
  • I'm not sure, I've never really used it myself. Since ID in itself isn't a unique key, technically it is no problem to let this happen. But I understand why you ask.

    Probably the simplest solution is to create a UNIQUE index on id, so you can't do it anymore. 

    The alternative would be to do a SELECT on every INSERT, which is not an optimal solution either.
  • I considered the UNIQUE approach, but wouldn't that stop the model from creating new entries when you do an "update" since updates for a temporal model instance are basically just new entries with a different temporal_start time as well?

    Running a SELECT / Temporal_Model::find() to see if one exists before an insert isn't exactly ideal but might be the only way to make sure.
  • HarroHarro
    Accepted Answer
    Hmm... yeah, true. Shows I'm not an expert on Temperal models. ;-)

    You could extend \Orm\Model\Temporal, overload the save() method, and do your check there in case of static::is_new() ? Then it would be transparent for your app...
  • Thanks for the info and ideas. I'll see if I like that approach or if I'd rather go with a something that doesn't use the Temporal class.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion