2012-04-11 15 views
0

Wie würde ich folgendes speichern?Daten speichern, Daten auf alle Datensätze anwenden?

Ich möchte, dass die ersten 4 Elemente auf alle "zu speichernden Produkte" angewendet werden, da diese Daten oben in meinem Formular stehen und "global" zu den Produkten, die gespeichert werden es zu jedem Produkt oder muss ich eine foreach-Schleife erstellen und manuell die Daten einfügen?

Globale Daten zu jedem Produkt.

[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] => 
       ) 

     ) 

) 

Save-Methode in der Steuerung

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

Antwort

1

ich persönlich eine foreach-Schleife verwenden würde, wird SaveAll zum Speichern zugehörige Daten gemeint. z.B.

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

Vielen Dank für Ihre schnelle Antwort :) –