0

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.

6
  • 1
    what are your model associations Commented Dec 4, 2012 at 16:54
  • Hi Colby Guyer. I don't have any, yet. But if I rewrite the $this->request->data array in the controller following the valid format above (you know, just to see what happens), everything works. So I guess it has nothing to do with model associations. Thank you for answering ;-) Commented Dec 4, 2012 at 16:58
  • Have you tried actually saving it ye? My guess is your format is fine. Commented Dec 4, 2012 at 17:06
  • Hi Dave. Yes, I have tried, but data can't be validated. As I said to Colby Guyer (comment above), if I pass a valid array format (as says in the docs) it just works. The case is: why the Form helper gives an invalid array format? Thank you for joining. Commented Dec 4, 2012 at 17:17
  • @CarlesJoveBuxeda - you have yet to mention anything about validation. Commented Dec 4, 2012 at 19:07

1 Answer 1

0

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

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

1 Comment

Thank you @Ceeram ! That solved my issue. Oh boy, I don't know how I hadn't thought of this... So simple! ;-)

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.