您好我有以下输入
<div class="span" >
<?php echo $form->textFieldRow($contactModel,'name[]'); ?>
<?php echo $form->textFieldRow($contactModel,'designation[]'); ?>
<?php echo $form->textFieldRow($contactModel,'contact_number[]'); ?>
<?php echo $form->textFieldRow($contactModel,'email_id[]'); ?>
</div>
<div class="span3">
<?php echo $form->textFieldRow($contactModel,'name[]', array('')); ?>
<?php echo $form->textFieldRow($contactModel,'designation[]'); ?>
<?php echo $form->textFieldRow($contactModel,'contact_number[]'); ?>
<?php echo $form->textFieldRow($contactModel,'email_id[]'); ?>
</div>
发布以下数据
阵
(
[name] =&gt;排列
(
[0] =&gt; P1
[1] =&gt; B1
)
[designation] => Array
(
[0] => gjhgjj
[1] => jhbjbb
)
[contact_number] => Array
(
[0] => 878978
[1] => 8897897
)
[email_id] => Array
(
[0] => bjhhhjk
[1] => mnbnn
)
)
如何将其保存到$ model-&gt;属性? 我试过这个
foreach ($_POST['Contacts'] as $key=>$value){
foreach ($_POST['Contacts'][$key] as $val ){
$contactModel = new Contacts;
$contactModel->$key = $val;
$contactModel->save();
}
$contactModel->save();
}
但它没有保存。请帮忙。
答案 0 :(得分:0)
你可以试试这段代码吗?
if(isset($_POST['Contacts'])){
foreach($_POST['Contacts'] as $ContactModels){
$ContactModels[]=new Contacts;
}
foreach($ContactModels as $i=>$ContactModel){
if(isset($_POST['Contacts'][$i])){
$ContactModel->attributes=$_POST['Contacts'][$i];
$ContactModel->save();
}
}
}
答案 1 :(得分:0)
您可以将括号放在属性名称的前面,并为其指定索引:
<div class="span" >
<?php echo $form->textFieldRow($contactModel,'[0]name'); ?>
<?php echo $form->textFieldRow($contactModel,'[0]designation'); ?>
<?php echo $form->textFieldRow($contactModel,'[0]contact_number'); ?>
<?php echo $form->textFieldRow($contactModel,'[0]email_id'); ?>
</div>
<div class="span3">
<?php echo $form->textFieldRow($contactModel,'[1]name', array('')); ?>
<?php echo $form->textFieldRow($contactModel,'[1]designation'); ?>
<?php echo $form->textFieldRow($contactModel,'[1]contact_number'); ?>
<?php echo $form->textFieldRow($contactModel,'[1]email_id'); ?>
</div>
然后你可以用foreach循环:
foreach($_POST['Contacts'] as $attributes) {
$contactModel = new Contacts;
$contactModel->attrbutes = $attributes;
$contactModel->save();
}
如果你有现有的模型,你可以使用他们的id作为索引,并以这种方式找到并保存正确的模型。
答案 2 :(得分:0)
在保存数组之前,尝试将其编码为字符串。控制器代码:
public function actionUpdate($id)
{
$model = $this->findModel($id);
if(isset($_REQUEST['Objekte']['attribute']))
{
if(is_array($_REQUEST['Objekte']['attribute']))
{
$model->attribute = json_encode($_REQUEST['Objekte']['attribute']);
}
}
// now save