Ryan Potter
Ryan Potter

Reputation: 835

CakePHP saving dynamic inputs to a single row.

Basically I've made a form in Cakephp 2.1 with a jQuery button that appends an input field and adds a count to each of the newly created input like so:

<input type="text" name="data[foo][link]" /> // Original input
<input type="text" name="data[foo][1][link]" /> // Appended inputs
<input type="text" name="data[foo][2][link]" />
<input type="text" name="data[foo][3][link]" />

My question is if it's possible to get all of these inputs to save into the same [foo][link] table in the database (preferably as an array)?

Thanks heaps.

Upvotes: 0

Views: 339

Answers (1)

floriank
floriank

Reputation: 25698

public beforeSave() {
    if (isset($this->data['Foo'])) {
        $this->data['YourModel']['realField'] = serialize($this->data['Foo']);
    }
    return true;
}

In the afterFind() you revert that by using unserialize();

data[foo][link] Should be data[foo][0][link].

Upvotes: 2

Related Questions