0

I wanna save values from an array into one field of my database. I've been using that code but nothing got saved.

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

Thanks.

4
  • Where is the save code you've tried to use? Which version of CakePHP are you using (it makes a big difference). The official documentation goes into great detail on how to save so make sure you've properly read through it! Commented Jun 8, 2015 at 13:53
  • Hey! Yeah I've read about it and followed every step but nothing got saved :/ ! I'm using CakePhp 2.6.0 . I have an HABTM association , and my save code is in the association controller. Commented Jun 8, 2015 at 14:22
  • Please add the code you're using in your controller to save with to the question. The form inputs don't really provide enough information about what you're trying or what is going wrong. Commented Jun 8, 2015 at 14:29
  • If you really want to store this array into one database field you can json_encode($this->request->data['Model']) and then you can easily json_decode($value, true); it back when you need. Commented Jun 8, 2015 at 14:34

1 Answer 1

0

I think you need to save data with json_encode() value.

// 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']));
        }
    }   
}

You can use implode() to generate comma separated data. If you want to use implode(), look at Inserting an array into a mysql database column

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.