Search code examples
phpcakephp-3.0

Cakephp 3 creating multiple entity's rows from form


I'm trying to save multiple rows of a same entity but I'm failing to create the form.

So.. This is my controller: CuotasController

    $cuotas = array();
    $date = Time::create($inicioPago->year, $inicioPago->month, $inicioPago->day);
    $meses = 0;

    while ($date < $finPago) {
        $cuota = $this->Cuotas->newEntity();
        $cuota->vencimiento = $date;
        array_push($cuotas, $cuota);
        $date = Time::create($date->year, $date->month + 1, $date->day);
        $meses = $meses + 1;
    }

Basically, in this code I generate an array of Cuotas (my entity) and the populate dynamically that array.

So here's the form:

                    <?= $this->Form->create($cuotas) ?>
                    <?php foreach ($cuotas as $cuota): ?>
                    <tbody>
                        <tr>
                            <td>
                                <div class="form-group">
                                    <div class="input text required">
                                        <?php  echo $this->Form->input('vencimiento',  ['required' => true, ['class' => 'form-control'] ] ); ?>
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div class="form-group">
                                    <div class="input text required">
                                        <?php  echo $this->Form->input('monto_pesos',  ['required' => true, 'class' => 'form-control' ] ); ?>
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div class="form-group">
                                    <div class="input text required">
                                        <?php  echo $this->Form->input('monto_dolares',  ['required' => true, 'class' => 'form-control' ] ); ?>
                                    </div>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
                <?= $this->Html->link(__('Volver'), ['action' => 'index'] , array('class'=>'btn btn-danger', 'style' => 'margin-top:1em') ) ?>
                <?= $this->Form->button(__('Guardar'), ['class'=>'btn btn-success', 'style' => 'margin-top:1em']) ?>
                <?= $this->Form->end() ?>

Finally, in the controller, this is how I try to patch the entities:

$entities = $this->Cuotas->newEntities($this->request->data);

Do you know where I am messing it all up?

Thanks!


Solution

  • I think that you are overwriting the form on each iteration.

    Inspect the html generated code, if this looks like:

    <input name="abc[property_name]" />
    

    Check if in the next row it looks equal.

    You sould have something like this:

    <input name="abc[0][one_prop]" />
    <input name="abc[0][other_prop]" />
    
    <input name="abc[1][one_prop]" />
    <input name="abc[1][other_prop]" />
    

    Is much better if you use the id of the entity to identify when is submitted.