doudao0660 2014-01-10 13:24
浏览 57
已采纳

如何实现CRUD与很多:很多关系

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

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

For example

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');

Wich generates this table, wich is OK:

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

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');

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

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?

My idea:

  • 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..

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?

Thank you in advance.

  • 写回答

2条回答 默认 最新

  • duanji9481 2014-01-10 23:42
    关注

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

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

    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.

    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. Sorry, this time I have no ready-to-use example :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行