如何以多对多关系实施CRUD [英] How to implement CRUD with many:many relationship

查看:263
本文介绍了如何以多对多关系实施CRUD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是atk4的新手,但是我找不到使用带有许多表之间许多关系的CRUD的简单示例。

im new to atk4, but i couldn't find some simple examples for using CRUD with many:many relationships between tables.

我在一些说明中读到了M :M最好用中间表实现,这似乎合乎逻辑。.

I read in some instructions that M:M is best implemented with intermediate table, wich seems logical..

例如

Model ONE has:
$this->hasMany('Table1Table2','table1_id');

Model TWO has:
$this->hasMany('Table1Table2','table2_id');

And Intermediate Model (Table1Table2) has:
$this->hasOne('Table1');
$this->hasOne('Table2');

将生成此表,但可以:

create table table1table2 (  
  id int auto_increment not null primary key,
  table1_id varchar(255),
  table2_id varchar(255));

但是如何实施CRUD? -如何实现列表,添加新内容,编辑等。

But how to implement CRUD? - how to implement listings, adding new, edit etc..

在页面上,我只需像这样插入:

When on page i simply insert like this:

$this->add('CRUD')->setModel('Table1');

不生成任何关系...用户可以选择(添加和编辑时会很好)表1中的值)。

There is no relationship generated... It would be nice that user could select (on add and edit of table1) values from table2.

从阅读和学习教程中我就知道了,但也许完全是劳累过度,所以我真的在问ATK4解决此问题的最佳方法是什么?

From reading and wathing tutorials i have idea, but maybe is totally overwork, so im really asking what is the best way at ATK4 for this problem?

我的想法:


  • 添加多个下拉菜单以编辑和添加表单,并使用table2值填充表单。如果处于编辑状态,请在中间表中检查已检查的内容。

  • 将基本CRUD扩展到formSubmitSuccess上,将下拉列表中的所选值插入中间表


  • 稍后,对GRID和其他内容进行类似的检查...可能会产生很多额外的工作。

  • add multiple dropdown to edit and add form and populate it with table2 values. If in edit, check in intermediate table for what is already checked.
  • extend basic CRUD, on formSubmitSuccess insert selected values from dropdown to intermediate table
  • do it differently for edit/add new..
  • later, do similar check for GRID&other stuff... can generate a lot of extra work..

所以,我认为它是可行的,但是我确信M:M关系非常普遍(基本上是两个1:M),所以也许还有更好的方法解决方案?

So, i see its doable, but i'm shure M:M relationships are very common (it's basically two 1:M), so maybe is there much better solution?

预先感谢您。

推荐答案

毫无疑问,您关系数据库设计中将需要中间表。

No doubts you'll need intermediate table in relational DB design.

问题是,如何定义模型。正如您已经自己解释的那样,一种解决方案是使用3种模型。但是,您也可以仅使用两个模型并在模型定义中使用联接将它们直接联接到中间数据库表中,从而执行类似的操作。
想法是-模型通常与数据库表不同。模型不仅仅是数据库表。一个模型可以像10个表一样联接起来,并用它们做一些花哨的事情:)

Question is, how you define Models. One solution is, as you already explained yourself, with 3 models. But you can also do something similar with just two models and using joins in your model definitions to join them directly to intermediate DB table. Idea here is - Model is not the same as DB table in general. Model is something more than DB table. One model can join up like 10 tables and do something fancy with them :)

我想对于这种接口,没有很好的即用型解决方案(查看),将适合所有需求。但是在某些情况下,如果只需要在两个表之间设置链接(是/否),则可以使用form + grid + grid-> addSelectable()或form + crud + crud-> grid-> addSelectable()。例如,通过这种结构,您可以将多个用户角色与用户相关联,将应用程序与管理员相关联,或将喜欢的颜色与人员相关联等。

I guess there is no nice out-of-the-box solution for such interface (View) which will fit all needs. But in some cases, if you only need to set links (yes/no) between two tables, you can use form + grid + grid->addSelectable() or form + crud + crud->grid->addSelectable(). With such construct you can, for example, associate multiple user roles to users or Apps to Admins or favorite colors to people etc.

如果中间表中有更多数据字段除了链接ID之外,还必须提供一些自定义代码。但是我想你仍然可以通过grid-> addSelectable()方法来窥探一下。

If you have more data fields in intermediate table than just linking id's, then you'll have to come up with some custom code. But I guess you can still take a peek on grid->addSelectable() method to grab some idea.

P.S。抱歉,这次我没有可用的示例:)

P.S. Sorry, this time I have no ready-to-use example :)

这篇关于如何以多对多关系实施CRUD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆