Yii 具有多个输入的父窗体子窗体
如何在不使用任何扩展名的情况下在child中保存多行? 在父窗体中,我有一个子窗体,用户只需输入所需的任何行。现在它只保存第一行 控制器:Yii 具有多个输入的父窗体子窗体,yii,Yii,如何在不使用任何扩展名的情况下在child中保存多行? 在父窗体中,我有一个子窗体,用户只需输入所需的任何行。现在它只保存第一行 控制器: //some parent stuff form here $valid = true; $arrAttrData = array(); if($model->save()){ if(isset($_POST['ProductAttribute'])){ foreach($_POST['P
//some parent stuff form here
$valid = true;
$arrAttrData = array();
if($model->save()){
if(isset($_POST['ProductAttribute'])){
foreach($_POST['ProductAttribute'] as $i=>$attrItem){
$attr = new ProductAttribute;
$attr->attributes = $attrItem;
if ( $model->product_id )
$attr->product_id = $model->product_id;
if ( ! $attr->validate() )
$valid = false;
else $arrAttrData[] = $attr;
}
}
if ( $valid ){
foreach( $arrAttrData as $attr ){
$attr->product_id = $model->product_id;
$attr->save();
}
}
}
家长:
//some parent stuff here
<div id="attribute">
<?php
$this->renderPartial('_attr', array(
'attr' => $attr,
));?>
</div>
//这里有些家长的东西
_属性:
<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'attr-form',
'htmlOptions'=>array('enctype'=>'multipart/form-data'),
)); ?>
<?php echo $form->errorSummary($attr); ?>
<table>
<tbody>
<tr>
<td>Attribute's Name</td><td>Attribute Price</td><td>Description</td>
</tr>
<?php for ($i=0;$i<5;$i++):?>
<tr>
<td><?php echo CHtml::activeTextField($attr,'[$i]name',array('class'=>'span12','placeholder'=>'Red or X-Small')); ?></td>
<td><?php echo CHtml::activeTextField($attr,'[$i]cost',array('class'=>'span4','placeholder'=>'89.99')); ?></td>
<td><?php echo CHtml::activeTextField($attr,'[$i]description',array('class'=>'span12','placeholder'=>'Spicy Red Thingy')); ?></td>
</tr>
<?php endfor?>
<br>
<?php echo CHtml::error($attr, '[$i]name'); ?>
<?php echo CHtml::error($attr, '[$i]cost'); ?>
<?php echo CHtml::error($attr, '[$i]description'); ?>
</tbody>
</table>
<?php $this->endWidget(); ?>
属性的名称属性价格描述
[编辑]:
表单html视图
<table>
<tbody>
<tr>
<td>Attribute's Name</td><td>Attribute Price</td><td>Description</td>
</tr>
<tr>
<td><input class="span12" placeholder="Red or X-Small" name="ProductAttribute[$i][name]" id="ProductAttribute_$i_name" type="text" maxlength="155"></td>
<td><input class="span4" placeholder="89.99" name="ProductAttribute[$i][cost]" id="ProductAttribute_$i_cost" type="text"></td>
<td><input class="span12" placeholder="Spicy Red Thingy" name="ProductAttribute[$i][description]" id="ProductAttribute_$i_description" type="text" maxlength="155"></td>
</tr>
<tr>
<td><input class="span12" placeholder="Red or X-Small" name="ProductAttribute[$i][name]" id="ProductAttribute_$i_name" type="text" maxlength="155"></td>
<td><input class="span4" placeholder="89.99" name="ProductAttribute[$i][cost]" id="ProductAttribute_$i_cost" type="text"></td>
<td><input class="span12" placeholder="Spicy Red Thingy" name="ProductAttribute[$i][description]" id="ProductAttribute_$i_description" type="text" maxlength="155"></td>
</tr>
<tr>
<td><input class="span12" placeholder="Red or X-Small" name="ProductAttribute[$i][name]" id="ProductAttribute_$i_name" type="text" maxlength="155"></td>
<td><input class="span4" placeholder="89.99" name="ProductAttribute[$i][cost]" id="ProductAttribute_$i_cost" type="text"></td>
<td><input class="span12" placeholder="Spicy Red Thingy" name="ProductAttribute[$i][description]" id="ProductAttribute_$i_description" type="text" maxlength="155"></td>
</tr>
<tr>
<td><input class="span12" placeholder="Red or X-Small" name="ProductAttribute[$i][name]" id="ProductAttribute_$i_name" type="text" maxlength="155"></td>
<td><input class="span4" placeholder="89.99" name="ProductAttribute[$i][cost]" id="ProductAttribute_$i_cost" type="text"></td>
<td><input class="span12" placeholder="Spicy Red Thingy" name="ProductAttribute[$i][description]" id="ProductAttribute_$i_description" type="text" maxlength="155"></td>
</tr>
<tr>
<td><input class="span12" placeholder="Red or X-Small" name="ProductAttribute[$i][name]" id="ProductAttribute_$i_name" type="text" maxlength="155"></td>
<td><input class="span4" placeholder="89.99" name="ProductAttribute[$i][cost]" id="ProductAttribute_$i_cost" type="text"></td>
<td><input class="span12" placeholder="Spicy Red Thingy" name="ProductAttribute[$i][description]" id="ProductAttribute_$i_description" type="text" maxlength="155"></td>
</tr>
</tbody>
</table>
属性的名称属性价格描述
在控制器中,尝试以下操作
if($model->save()){
if(isset($_POST['ProductAttribute'])){
foreach($_POST['ProductAttribute'] as $i=>$attrItem){
$attr = new ProductAttribute;
$attr->attributes = $attrItem;
if ( $model->product_id )
$attr->product_id = $model->product_id;
if ($attr->validate() )
$attr->save();
}
}
unset($attr);
} // foreach
}
@user3232194删除“else$arattrdata[]=$attr;”中的else部分,并修改“if($attr->product_id=$model->product_id;$attr->save();unset($attr);}”如果($attr->validate()),请尝试此操作并让我知道在此行之前尝试打印($attr->getErrors()),给我发什么错误来了请从浏览器查看你的表格来源,然后贴在这里我贴了。。上面写着1美元。。哪个应该是数字?还是不加。。我做了类似于
CHtml::activeTextField($attr,['.$i.]name'