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

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)