2017-04-06 1 views
0

私のアプリケーションでは、注文フォーム上の個々のラインアイテムをキャプチャするモデルがあります。無限の数が存在する可能性があります。それらは、外部キー(msr_id)によって「親」モデルに結び付けられます。CakePHP:foreachループでのデータの保存

場合によっては、注文フォームをコピーしてアイテムの並べ替えができます。個々の広告申込情報を新しいレコードにコピーする際に問題が発生しています。私は$ラインをデバッグする場合

/*Get the MSR line items and build an array out of them. The 
line items are tied to the main record by $orig['Msr']['id']*/ 
$this->loadModel('MsrLineItem'); 
$lines = $this->MsrLineItem->find('all', array('conditions'=>array('msr_id'=>$orig['Msr']['id'], 'MsrLineItem.deleted_record'=>0))); 

が、それは私がコピーしようとしている注文フォーム上に存在する2つのラインの項目を示しています。ここではラインのアイテムを取得し、私のコントローラのコードがあります。ここで私は新しいレコードに行項目のデータをコピーするのに使用しようとしている同じコントローラ内のコードは次のとおりです。

//Save the non-line item data first 
if($newmsr = $this->Msr->save($new)) { 
    //Set the line item ID to the new record ID (from $newmsr) 
    $this->MsrLineItem->set('msr_id', $newmsr['Msr']['id']); 
    //Loop through the line items, saving each one to the new record 
    foreach($lines as $line) { 
    $newli['MsrLineItem']['quantity'] = $line['MsrLineItem']['quantity']; 
    $newli['MsrLineItem']['unit_of_meas'] = $line['MsrLineItem']['unit_of_meas']; 
    $newli['MsrLineItem']['description'] = $line['MsrLineItem']['description']; 
    $newli['MsrLineItem']['part_no'] = $line['MsrLineItem']['part_no']; 
    $newli['MsrLineItem']['unit_price'] = $line['MsrLineItem']['unit_price']; 
    $this->MsrLineItem->save($newli, 'msr_id'); 
    } 
    $this->Session->setFlash(__('The MSR has been copied successfully.')); 
    $this->redirect(array('action' => 'edit', $newmsr['Msr']['id'])); 

私の問題は、私が唯一のループ内の最後の行項目をキャプチャしていますということです。それ以前に来たものは無視されます。すべての広告申込情報を取得するにはどうすればよいですか?

答えて

0

私はそれを理解しました。私は、ループの各反復の初めに)(MsrLineItem->の$ this - を呼び出す必要があり>を作成:

foreach($lines as $line) { 
    $this->MsrLineItem->create(); 
    //Copy the line items 
} 

はループにその行を追加し、それが今取り組んでいます。

関連する問題