I have two tables with a hasmany association (Events and Schedules, foreing key schedule_id in Events), also I have a hasmany association between Events and EventsUsers, and this last one has a hasmany association with Users and Pacients, I have 4 schedules, one is for Users and the other three are for Pacients, so when I select the schedule for Users I show a grid with the users and when I select the three schedules for pacients I show a select where I choose one pacient, I use jquery to show or hide the grid or the select, but when I submit the form for a pacient it sends all the users and I only want to send one user, how can I do that?
This is the code for add view:
<?php
/**
* @var \App\View\AppView $this
* @var \App\Model\Entity\Event $event
* @var \Cake\Collection\CollectionInterface|string[] $groups
* @var \Cake\Collection\CollectionInterface|string[] $nets
* @var \Cake\Collection\CollectionInterface|string[] $schedules
* @var \Cake\Collection\CollectionInterface|string[] $users
*/
?>
<div class="row">
<div class="column-responsive column-80">
<div class="events form content">
<?= $this->Form->create($event) ?>
<fieldset>
<legend><?= __('Agregar Evento') ?></legend>
<?php
echo $this->Form->control('description',['label'=>'Descripción del evento']);
echo $this->Form->control('schedule_id',['label'=>'Agenda','empty' => '-- Seleccionar agenda --']);
echo '<div id="selectPacient" style="display:none;">';
echo $this->Form->control('events_users.0.pacient_id',['label'=>'Paciente','options'=>$pacients,'empty' => '-- Seleccionar paciente --']);
echo $this->Form->hidden('events_users.0.user_id', ['value' => $viewUser["id"]]);
echo $this->Form->control('events_users.0.observation', ['label' => 'Observación']);
echo '</div>';
?>
<div id="selectUser" style="display:none;">
<div class="table-responsive">
<table>
<thead>
<tr>
<th>Usuario</th>
<th>Perfil</th>
<th>Datos</th>
</tr>
</thead>
<tbody>
<?php
$i=0;
foreach ($users as $user):
?>
<tr>
<td><?= $user->full_name ?></td>
<td><?= $user->profile->name ?></td>
<td>
<?php
foreach ($supplies as $supply):
?>
<p>
<?php
echo $supply->description;
echo $this->Form->control('events_users.'.$i.'.quantity',['label' => false, 'placeholder'=>'Ingrese la cantidad']);
echo $this->Form->date('events_users.'.$i.'.deliver_date', ['class' => 'form-date']);
echo $this->Form->hidden('events_users.'.$i.'.user_id',['value'=>$user->id]);
echo $this->Form->hidden('events_users.'.$i.'.supply_id',['value'=>$supply->id]);
?>
</p>
<?php
$i++;
endforeach;
?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</fieldset>
<?= $this->Form->button(__('Guardar')) ?>
<?= $this->Form->end() ?>
</div>
</div>
</div>
<script>
$(function(){
$('#schedule-id').change(function(){
var schedule_id = $(this).val();
switch(schedule_id){
case '1': $('#selectPacient').hide();$('#selectUser').show();
break;
default: $('#selectPacient').show();$('#selectUser').hide();
break;
}
})
})
</script>
And this is the code for add action:
public function add()
{
$this->loadModel("Stocks");
$event = $this->Events->newEmptyEntity();
if ($this->request->is('post')) {
$event = $this->Events->newEntity($this->request->getData(), ['associated' => ['EventsUsers']]);
$event->group_id = $this->controllerUser['group_id'];
$event->net_id = $this->controllerUser['net_id'];
$event->main_gate = $this->controllerUser['main_gate'];
debug($this->request->getData());
if ($this->Events->save($event)) {
if(!empty($this->request->getData("events_users"))){
if($event->schedule_id > 1){
$user_id = $this->request->getData('events_users.0.user_id');
$pacient_id = $this->request->getData('events_users.0.pacient_id');
$observation = $this->request->getData('events_users.0.observation');
$event->event_users->event_id = $event->id;
$event->event_users->user_id = $user_id;
$event->event_users->pacient_id = $pacient_id;
$event->event_users->observation = $observation;
$this->Events->EventsUsers->save($event->event_users);
}else{
foreach($event->events_users as $key => $user){
if(!empty($this->request->getData('events_users.'.$key.'.quantity')) &&
!empty($this->request->getData('events_users.'.$key.'.deliver_date'))){
$user->event_id = $event->id;
$this->Events->EventsUsers->save($user);
}
}
}
}
$this->Flash->success(__('El evento ha sido creado'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('No se pudo crear el evento. Por favor, intente nuevamente'));
}
$pacients = $this->Events->EventsUsers->Pacients->find('list')->where(['Pacients.group_id' => $this->controllerUser['group_id'],
'Pacients.net_id' => $this->controllerUser['net_id'], 'Pacients.main_gate' => $this->controllerUser['main_gate']]);
$users = $this->Events->EventsUsers->Users->find('all')->contain('Profiles')->leftJoinWith('Groups')->where(['GroupsUsers.group_id' => $this->controllerUser['group_id'],
'GroupsUsers.net_id' => $this->controllerUser['net_id'],'GroupsUsers.main_gate' => $this->controllerUser['main_gate']]);
$supplies = $this->Events->EventsUsers->Supplies->find('all');
$schedules = $this->Events->Schedules->find('list');
$this->set(compact('event', 'pacients', 'users', 'supplies', 'schedules'));
}