Array of inputs in cakePhp 2.5.6

I have an array of inputs in a view to save records in a hasMany relation, when I click on the save button the controller saves the next index of the array. What should I do to save the actual index of the array?
This is the code of the view:

<?php
	echo $this->Form->create('Pacient');
    echo $this->Form->input('estado',array('class'=>'form-control','label'=>'Estado','options'=>array(1 => 'Alerta verde', 2 => 'Alerta amarillo', 3 => 'Alerta naranja', 4 => 'Alerta rojo')));
?>
<table class="table table-bordered table-sortable">
<thead>
    <tr>
        <th class="visible-xs">Paciente</th>
        <th class="hidden-xs">Nombre</th>
        <th class="hidden-xs">Apellido</th>
        <th class="hidden-xs">Telefono</th>
        <th class="hidden-xs">Edad</th>
        <th class="hidden-xs">Direccion</th>
        <th class="hidden-xs">Grupo</th>
        <th class="hidden-xs">Red</th>
        <th class="hidden-xs">Nodo</th>
        <th class="hidden-xs">Inmunización</th>
        <th class="hidden-xs">Agente asignado</th>
        <th class="hidden-xs">Estado</th>
        <th class="hidden-xs">Acciones</th>
        <th class="hidden-xs">Aislado</th>
    </tr>
</thead>
<tbody>

    
    <?php

    foreach ($model as $Pacient):
        $color = '';
        if($Pacient['Pacient']['inmunity_a'] && $Pacient['Pacient']['inmunity_b']) {
            $color = '#3c9f40';
        } else if ($Pacient['Pacient']['inmunity_a']) {
            $color = '#f15f21';
        } else if ($Pacient['Pacient']['inmunity_b']) {
            $color = '#ccb700';
        } else {
            $color = '#f44336';
        }
    ?>
    <tr>
        <td class="visible-xs">
            <?php if($Pacient['Pacient']['status'] == 3) echo $this->html->image('covid-icon.png',array('style'=>'width:24px')); ?>
            <h3><?php echo $Pacient['Pacient']['name']; ?> <?php echo $Pacient['Pacient']['lastname']; ?></h3>
            <div>
               <?php if($viewUser['isSanitaryAgent']): ?>
                    <?php echo $this->Html->link(
                        '<span class="glyphicon glyphicon-eye-open"></span> Efectuar visita',
                        array('controller'=>'home', 'action' => 'addObservation', $Pacient['Pacient']['id']),
                        array('escape' => false,'class' => 'btn btn-success','title' => 'Realizar visita') // This line will parse rather then output HTML
                    ); ?>
               <?php endif; ?>
               
               <?php echo $this->Html->link(
                    '<span class="glyphicon glyphicon-pencil"></span> Editar',
                    array('action' => 'edit', $Pacient['Pacient']['id']),
                    array('escape' => false,'class' => 'btn btn-primary','title' => 'Modificar') // This line will parse rather then output HTML
                ); ?>
                            
                <?php
                    echo $this->Form->postLink(
                        '<span class="glyphicon glyphicon-remove"></span> Borrar',
                        array('action' => 'delete', $Pacient['Pacient']['id']),
                        array('confirm' => 'Esta seguro?','escape' => false,'class' => 'btn btn-danger','title' => 'Eliminar')
                    );
                ?>
            </div>
            Telefono: <?php echo $Pacient['Pacient']['phone']; ?><br/>
            Edad: <?php echo $Pacient['Pacient']['age']; ?><br/>
            Dirección: <?php echo $Pacient['Pacient']['address']; ?><br/>
            Celula: <?php echo $Pacient['Group']['name']; ?><br/>
            UF: <?php echo $Pacient['Net']['name']; ?><br/>
            Nodo: <?php echo $Pacient['Pacient']['main_gate'] ? 'A' : 'B'; ?><br/>
            Inmunización:
            <div style="padding: 10px; display: flex; justify-content: space-around; background-color:<?php echo $color; ?>;color:#fff">
                <div><?php echo $Pacient['Pacient']['inmunity_a'] ? '<span class="glyphicon glyphicon-ok"></span> Gripe: Aplicada' : '<span class="glyphicon glyphicon-remove"></span> Gripe: No aplicada'; ?></div>
                <div><?php echo $Pacient['Pacient']['inmunity_b'] ? '<span class="glyphicon glyphicon-ok"></span> Neumococo: Aplicada' : '<span class="glyphicon glyphicon-remove"></span> Neumococo: No aplicada'; ?></div>
            </div>
            <div>Agente sanitario asignado: <?php echo $Pacient['User']['full_name']; ?></div>
            <div>Estado: <?php echo $statusEnum[$Pacient['Pacient']['status']]; ?></div>
        </td>
        <td class="hidden-xs">
            <?php if($Pacient['Pacient']['status'] == 3) echo $this->html->image('covid-icon.png',array('style'=>'width:24px')); ?>
            <?php echo $Pacient['Pacient']['name']; ?>
        </td>
        <td class="hidden-xs"><?php echo $Pacient['Pacient']['lastname']; ?></td>
        <td class="hidden-xs"><?php echo $Pacient['Pacient']['phone']; ?></td>
        <td class="hidden-xs"><?php echo $Pacient['Pacient']['age']; ?></td>
        <td class="hidden-xs"><?php echo $Pacient['Pacient']['address']; ?></td>
        <td class="hidden-xs"><?php echo $Pacient['Group']['name']; ?></td>
        <td class="hidden-xs"><?php echo $Pacient['Net']['name']; ?></td>
        <td class="hidden-xs"><?php echo $Pacient['Pacient']['main_gate'] ? 'A' : 'B'; ?></td>
        <td class="hidden-xs" style="background-color:<?php echo $color; ?>;color:#fff">
            Gripe: <?php echo $Pacient['Pacient']['inmunity_a'] ? 'Si' : 'No' ?><br/>
            Neumococo: <?php echo $Pacient['Pacient']['inmunity_b'] ? 'Si' : 'No' ?><br/>
        </td>
        <td class="hidden-xs"><?php echo $Pacient['User']['full_name']; ?></td>
        <td class="hidden-xs"><?php echo $statusEnum[$Pacient['Pacient']['status']]; ?></td>
        <td class="hidden-xs">
             <?php echo $this->Form->input('statusAlert.',array('class'=>'status','options'=>array(1 => 'Alerta verde', 2 => 'Alerta amarillo', 3 => 'Alerta naranja', 4 => 'Alerta rojo'), 'default'=>$Pacient['Pacient']['statusAlert'])); ?>
             <?php echo $this->Form->hidden('pacient_id.',array('value'=>$Pacient['Pacient']['id'])); ?>
             <?php echo $this->Form->hidden('fecha.',array('value'=>date('Y-m-d'))); ?>
             <?php echo $this->Form->hidden('hora.',array('value'=>date('H:i:s'))); ?>
             <?php echo $this->Form->hidden('user_id.',array('value'=>$viewUser['id'])); ?>
             <?php echo $this->Form->hidden('group_id.',array('value'=>$Pacient['Pacient']['group_id'])); ?>
             <?php echo $this->Form->hidden('net_id.',array('value'=>$Pacient['Pacient']['net_id'])); ?>
             <?php echo $this->Form->hidden('main_gate.',array('value'=>$Pacient['Pacient']['main_gate'])); ?>
        </td>
        <td>
             <?php echo $this->Form->checkbox('isolated.',array('value'=>true)); ?>
        </td>
    </tr>
    <?php endforeach; ?>
    <?php unset($Pacient); ?>
</tbody>
</table>
<?php
	echo $this->Form->end('Guardar');
?>
<script>
$(function(){
	$('#PacientEstado').change(function(){
		$('.status').val($(this).val());
	})
})
</script>

Your second block of code is unreadable with the formatting like this.

Also, what version of Cake is this running on?

Is running in cakePhp 2.5.6

How can I format the code so that is readable?

This is the code of the controller:

public function block()
	{
        if ($this->request->is('post')) {
			$guardar = false;
			foreach($this->request->data['Pacient']['statusAlert'] as $key => $value){
				$isolated = (!empty($this->request->data['Pacient']['isolated'][$key])) ? $this->request->data['Pacient']['isolated'][$key] : 0;
				$data = array('EstadosxDia'=>array('status' => $value,'isolated'=>$isolated,'fecha' => $this->request->data['Pacient']['fecha'][$key],
					'hora' => $this->request->data['Pacient']['hora'][$key],'user_id' => $this->request->data['Pacient']['user_id'][$key],
					'pacient_id' => $this->request->data['Pacient']['pacient_id'][$key],'group_id' => $this->request->data['Pacient']['group_id'][$key],
					'net_id' =>$this->request->data['Pacient']['net_id'][$key],'main_gate' => $this->request->data['Pacient']['main_gate'][$key]));
				$this->EstadosxDia->create();
				$guardar = $this->EstadosxDia->save($data);
				$turnos = $this->Turnos->find('first',array('conditions'=>array('Turnos.group_id' => $this->request->data['Pacient']['group_id'][$key],
					'Turnos.net_id' => $this->request->data['Pacient']['net_id'][$key],'Turnos.main_gate' => $this->request->data['Pacient']['main_gate'][$key],
					'Turnos.pacient_id' => $this->request->data['Pacient']['pacient_id'][$key],'Turnos.fecha_volver >'=>date('Y-m-d'))));
				if(!empty($turnos) && !empty($this->request->data['Pacient']['isolated'][$key])){
					$fecha_actual = date("Y-m-d");
					$fecha_volver = date("Y-m-d",strtotime($fecha_actual."+ 2 days"));
					$hora_volver = date("H:i:s");
					$this->Turnos->updateAll(array('fecha_volver'=>$fecha_volver,'hora_volver'=>$hora_volver),array('Turnos.id'=>$turnos['Turnos']['id']));
				}
			}
			if($guardar){
				foreach($this->request->data['Pacient']['statusAlert'] as $key => $status){
					$this->Pacient->updateAll(array('statusAlert' => $status,'isolated'=>$this->request->data['Pacient']['isolated'][$key]),
						array('Pacient.id' => $this->request->data['Pacient']['pacient_id'][$key]));
				}
                $this->Session->setFlash(__('Se cambió el estado de los pacientes!'),'msg_success');
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(__('Oops! no se pudo modificar el estado de los pacientes!'),'msg_danger');
		}
		$pacients = $this->Pacient->find('all',array('conditions'=>array('Pacient.group_id' => $this->controllerUser['group_id'], 'Pacient.net_id' => $this->controllerUser['net_id'])));
        $this->set('model',$pacients);
	}
?>

Can someone please answer me this question, or tell me that everything is alright?

I don’t see anything obviously wrong.

I don’t really understand your question here. Do you mean that if you have 3 records and send them through your save loop, record[1] gets record[0] data mapped onto it?

hmm… There is one suspicious detail.

In your Form you create inputs with code like this:

echo $this->Form->hidden('hora.',array('value'=>date('H:i:s')));

The first argument (in this case hora. will create the ‘name’ attribute that will determine the Post data array structure.

In your controller you get the request data with code like this:

'hora' => $this->request->data['Pacient']['hora'][$key]

To get this structure, I would expect to see your inputs make like this:

echo $this->Form->hidden('Pacient.hora.',array('value'=>date('H:i:s')));

Yes, that’s what’s happening in my code

what does the HTML of your form look like?
and what does this->request->data look like?

This is how it looks this->request->data:

Array ( [DataTables_Table_0_length] => 10 [Pacient] => Array ( [estado] => 3 [statusAlert] => Array ( [0] => 3 [1] => 3 ) [pacient_id] => Array ( [0] => 21 [1] => 19 ) [fecha] => Array ( [0] => 2020-05-20 [1] => 2020-05-20 ) [hora] => Array ( [0] => 19:35:30 [1] => 19:35:30 ) [user_id] => Array ( [0] => 26 [1] => 26 ) [group_id] => Array ( [0] => 1 [1] => 1 ) [net_id] => Array ( [0] => 1 [1] => 1 ) [main_gate] => Array ( [0] => 1 [1] => 0 ) [isolated] => Array ( [0] => 0 [1] => 1 [2] => 0 ) ) )

And this is how it looks the :

<form action="/estadoatulado/Pacients/block" id="PacientBlockForm" method="post" accept-charset="utf-8"><div style="display:none;"><input type="hidden" name="_method" value="POST"/></div><div class="input select"><label for="PacientEstado">Estado</label><select name="data[Pacient][estado]" class="form-control" id="PacientEstado">
<option value="1">Alerta verde</option>
<option value="2">Alerta amarillo</option>
<option value="3">Alerta naranja</option>
<option value="4">Alerta rojo</option>
</select></div><table class="table table-bordered table-sortable">
<thead>
    <tr>
        <th class="hidden-xs">Nombre</th>
        <th class="hidden-xs">Apellido</th>
        <th class="hidden-xs">Telefono</th>
        <th class="hidden-xs">Edad</th>
        <th class="hidden-xs">Direccion</th>
        <th class="hidden-xs">Grupo</th>
        <th class="hidden-xs">Red</th>
        <th class="hidden-xs">Nodo</th>
        <th class="hidden-xs">Inmunización</th>
        <th class="hidden-xs">Agente asignado</th>
        <th class="hidden-xs">Estado</th>
        <th class="hidden-xs">Acciones</th>
        <th class="hidden-xs">Aislado</th>
    </tr>
</thead>
<tbody>

    
        <tr>
        <td class="hidden-xs">
                        Zunilda         </td>
        <td class="hidden-xs">Giustetto</td>
        <td class="hidden-xs">3512046200</td>
        <td class="hidden-xs">72</td>
        <td class="hidden-xs">Brown esq. Lagunilla</td>
        <td class="hidden-xs">Celula 1</td>
        <td class="hidden-xs">Celula 1 - UF No.1</td>
        <td class="hidden-xs">B</td>
        <td class="hidden-xs" style="background-color:#f44336;color:#fff">
            Gripe: No<br/>
            Neumococo: No<br/>
        </td>
        <td class="hidden-xs">Jose Bergagna</td>
        <td class="hidden-xs">Sano</td>
        <td class="hidden-xs">
             <div class="input select"><label for="PacientStatusAlert"></label><select name="data[Pacient][statusAlert][]" class="status" id="PacientStatusAlert">
<option value="1">Alerta verde</option>
<option value="2">Alerta amarillo</option>
<option value="3" selected="selected">Alerta naranja</option>
<option value="4">Alerta rojo</option>
</select></div>             <input type="hidden" name="data[Pacient][pacient_id][]" value="19" id="PacientPacientId"/>             <input type="hidden" name="data[Pacient][fecha][]" value="2020-05-20" id="PacientFecha"/>             <input type="hidden" name="data[Pacient][hora][]" value="19:44:01" id="PacientHora"/>             <input type="hidden" name="data[Pacient][user_id][]" value="26" id="PacientUserId"/>             <input type="hidden" name="data[Pacient][group_id][]" value="1" id="PacientGroupId"/>             <input type="hidden" name="data[Pacient][net_id][]" value="1" id="PacientNetId"/>             <input type="hidden" name="data[Pacient][main_gate][]" value="0" id="PacientMainGate"/>        </td>
        <td>
             <input type="hidden" name="data[Pacient][isolated][]" id="PacientIsolated_" value="0"/><input type="checkbox" name="data[Pacient][isolated][]"  value="1" id="PacientIsolated"/>        </td>
    </tr>
        <tr>
        <td class="hidden-xs">
                        Paciente        </td>
        <td class="hidden-xs">Prueba</td>
        <td class="hidden-xs">12345678</td>
        <td class="hidden-xs">70</td>
        <td class="hidden-xs">San Martin 1231, Cordoba Capital</td>
        <td class="hidden-xs">Celula 1</td>
        <td class="hidden-xs">Celula 1 - UF No.1</td>
        <td class="hidden-xs">A</td>
        <td class="hidden-xs" style="background-color:#3c9f40;color:#fff">
            Gripe: Si<br/>
            Neumococo: Si<br/>
        </td>
        <td class="hidden-xs">Nestor Muguiro</td>
        <td class="hidden-xs">Sospechoso</td>
        <td class="hidden-xs">
             <div class="input select"><label for="PacientStatusAlert"></label><select name="data[Pacient][statusAlert][]" class="status" id="PacientStatusAlert">
<option value="1">Alerta verde</option>
<option value="2">Alerta amarillo</option>
<option value="3" selected="selected">Alerta naranja</option>
<option value="4">Alerta rojo</option>
</select></div>             <input type="hidden" name="data[Pacient][pacient_id][]" value="21" id="PacientPacientId"/>             <input type="hidden" name="data[Pacient][fecha][]" value="2020-05-20" id="PacientFecha"/>             <input type="hidden" name="data[Pacient][hora][]" value="19:44:01" id="PacientHora"/>             <input type="hidden" name="data[Pacient][user_id][]" value="26" id="PacientUserId"/>             <input type="hidden" name="data[Pacient][group_id][]" value="1" id="PacientGroupId"/>             <input type="hidden" name="data[Pacient][net_id][]" value="1" id="PacientNetId"/>             <input type="hidden" name="data[Pacient][main_gate][]" value="1" id="PacientMainGate"/>        </td>
        <td>
             <input type="hidden" name="data[Pacient][isolated][]" id="PacientIsolated_" value="0"/><input type="checkbox" name="data[Pacient][isolated][]"  value="1" id="PacientIsolated"/>        </td>
    </tr>
        </tbody>
</table>
<div class="submit"><input  type="submit" value="Guardar"/></div></form>

I’m just not seeing a problem

I found the problem, it got solved by putting ‘hiddenField’ => false