使用 ATK4 管理模型中的增量字段(不是 mysql 自动增量列)
好的 - 这可能很简单,但我无法想象我需要在 ATK4 中放置什么。
我有一个表(团队),其中包含 id、名称和参考列。 id 是一个自动增量列和一个引用。表中有 3 行,如下所示
id, name, last_ref
1, 'Team 1', 1000
2, 'Team 2', 1000
3, 'Team 3', 2000
还有另一个表(故事),其中包含 id、name、team_id 和 team_ref 列,填充数据后如下所示
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
对于每次插入故事表,都会在团队表中查找 team_ref ,增加 1,并将结果存储在故事行中。如果其他人也将新行插入到故事表中,last_ref 字段也应该立即更新。
因此,将上述内容插入故事表后,团队表应如下所示,以便每个团队维护自己的序列并按顺序分配数字。
id, name, last_ref
1, 'Team 1', 1003
2, 'Team 2', 1001
3, 'Team 3', 2001
插入故事记录的页面是 CRUD,但不确定我是否应该将逻辑插入 CRUD、页面或模型本身。它应该只影响插入,我想也许它应该是 addField('last_ref') 上的默认值,但我可以将其设为一个函数吗?该函数应该在哪里定义?
尽管不是必需的,但最好是增加 last_ref 应该跳过当前团队表中已使用的任何引用(如果通过 CRUD 之外的其他方式插入)。
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然,模型是像这样的数据库相关逻辑的正确位置。
重新定义 beforeInsert 函数,该函数将锁定表、查找最大元素并设置引用:
此处以“my”开头的函数将是您自定义实现的函数。
然后,假设您正确使用模型,则不会有“其他方式”来插入记录,并且您无需担心跳过这些条目。
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.