Reputation: 642
Form (newreports.php) when on submit needs to save to table as well as save hidden data (27 records with 7 columns each) to a BelongsTo table. 7 columns are: id,user_id,reports_id,count,area,area_id,comments. Area needs to pre-fill as 0-26 and reports_id needs to be the same for all (100). User_id should pre-fill from the form entry. Also, id should auto-fill. I think this can be done in my controller for function newreports().
Do I need to write the array like this or is there a simplified way?
$this->Report->saveAll(
Array
(
[Report] => Array
(
[0] => Array
(
[id] => //leave blank because it will auto-fill?
[user_id] => //dynamically from form input
[reports_id] => //dynamically from form input
[area_id] => //dynamically from form input
[area] => 0
[count] => // this should be blank as there are no counts yet
[comments] => // this should be blank as there are no comments yet
)
[1] => Array
(
[id] => //leave blank because it will auto-fill?
[user_id] => //dynamically from form input
[reports_id] => //dynamically from form input
[area_id] => //dynamically from form input
[area] => 1
[count] => // this should be blank as there are no counts yet
[comments] => // this should be blank as there are no comments yet
)
)
)
Upvotes: 0
Views: 1381
Reputation: 4266
If your form/view code is written the right way, you should be able to get away with $this->Report->saveAll($this->data['Report']);
.
View the HTML source for your form. The values of the name attributes for the input tags should be something like data[Report][0][id]
, data[Report][0][user_id]
, etc. The second record should have fields looking like data[Report][1][id]
, data[Report][2][user_id]
, etc.
When this gets POSTed, it will automatically import in to the correct array structure in $this->data.
If this form will always create new records, then leave out the data[Report][][id]
fields (or leave them empty), and saveAll will create new records.
Update to handle first comment.
It's not clear if you want an assignment to $data after or before the controller saves all that hidden data. Let's deal with both parts; first, the save.
So, your own answer nearly gets it right for creating the data.
$count = 27;
$v = 0;
$data = array('Report' => array());
do {
$data['Report'][] = array('reports_id' => $id, 'area' => $v);
$v++;
} while ($v < $count);
// save report rows to database
$this->Report->saveAll($data['Report']);
So, that prepares a data structure, much like you've done in your original question, and in your answer. However, we use saveAll() to create them all in one go.
The next part, is to retrieve the saved data, and put into $this->data, so you can use it in the form that your users will see after the redirect, as per your comment. You'll need something like this in the controller.
$reports = $this->Report->find('all', array(
'conditions' => array(
'reports_id' => $id
)
));
// merge this in with existing data
$this->data = Set::merge($reports, $this->data);
It is assumed that $id is on the URL as a part of the redirect. And that's how you assign that data to $this->data, so you can use it in your form.
And in your form, you can reference the multiple fields like so:
$this->Form->create('Report');
foreach ($data['Report'] as $i => $report) {
$this->Form->input('Report.'.$i.'.id');
$this->Form->input('Report.'.$i.'.user_id');
$this->Form->input('Report.'.$i.'.reports_id');
$this->Form->input('Report.'.$i.'.area_id');
$this->Form->input('Report.'.$i.'.area');
$this->Form->input('Report.'.$i.'.count');
$this->Form->input('Report.'.$i.'.comment');
}
$this->Form->end('Submit);
You can read more about forms for multiple records at the CakePHP manual.
Upvotes: 0
Reputation: 642
This is what did it for me
$count = 27;
$v = 0;
do {
######### save report rows to database
$this->Report->create();
$this->Report->set(array('reports_id' => $id , 'area' => $v));
$this->Report->save();
$v++;
} while ($v < $count);
Upvotes: 1