OK -this might be simple but i’m having trouble visualising where i need to put what in ATK4.
I have a table (team) with columns id, name and reference. The id is an autoincrement colunm and a reference. In the table there are 3 rows like this
id, name, last_ref
1, 'Team 1', 1000
2, 'Team 2', 1000
3, 'Team 3', 2000
There is another table (story) with columns id, name, team_id and team_ref which after populating with data looks like this
id, name, team_id, team_ref
1, 'Story A', 1, 1001
2, 'Story B', 1, 1002
3, 'Story C', 1, 1003
4, 'Story D', 2, 1001
5, 'Story E', 3, 2001
For each insert into the story table, the team_ref is looked up in the team table, incremented by 1 and the result stored against the story row. The last_ref field should also be updated immediately in case anyone else also inserts a new row into the story table.
So after the above inserts into the story table, the team table should look like below so each team is maintaining it’s own sequence and allocating the numbers in order.
id, name, last_ref
1, 'Team 1', 1003
2, 'Team 2', 1001
3, 'Team 3', 2001
The page where the story records are inserted is a CRUD but not sure if i should insert the logic into the CRUD, the page or the model itself. It should only impact inserts and i thought maybe it should be a defaultValue on the addField(‘last_ref’) but can i make this a function and where should the function be defined ?
A nice to have, although not essential, would be that incrementing of last_ref should skip any references that are already used in the table for the current team (in case inserted via some other means other than the CRUD).
Thanks in advance.
Certainly Model is the right place for database-related logic like this.
Redefine beforeInsert function, which would lock tables, look up largest element and set the reference:
Functions here starting with “my” would be your custom-implemented ones.
Then, assuming that you are using models properly, there would be no “other means” to insert record and you wouldn’t need to worry about skipping those entries.