如何在yii中处理具有多个模型和表格输入的复杂表格 [英] How to handle complex form with multiple models and tabular input in yii

查看:137
本文介绍了如何在yii中处理具有多个模型和表格输入的复杂表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多有关如何在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).

例如,我可能想保存一个Blog帖子(第一个模型)以及作者信息(第二个模型)和参考列表(第三个模型,表格形式).

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

我想一步一步地通过ajax验证它们,并以事务方式保存它们.

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-aving-related-models/

yii中的多种模型形式

http://www.yiiframework.com/doc/guide/1.1/zh-CN/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-aving-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.

推荐答案

我认为您正在寻找这样的东西:

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.

从链接中引用:

HAS_ONE和HAS_MANY

假设用户HAS_ONE地址和HAS_MANY电子邮件以及地址/电子邮件BELONGS_TO用户(都具有外键列user_id).

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.

这篇关于如何在yii中处理具有多个模型和表格输入的复杂表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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