0

我想将数组中的值保存到数据库的一个字段中。我一直在使用该代码,但没有保存任何内容。

$this->Form->input('Model.0.field1');
$this->Form->input('Model.0.field2');
$this->Form->input('Model.1.field1');
$this->Form->input('Model.1.field2');

谢谢。

4

1 回答 1

0

我认为您需要保存json_encode()有价值的数据。

// In your controller

public function test() {

    if($this->request->is('post'))   {

        //If you want to insert in single row then you can use json_encode() and add to your colum.

        $insert_data = json_encode($this->request->data);

        $data = array(); 
        // Load your model where you want save data

        $this->loadModel('Test'); 
        // set attribute name where you want to save

        $data['Test']['value'] = $insert_data; 
        $this->Test->save($data);

        //For viewing your data
        $fetchedData = $this->Test->find('all');
        foreach($fetchedData as $items) {
            var_dump(json_decode($items['Test']['value']));
        }
    }   
}

您可以使用 implode() 生成逗号分隔的数据。如果要使用implode(),请查看将数组插入 mysql 数据库列

于 2015-06-09T05:36:11.857 回答