Carles Jove i Buxeda
Carles Jove i Buxeda

Reputation: 701

CakePHP invalid data format on saveAll() using input helper

I'm working with CakePHP(2.2.3) for the first time, and I'm having this issue. I created a simple form with the Input helper in order to try the saveAll() method.

Here's the form code. No big deal.

$this->Form->create('Section');
$this->Form->input("Section.0.title");
$this->Form->input("Section.1.title");
$this->Form->end('Save');

According to CakePHP's docs, in order to do a saveAll(), you need an array like this:

Array
(
    [0] => Array
        (
            [Section] => Array
                (
                    [title] => title 1
                )
        )
    [1] => Array
        (
            [Section] => Array
                (
                    [title] => title 2
                )

        )

)

However, if I dump $this->request->data, the array I get from the form is like this:

Array
(
    [Section] => Array
        (
            [0] => Array
                (
                    [title] => title 1
                )
            [1] => Array
                (
                    [title] => title 2
                )
        )
)

I guess that when using the Input helper in such a simple situation, $this->request->data array should have the valid format. So I guess I'm missing something, but I can't find what.

Is there a way to get the array in the valid format, or do I need to create a custom method to rewrite it?

Thank you very much in advance.

Upvotes: 0

Views: 656

Answers (1)

Ceeram
Ceeram

Reputation: 748

in order to save multiple records of a single model, the array should be numerical indexed, so $this->Section->saveAll($this->request->data['Section']); will do it for you

Upvotes: 0

Related Questions