我想将数组中的值保存到数据库的一个字段中。我一直在使用该代码但没有得到任何保存。
$this->Form->input('Model.0.field1');
$this->Form->input('Model.0.field2');
$this->Form->input('Model.1.field1');
$this->Form->input('Model.1.field2');
感谢。
答案 0 :(得分: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()
,请查看Inserting an array into a mysql database column