具有多个输入子表单的父表单

时间:2014-05-06 19:11:21

标签: yii

如何在不使用任何扩展程序的情况下在子项中保存多行? 在父表单中,我有一个子表单,用户只需输入所需的行。现在它只保存第一行。

控制器:

//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>

_attr:

  <?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>

1 个答案:

答案 0 :(得分:0)

在你的控制器中,试试这个

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
}