我想在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> </td>
</tr>
<?php endforeach; ?>
</table>
<?php echo $this->Form->submit(__('Save All'),array('multiple'=>'true'), array('class' => 'btn btn-info')); ?>
这只存储最后一行中的一条记录。另外三行未保存。
答案 0 :(得分:1)
如果您使用Model::saveMany()
,则您的数据需要格式化为
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')
修改:示例代码
你可以用这样的东西调整原始代码
$i = 0;
foreach($categories as $category){
echo $this->Form->input('Model.' .$i. '.field1');
echo $this->Form->input('Model.' .$i. '.field2');
$i++;
}
答案 1 :(得分:-1)
如果要保存多行,则必须取消设置上次保存的项目的ID。使用
$this->Model->save();
然后使用
unset($this->Model->id);