2013-10-26 5 views
-1

케이크 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

같은 것을 사용하여 원래의 코드를 적용 할 수있다? –

답변

-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 -data-array)하지만, 그는'Model :: saveAll()'] (http://book.cakephp.org/2.0/en/models/)의'Model :: save()'호출을 여러 번 사용하지 않고있다. save-your-data.html # model-saveall-array-data-null-array-options-array), 실제로 문제는 데이터의 형식입니다. [Model :: saveMany()'] (http://book.cakephp.org/2.0/ko/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 코드
당신은 당신이 edit.ctp 파일의 전체 코드를 제공 할 수이

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