2014-05-06 53 views
-1

如何在不使用任何擴展名的情況下在兒童中保存多行? 在父窗體中我有一個子窗體,用戶只需輸入任何需要的行。現在它只保存第一行。父窗體與多個輸入子窗體

控制器:

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

回答

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 
} 
+0

啊,沒有工作。還是保存了第一個。 – JamAndJammies

+0

@ user3232194刪除「else $ arrAttrData [] = $ attr中的else部分;」並修改「if($ valid){attr-> product_id = $ model-> product_id; $ attr-> save(); unset($ attr); }」試試這個,讓我知道 –

+0

hm。 。not working – JamAndJammies

相關問題