tags:

views:

56

answers:

3

I have an array field in the HTML:

< input type="text" name="WineDescripcion[Description][]" />

This field can be duplicated by clicking a button, How can I save it into the model trough the controller ?

This is my current code:

$this->Vino->VinoDescription->save($this->data);

But this returns an SQL error:

Query: INSERT INTO `vino_description` (`subtitulo`, `descripcion`, `id_vino`) VALUES (Array, Array, 60) 

Also if I try to change the name in the HTML to:

< input type="text" name="WineDescripcion[][Description]" />

That works, but only saves 1 record with "NULL" values. Thanks in advanced.

A: 

Try to handle your post data in controller before inserting. You try to insert values as arrays. It isn't correct. Give not array values from post data. You can use print_r($this->data) to see what is your post data structure.

Alexander.Plutov
This is the output with the current field name:WineDescripcion[][Subtitle]WineDescripcion[][Description][VinoDescription] => Array ( [0] => Array ( [Subtitle] => Title 1 ) [1] => Array ( [Description] => Description 1 ) [2] => Array ( [Subtitle] => Title 2 ) [3] => Array ( [Description] => Description 2 ) )
eft0
A: 
foreach ($this->data as $row) {
   $insertData = array('subtitle' => $row['Subtitle'], 'description' => $row['description']);
   $this->Vino->VinoDescription->save($insertData);
}

And maybe you have error in your syntax subtitulo, descripcion. Maybe it should be subtitle and description?

Alexander.Plutov
Undefined index, becouse it's s array for each element
eft0
the data wont be in $this->data as the field name is not data[Something]
dogmatic69
+1  A: 

This is how finally solve this:

    for ($i=0; $i<=count($this->data['VinoDescription'])-1; $i++) {
        $this->Vino->VinoDescription->create();
        $insertData = array('id_vino' => $this->Vino->id, 'subtitulo' => $this->data['VinoDescription']['subtitulo'][$i], 'descripcion' => $this->data['VinoDescription']['descripcion'][$i]);
        $this->Vino->VinoDescription->save($insertData);
    } 

Thanks Alexander for the tips.

eft0