Saving multiple records from a list in CakePHP 3

Saving multiple records from a list in CakePHP 3

Example Form:

<?php echo $this->Form->create($childRecords); ?>
<?php echo $this->Form->input('parent_id', ['type'=>'hidden', 'value'=>$parent->id]); ?>
<?php foreach ($ChildRecords as $childRecord): ?>

<?php echo $this->Form->input('id', ['name'=>'row_id[]', 'label'=>false, 'class'=>'form-control', 'value'=>$childRecord->id]); ?>
<?php echo $this->Form->input('tso_hours', ['name'=>'tso_hours[]', 'label'=>false, 'class'=>'form-control', 'value'=>$childRecord->field1]); ?>
<?php echo $this->Form->input('tbo', ['name'=>'tbo[]', 'label'=>false, 'class'=>'form-control', 'value'=>$childRecord->field2]); ?>

<?php endforeach; ?>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

 

 

In your controller, under post/put:

$x=0;
foreach($this->request->data['id'] as $tlc_id) {

$row = $this->childRecords->get($tlc_id);
$row->id = $row_id;
$row->field1 = $this->request->data['field1'][$x];
$row->field2 = $this->request->data['field2'][$x]; 
$this->ChildRecords->save($row);
$x++;

} 

Share this Post