使用 ATK4 管理模型中的增量字段(不是 mysql 自动增量列)

发布于 2024-12-09 19:50:14 字数 929 浏览 1 评论 0原文

好的 - 这可能很简单,但我无法想象我需要在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤痕我心 2024-12-16 19:50:14

当然,模型是像这样的数据库相关逻辑的正确位置。

重新定义 beforeInsert 函数,该函数将锁定表、查找最大元素并设置引用:

function beforeInsert(&$data){
    $this->myLockTable();
    $next_ref = $this->myGetNextRef();
    $data['team_ref']=$next_ref;
    return parent::beforeInsert($data);
}
function afterInsert($id){
    parent::afterInsert($id);
    $team = $this->add('Model_Team')->loadData($this->get('team_id'));
    $team->set('last_ref',$this->get('team_ref'))->update();
    $this->myUnlockTable();
}

此处以“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:

function beforeInsert(&$data){
    $this->myLockTable();
    $next_ref = $this->myGetNextRef();
    $data['team_ref']=$next_ref;
    return parent::beforeInsert($data);
}
function afterInsert($id){
    parent::afterInsert($id);
    $team = $this->add('Model_Team')->loadData($this->get('team_id'));
    $team->set('last_ref',$this->get('team_ref'))->update();
    $this->myUnlockTable();
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文