2012-04-11 89 views
0

我將如何保存以下內容;保存數據,如何將數據應用於所有記錄?

我希望前4項適用於所有「要保存的產品」,因爲這些數據位於我的表單頂部,「全局」位於保存的產品上,我將如何告訴cakephp保存它對每個產品還是我必須創建一個foreach循環並手動插入數據?

全球數據到每個產品。

[discount_id] => 17 
[range_id] => 21 
[category_id] => 6 
[user_id] => 104 
Array 
(
    [Product] => Array 
     (
      [discount_id] => 17 
      [range_id] => 21 
      [category_id] => 6 
      [user_id] => 104 
      [0] => Array 
       (
        [product_code] => ffff 
        [colour] => red 
        [lead_time_weeks] => 
        [description] => 
        [height] => 11111 
        [width] => 22222 
        [depth] => 
        [price] => 
        [discount] => 50 
        [discounted_price] => 
        [quantity] => 
       ) 

      [1] => Array 
       (
        [product_code] => fgfgfggf 
        [colour] => red 
        [lead_time_weeks] => 
        [description] => 
        [height] => 123 
        [width] => 123 
        [depth] => 
        [price] => 
        [discount] => 50 
        [discounted_price] => 
        [quantity] => 
       ) 

     ) 

) 

保存方法在控制器

$this->Product->saveAll($this->request->data['Product'] 

回答

1

我會親自使用foreach循環,白水是爲節省相關的數據。 例如

foreach($this->request->data['Product'] as $product){ 
    // Don't want to add the generic items 
    if(is_array($product)){ 
    $newProduct = $this->Product->create(); 
    $newProduct['Product'] = $product; // To save added them seperately 
    // Then add the generic items into the array 
    $newProduct['Product']['discount_id'] = $this->request->data['Product']['discount_id']; 
    etc... 
    $this->Product->save($newProduct); 
    } 
} 
+0

非常感謝您快速回答:) – 2012-04-11 10:35:01

相關問題