dplo59755 2013-10-24 16:31
浏览 31
已采纳

如何在yii中处理具有多个模型和表格输入的复杂表格

I've read a lot of documentation about how to handle models and forms in yii and I've found a solution that works for the case that I explain following, but the problem is that the code is complex to write and to maintain, so I'm looking for suggestions.

The case is the following: I need to save together two different models and a third model that is a tabular input (more instances of the same model).

For instance, I may want to save a Blog post (first model) with the author information (second model) and a list of references (third model, the tabular one).

I'd like to validate all of them via ajax in one step and save them transactionally.

I've read all of these link.

http://www.yiiframework.com/wiki/559/tabular-input-validating-and-saving-related-models/

multi model forms in yii

http://www.yiiframework.com/wiki/19/how-to-use-a-single-form-to-collect-data-for-two-or-more-models/

http://www.yiiframework.com/wiki/218/how-to-use-single-form-to-collect-data-for-two-or-more-models-cactiveform-and-ajax-validation-edition/

http://www.yiiframework.com/doc/guide/1.1/en/form.table

http://www.yiiframework.com/wiki/362/how-to-use-multiple-instances-of-the-same-model-in-the-same-form/

http://www.yiiframework.com/forum/index.php/topic/14082-transaction-on-multiple-ar/

http://www.yiiframework.com/wiki/559/tabular-input-validating-and-saving-related-models/

https://github.com/yiiext/with-related-behavior

The last link is interesting, but if someone has one best practice to share, I'll be thankful to him.

  • 写回答

1条回答 默认 最新

  • dtg25862 2013-12-12 14:43
    关注

    I think you are looking for something like this: http://www.yiiframework.com/extension/eadvancedarbehavior/

    This is an extension that does exactly that you asking.


    Quoted from the link:

    HAS_ONE and HAS_MANY

    Suppose a User HAS_ONE Address and HAS_MANY Emails, and Address/Email BELONGS_TO User (both have the foreignkey column user_id).

    So you can do now:

    $blog = new Blog();    
    $blog->author = $author; //the author model object
    $blog->references = array( $references1, $references2, ... ) ; //reference model objects
    $blog->save();
    

    I think you understand how to setup the forms? Those links you send are pretty clear about it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?