I have a belongsto association between StatesxDays and three tables (Users (this with two foreign keys: user_id and doctor_id), Groups, Nets), this table has these fields: id, user_id, group_id, net_id, main_gate, doctor_id, date, hour, status, created and modified, when I try to save various records to this table it gives me an error that date, hour and status ar required fields.
This is the code for addUser action:
public function addUser() {
$this->loadModel('SourcingEvents');
$statesxDay = $this->StatesxDays->newEmptyEntity();
if ($this->request->is('post')) {
$guardar = false;
$statesxDay = $this->StatesxDays->newEntities($this->request->getData(), ['associated' => ['Users' => ['onlyIds' => true],
'Nets' => ['onlyIds' => true], 'Groups' => ['onlyIds' => true], 'Doctor' => ['onlyIds' => true]]]);
debug($statesxDay);
foreach($statesxDay as $data){
$guardar = $this->StatesxDays->save($data);
}
if($guardar){
$this->Flash->success(__('Se modificaron los estados de los usuarios.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('No se pudieron modificar los estados de los usuarios'));
}
$users = $this->StatesxDays->Users->find('all')->leftJoinWith('Groups')->where(['GroupsUsers.net_id' => $this->controllerUser['net_id']])
->contain(['Groups.GroupsUsers.Nets', 'Profiles']);
$eventos = $this->SourcingEvents->find('list');
$this->set(compact('statesxDay', 'users', 'eventos'));
}
This is the code for addUser view:
<?php
/**
* @var \App\View\AppView $this
* @var iterable<\App\Model\Entity\EstadosxDia> $statesxDays
*/
?>
<div class="statesxDays add_user content">
<?php
echo $this->Form->create($statesxDay);
echo $this->Form->control('estado',['label'=>'Estado','options'=>[1 => 'Alerta verde', 2 => 'Alerta amarillo', 3 => 'Alerta naranja', 4 => 'Alerta rojo'], 'id' => 'estadoId']);
echo '<div id="selectEVent" style="display:none;">'.$this->Form->control('evento_id',['label'=>'Tipo de evento','options'=>$eventos,'empty'=>'-- Seleccionar evento --']).'</div>';
echo $this->Form->label('seleccionar', 'Seleccionar todos').$this->Form->checkbox('seleccionar',['id' => 'seleccionar']);
?>
<h3><?= __('Cambiar estado de recursos humanos') ?></h3>
<div class="table-responsive">
<table>
<thead>
<tr>
<th>Nivel</th>
<th>Foto</th>
<th>Email</th>
<th>Nombre</th>
<th>Apellido</th>
<th>Rol</th>
<th>Celula</th>
<th>UF</th>
<th>Nodo</th>
<th>Estado</th>
<th>Aislado</th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
foreach ($users as $user):
$color = '';
switch ($user->profile_id) {
case '2':
$color = '#000000';
break;
case '3':
$color = '#041d54';
break;
case '4':
$color = '#540404';
break;
case '5':
$color = '#6D147A';
break;
case '6':
$color = '#CC7400';
break;
case '7':
$color = '#2E2EBF';
break;
default:
$color = '#30773D';
break;
}
$group = '';
$net = '';
$main_gate = '';
if($user->profile_id >= 3):
$group = $user->groups[0]->group_name;
if($user->profile_id >= 4) $net = $user->groups[0]->groups_users[$i]->net->net_name;
$main_gate = $user->groups[0]->_joinData->main_gate;
echo $this->Form->hidden('statesxDays.'.$i.'.group_id',['value'=>$user->groups[0]->id]);
echo $this->Form->hidden('statesxDays.'.$i.'.net_id',['value'=>$user->groups[0]->_joinData->net_id]);
if(!empty($main_gate)) echo $this->Form->hidden('statesxDays.'.$i.'.main_gate',['value'=>$main_gate]);
endif
?>
<tr>
<td><?php echo $user->profile_id; ?></td>
<td><?php echo $this->Html->image($user->image_file_name_url, ['alt' => $user->image_file_name_filename, 'class' => 'img-circle']); ?></td>
<td><?php echo $user->email; ?></td>
<td><?php echo $user->firstname; ?></td>
<td><?php echo $user->lastname; ?></td>
<td style="background-color:<?php echo $color; ?>;color:#fff"><?php echo $user->profile->name; ?></td>
<td><?php echo $group; ?></td>
<td><?php echo $net; ?></td>
<td><?php if ($user->profile_id > 4) { echo '#'.$main_gate; } else {echo ' - ';} ?></td>
<td>
<?php echo $this->Form->control('statesxDays.'.$i.'.status',['label'=>false, 'options'=>[1 => 'Alerta verde', 2 => 'Alerta amarillo', 3 => 'Alerta naranja', 4 => 'Alerta rojo'], 'default' => $user->status, 'class'=>'status']); ?>
<?php echo $this->Form->hidden('statesxDays.'.$i.'.date',['value'=>date('Y-m-d')]); ?>
<?php echo $this->Form->hidden('statesxDays.'.$i.'.hour',['value'=>date('H:i:s')]); ?>
<?php echo $this->Form->hidden('statesxDays.'.$i.'.user_id',['value'=>$viewUser['id']]); ?>
<?php echo $this->Form->hidden('statesxDays.'.$i.'.doctor_id',['value'=>$user->id]); ?>
</td>
<td>
<?php echo $this->Form->checkbox('users.'.$i.'.isolated',['value'=>true,'hiddenField' => false, 'class' => 'isolated']); ?>
</td>
</tr>
<?php
$i++;
endforeach;
?>
</tbody>
</table>
</div>
<?= $this->Form->button(__('Guardar')) ?>
<?= $this->Form->end() ?>
</div>
<script>
$(function(){
$('#estadoId').change(function(){
status = $(this).val();
$('.status').val(status);
if(status=="1" || status=="3") $('#selectEVent').show();
else $('#selectEVent').hide();
})
})
$(function(){
$('#seleccionar').change(function() {
$('.isolated').prop('checked', $(this).is(':checked'));
});
});
</script>
This is what it shows debug($this->request->getData()):
APP/Controller/StatesxDaysController.php (line 55)
[
'estado' => '4',
'evento_id' => '',
'seleccionar' => '1',
'statesxDays' => [
(int) 0 => [
'group_id' => '1',
'net_id' => '1',
'status' => '4',
'date' => '2023-04-02',
'hour' => '16:57:22',
'user_id' => '4',
'doctor_id' => '4',
],
(int) 1 => [
'group_id' => '1',
'net_id' => '1',
'main_gate' => 'A',
'status' => '4',
'date' => '2023-04-02',
'hour' => '16:57:22',
'user_id' => '4',
'doctor_id' => '5',
],
(int) 2 => [
'group_id' => '1',
'net_id' => '1',
'main_gate' => 'B',
'status' => '4',
'date' => '2023-04-02',
'hour' => '16:57:22',
'user_id' => '4',
'doctor_id' => '6',
],
(int) 3 => [
'group_id' => '1',
'net_id' => '1',
'main_gate' => 'A',
'status' => '4',
'date' => '2023-04-02',
'hour' => '16:57:22',
'user_id' => '4',
'doctor_id' => '7',
],
(int) 4 => [
'group_id' => '1',
'net_id' => '1',
'main_gate' => 'B',
'status' => '4',
'date' => '2023-04-02',
'hour' => '16:57:22',
'user_id' => '4',
'doctor_id' => '8',
],
(int) 5 => [
'group_id' => '1',
'net_id' => '1',
'main_gate' => 'A',
'status' => '4',
'date' => '2023-04-02',
'hour' => '16:57:22',
'user_id' => '4',
'doctor_id' => '9',
],
(int) 6 => [
'group_id' => '1',
'net_id' => '1',
'main_gate' => 'B',
'status' => '4',
'date' => '2023-04-02',
'hour' => '16:57:22',
'user_id' => '4',
'doctor_id' => '10',
],
],
'users' => [
(int) 0 => [
'isolated' => '1',
],
(int) 1 => [
'isolated' => '1',
],
(int) 2 => [
'isolated' => '1',
],
(int) 3 => [
'isolated' => '1',
],
(int) 4 => [
'isolated' => '1',
],
(int) 5 => [
'isolated' => '1',
],
(int) 6 => [
'isolated' => '1',
]
],
]
This is what it shows this debug():
APP/Controller/StatesxDaysController.php (line 55)
[
(int) 0 => object(App\Model\Entity\StatesxDay) id:0 {
'[new]' => true
'[accessible]' => [
'user_id' => true,
'group_id' => true,
'net_id' => true,
'main_gate' => true,
'pacient_id' => true,
'doctor_id' => true,
'date' => true,
'hour' => true,
'status' => true,
'created' => true,
'modified' => true,
'user' => true,
'group' => true,
'net' => true,
'pacient' => true,
]
'[dirty]' => [
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => true
'[errors]' => [
'date' => [
'_required' => 'This field is required',
],
'hour' => [
'_required' => 'This field is required',
],
'status' => [
'_required' => 'This field is required',
],
]
'[invalid]' => [
]
'[repository]' => 'StatesxDays'
},
(int) 1 => object(App\Model\Entity\StatesxDay) id:1 {
'[new]' => true
'[accessible]' => [
'user_id' => true,
'group_id' => true,
'net_id' => true,
'main_gate' => true,
'pacient_id' => true,
'doctor_id' => true,
'date' => true,
'hour' => true,
'status' => true,
'created' => true,
'modified' => true,
'user' => true,
'group' => true,
'net' => true,
'pacient' => true,
]
'[dirty]' => [
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => true
'[errors]' => [
'date' => [
'_required' => 'This field is required',
],
'hour' => [
'_required' => 'This field is required',
],
'status' => [
'_required' => 'This field is required',
],
]
'[invalid]' => [
]
'[repository]' => 'StatesxDays'
},
]
What am I doing wrong?