2013-10-26 51 views
-1

我想在cake php中存儲多個輸入行。但我得到一個錯誤。這裏是我的代碼:在單個表格中添加多個輸入行

控制器::行動:

if ($this->request->is('post')) { 
    $this->OrderPlan->create(); 

    if ($this->OrderPlan->saveAll($this->request->data, 
      array('validation'=>false), 
      array('atomic'=>true))) { 

     $this->Session->setFlash(__('The order plan has been saved.')); 
     return $this->redirect(array('action' => 'index')); 
    } else { 
     $this->Session->setFlash(__('The enquiry could not be saved. Please, try again.')); 
    } 
} 

編輯頁面:

<?php 
    $catageries = array("yarn", "knitting", "dyeing", " Compacting "); 
    foreach ($catageries as $value): 
?> 
     <td></td> 
     <td><?php echo "$value";?></td> 
     <td><?php echo $this->Form->input('work_begin_date', array('type' => 'text', 'class' => 'span6 datepicker', 'label' => false, 'tabindex' => '1', 'id' => 'work-begin-date', 'wrapInput' => false)); ?></td> 
     <td><?php echo $this->Form->input('work_end_date', array('type' => 'text', 'class' => 'span6 datepicker', 'label' => false, 'tabindex' => '1', 'id' => 'work-end-date', 'wrapInput' => false)); ?></td> 
     <td><?php echo $this->Form->input('lead_time_from_po', array('type' => 'text', 'class' => 'span6 datepicker', 'label' => false, 'tabindex' => '1', 'id' => 'lead-time-form-po', 'wrapInput' => false)); ?></td> 
     <td><?php echo $this->Form->input('po_to_be_issued_on_date', array('type' => 'text', 'class' => 'span6 datepicker', 'label' => false, 'tabindex' => '1', 'id' => 'po-to-be-issued-on-date', 'wrapInput' => false)); ?></td> 
     <td><?php echo $this->Form->input('planned_po_qty', array('type' => 'text', 'class' => 'span6 datepicker', 'label' => false, 'tabindex' => '1', 'id' => 'planned-po-qty', 'wrapInput' => false)); ?></td> 
     <td><?php echo $this->Form->input('planned_unit_rate', array('type' => 'text', 'class' => 'span6 datepicker', 'label' => false, 'tabindex' => '1', 'id' => 'planned-unit-rate', 'wrapInput' => false)); ?></td> 
     <td><?php echo $this->Form->input('po_date', array('type' => 'text', 'class' => 'span6 datepicker', 'label' => false, 'tabindex' => '1', 'id' => 'po-date', 'wrapInput' => false)); ?></td> 
     <td><?php echo $this->Form->input('po_number', array('type' => 'text', 'class' => 'span6 datepicker', 'label' => false, 'tabindex' => '1', 'id' => 'po-number', 'wrapInput' => false)); ?></td> 
     <td><?php echo $this->Form->input('party_name', array('type' => 'text', 'class' => 'span6 datepicker', 'label' => false, 'tabindex' => '1', 'id' => 'party-name', 'wrapInput' => false)); ?></td> 
     <td><?php echo $this->Form->input('actual_po_qty', array('type' => 'text', 'class' => 'span6 datepicker', 'label' => false, 'tabindex' => '1', 'id' => 'actual-po-qty', 'wrapInput' => false)); ?></td> 
     <td><?php echo $this->Form->input('actual_unit_rate', array('type' => 'text', 'class' => 'span6 datepicker', 'label' => false, 'tabindex' => '1', 'id' => 'actual_unit_rate', 'wrapInput' => false)); ?></td> 
     <td>&nbsp;</td> 
    </tr> 

<?php endforeach; ?> 
    </table> 
    <?php echo $this->Form->submit(__('Save All'),array('multiple'=>'true'), array('class' => 'btn btn-info')); ?> 

這店了最後一排只有一個記錄。另外三行沒有保存。

+0

適應你原來的代碼,你可以提供edit.ctp文件的完整代碼? – 2013-10-26 13:17:54

回答

-1

如果您要保存多行,那麼您必須取消設置上次保存的項目的ID。使用
$this->Model->save(); ,然後如果你正在使用Model::saveMany()使用

unset($this->Model->id); 
+2

這絕對是錯誤的方式。爲了重置模型狀態,可以使用['Model :: create()'](http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-create-array但是,他沒有使用多個'Model :: save()'調用,而是使用['Model :: saveAll()'](http://book.cakephp.org/2.0/en/models/)因此問題實際上是數據的格式,必須是['Model :: saveMany()'] (http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-savemany-array-data-null-array-options-array)兼容。 – ndm

1

,您的數據需要這樣

Model => 
    0 => 
    field1 => var1 
    field2 => var2 
    1 => 
    field1 => var3 
    field2 => var4 

爲了做到這一點被格式化,表單字段需要索引,像這

$this->Form->input('Model.0.field1') 
$this->Form->input('Model.0.field2') 
$this->Form->input('Model.1.field1') 
$this->Form->input('Model.1.field2') 

編輯:考試PLE代碼
你可以像這樣的東西

$i = 0; 
foreach($categories as $category){ 
    echo $this->Form->input('Model.' .$i. '.field1'); 
    echo $this->Form->input('Model.' .$i. '.field2'); 
    $i++; 
}