⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

set a preformatted default value to a data field



hurtz

hurtz
  • profile picture
  • Member

Posted 15 January 2017 - 05:10 AM

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


hurtz

hurtz
  • profile picture
  • Member

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.


Amit Shah

Amit Shah
  • profile picture
  • Member

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 :)


hurtz

hurtz
  • profile picture
  • Member

Posted 18 January 2017 - 07:15 AM

thanks for setting the direction and also for the advice :)