在 Yii 中保存多个表格输入 [英] Save multiple tabular input in Yii

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

问题描述

我想知道如何在 Yii 中插入表格数据.

I'm wondering how can I insert tabular data in Yii.

当然,我在此遵循了 docs方面,但我的情况几乎没有差异.

Of course, I've followed docs in this aspect however there are few differences in my situation.

首先,我想保存两个模型,与文档文章中的完全相同.主要区别在于第二个模型可能有多个元素(数据库中的简单一对多关系).

First of all, I want to save two models, exactly as in the docs article. The main difference is that there might be more that one element for second model (simple one to many relation in database).

我使用 CHtml 来构建我的表单.我实现了一个 jQuery 片段来动态添加更多输入组.

I use CHtml to build my forms. I implemented a jQuery snippet to add more input groups dynamically.

我现在无法显示我的代码,因为它完全搞砸了,目前无法正常工作.

I'm unable to show my code now as it's totally messed up and not working currently.

我的主要问题是:如何处理 Yii 中第二个模型的元素数组?

My main question is: how to handle the array of elements for second model in Yii?

推荐答案

在控制器中定义你的两个模型

Define your two models in controller

$model1= new Model1();
$model2= new Model2();

//massive assignments
$model1->attributes=$_POST['Model1']
$model2->attributes=$_POST['Model2']

//validation
$valid= $model1->validate();
$valid =$valid &&  $model2->validate();

if($valid){
   $model1->save(false);
   $model1->save(false);  
  }

如果您想单独访问字段,请转储您的帖子,您可以查看发布数组格式,或者您可以像这样手动分配而不是进行大量分配

if you want to access fields individually dump your post and you can view the the post array format or instead of doing massive assignments you can manually assign like this

$model1->field1 =$_POST['Model1']['field1'];

//validation  logic
...  
 if($valid){
   $model1->save(false);
   $model1->save(false);  
  }

这篇关于在 Yii 中保存多个表格输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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