1

I have a model and hasMany relation to another model.

The single fields data is saved to the main model, and the data from dropdown select input should be saved to the associated model.

When I'm trying to manually create an array and use saveAll, everything works fine and the data is saved. But the issue with the select input is inside its name. The correct format is:

array(
    'Article' => array('title' => 'My first article'),
    'Comment' => array(
        array('body' => 'Comment 1', 'user_id' => 1),
        array('body' => 'Comment 2', 'user_id' => 12),
        array('body' => 'Comment 3', 'user_id' => 40),
    ),
)

When creating the select list and passing the options array, we can only setone tag name like Comment.body, but we have to use Comment.0.body, Comment.1.body, etc.

So the format of the post data is unreadable for Cake to successfully use saveAll.

Has anybody encountered this issue - generating multiple select input with correct names and values?

2 Answers 2

2

The issue was resolved by switching to a $hasAndBelongsToMany relation.

0

How are you generating your form? The correct format is the second you said, Comment.0.body. For this you need something like this:

echo $this->Form->input('Comment.0.body');
echo $this->Form->input('Comment.1.body');

Will generate:

<input type='text' id='Comment0Body' name="data[Comment][0][body]" />
<input type='text' id='Comment1Body' name="data[Comment][1][body]" />
1
  • Hello Paulo and thanks, but the issue if that I have 20 elements related, so I have this to be a multiple dropdown select tag, the issue is that we set input name for select once with $this->Form->select('Model.field', $options))
    – Cassius
    Commented Apr 17, 2012 at 15:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.