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


How to handle complex form with multiple models and tabular input in yii

我已经阅读了很多关于如何在 yii 中处理模型和表单的文档,并且我找到了适用于我在下面解释的情况的解决方案,但问题是代码编写和维护起来很复杂,所以我正在寻找建议。

情况如下:我需要将两个不同的模型和第三个模型(同一模型的更多实例)保存在一起。

例如,我可能想保存一篇博客文章(第一个模型),其中包含作者信息(第二个模型)和参考文献列表(第三个模型,表格模型)。

我想一步通过 ajax 验证所有这些并以事务方式保存它们。

我已经阅读了所有这些链接。

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

易中的多模型形式

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

最后一个链接很有趣,但如果有人有一个最佳实践可以分享,我会感谢他。

我认为您正在寻找这样的东西:http://www.yiiframework.com/extension/eadvancedarbehavior/

这是一个完全符合您要求的扩展程序。


引自链接:

HAS_ONE和HAS_MANY

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

所以你现在可以做:

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

我想你明白如何设置表格吗?您发送的那些链接对此非常清楚。