2016-07-29 18 views
0

Verwenden von Cake v2.x Ich habe ein Formular für meine Add-Methode, die vier HTML-Select-Eingaben bietet. Die wählen sind optional, wenn der Benutzer eine Auswahl vornimmt und speichert das Format meiner Array gespeichert werden soll, wie folgt:CakePHP Empty Selects Speichern

array(
    'AttributeProductVariation' => array(
     'product_id' => '26', 
     'sku' => 'xxxxxx', 
     'stock_level' => '10', 
     'id' => '' 
    ), 
    'AttributeVariation' => array(
     (int) 1 => array(
      'variation_id' => '1' 
     ), 
     (int) 2 => array(
      'variation_id' => '' 
     ), 
     (int) 4 => array(
      'variation_id' => '' 
     ), 
     (int) 7 => array(
      'variation_id' => '' 
     ) 
    ) 
) 

Das Problem ist, dass die Werte in der AttributeVariation Tabelle mit variation_id = 0 für die 3 gespeichert werden wählt aus, welche leer gelassen wurden. Ich brauche diese Option, um ignoriert zu werden und nicht in diesem Fall gespeichert zu werden.

Meine Formhelper ist wie folgt:

$this->Form->input('AttributeVariation.'.($key).'.variation_id', [ 
    'label' => $attributes[$key],      
    'required' => false, 
    'options' => $variations[$key], 
    'empty' => 'Select an Attribute if required', 
]); 

ich wie folgt speichern:

$this->AttributeProductVariation->saveAll($this->request->data) 

ich natürlich für leere wählt aussehen könnte und diese aus dem Array entfernen vor dem Speichern, aber diese scheint, wie ein hacken? Ich bin mir sicher, dass das Framework dies aus der Box bewältigen kann.

+0

Sie 'variation_id' überprüfen kann, ist nicht leer – Poonam

Antwort