Reputation: 153
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?
Upvotes: 1
Views: 1258
Reputation: 153
The issue was resolved by switching to a $hasAndBelongsToMany
relation.
Upvotes: 2
Reputation: 5303
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]" />
Upvotes: 0