Hi! Im new in using the grocery crud. How can I set a preformatted value as my data field value? For example, I would like my data field to have this value: record number = 2017-01-001 where the first 4 digits is the current year, the second 2 digits will be the current month and the last 3 digits is an auto increment number. Thanks
set a preformatted default value to a data field
- Single Page
Posted 15 January 2017 - 05:10 AM
Posted 16 January 2017 - 03:09 AM
after searching for quite sometime :( i was able to figure out on how to do this. I'm not sure if this is the best approach but at least it solves the problem. First, I created 2 fields in my DB, record number and 1 field to store the last 3 digits of my record number. Then I used callback_before_insert and callback_before_update.
My only problem here now is how am I going to capture the last entry of the last 3 digits of my record number in the db and +1 if i click the add button. Hope someone can help me on this matter.
Posted 18 January 2017 - 02:07 AM
Hi hurtz,
This is a typical scenario where its like you want to create your own sequence. Here what you look in for is creating the separate sequence for month / year. There is a simpler approach to what you wana achieve - u need to maintain a table for sequence with 3 columns - year / month / next_id
when u insert - u can do a callback before insert .. grab the fresh id - but how? i recommend u write a store procedure / function @db level. What this needs to do - lock the table - read the value ... increment .. and update the value ... unlock the table and return it to you. The steps mentioned can be performed even @php level but .. writing the same in db will make it execute much faster.
This will return u the fresh sequence for the year / month that u can use as your key :)
Happy GCing to you :)
Posted 18 January 2017 - 07:15 AM
thanks for setting the direction and also for the advice :)