开发者

cakephp save array field (hasMany)

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 advance.


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.


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜