Key increment
For some simple models, creating a unique value can simply be done by updating the identifier field with the id that has been created on the insertion. However, if you have various data types that fit into the same table, you may not necessarily want them to share the same id. That is where the key_increments table comes in.
Contents |
How does it work
All you have to do is to call the getKeyIncrement function on your model. This function needs to be provided with the key name and the string in which to put the key. Once the key increment is read from the database, a string replace will occur on the string, seeking to replace %%key_increment%% by the fetched value.
This function locks the database key_increments table and is thread safe.
How to make it work
For a complete example, see clinicalannotation.misc_identifier.
Create a key
Head over the key_increments table and add your key in there. The assigned value is the value that will be used on the next request. If you want the first returned value to be 10000, you just put 10000 as key_value.
Controls
Usually when different kind of data is found within the same table, a *_controls table exists. This is usually where you put your basic value (the one that need to be updated with the key). If it's the case, this is also where you want to put a reference to your key_name.
The add form
Display
When you display your form, you want your "will be replaced" field to be readonly. The user will notice the %%key_increment%% part and thus get a hint that this will be updated upon submission.
Submission
If the data validates, you want to generate and set your value. You'll need to make a call to getKeyIncrement(key_name, value_to_update) where key is the name of the key to take the value from and value_to_update is the string containing the %%key_increment%% to replace.
This function is inherited by app_model. So you'll simply need to do somthing like
$this->model->getKeyIncrement(key_name, value_to_update);