I have two tables associated with belongsTomany (Teams and Professionals, join table ProfessionalsTeams) when I try to save them in TeamsController I debug($team)
and shows me this:
APP/Controller\TeamsController.php (line 66)
object(App\Model\Entity\Team) id:0 {
'pacient_id' => (int) 28
'professionals' => [
]
'date' => object(Cake\I18n\FrozenDate) id:1 {
'time' => '2023-11-21 00:00:00.000000-03:00'
'timezone' => 'America/Argentina/Cordoba'
'fixedNowTime' => false
}
'observations' => 'Prueba de profesionales'
'anulled' => false
'_joinData' => object(Cake\ORM\Entity) id:2 {
'profile_id' => '2'
'generate' => '1'
'[new]' => true
'[accessible]' => [
'*' => true,
]
'[dirty]' => [
'profile_id' => true,
'generate' => true,
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => false
'[errors]' => [
]
'[invalid]' => [
]
'[repository]' => ''
}
'[new]' => true
'[accessible]' => [
'pacient_id' => true,
'date' => true,
'observations' => true,
'created' => true,
'modified' => true,
'anulled' => true,
'pacient' => true,
'professionals' => true,
]
'[dirty]' => [
'pacient_id' => true,
'professionals' => true,
'date' => true,
'observations' => true,
'anulled' => true,
'_joinData' => true,
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => false
'[errors]' => [
]
'[invalid]' => [
]
'[repository]' => 'Teams'
}
This is the code for add action:
public function add()
{
$team = $this->Teams->newEmptyEntity();
if ($this->request->is('post')) {
$team = $this->Teams->newEntity($this->request->getData(), ['associated' => 'Professionals']);
$profile_id = $this->request->getData('professionals.0._joinData.profile_id');
$generate = $this->request->getData('professionals.0._joinData.generate');
$joinData = new Entity(['profile_id' => $profile_id, 'generate' => $generate], ['markNew' => true]);
$team->_joinData = $joinData;
if ($this->Teams->save($team)) {
$this->Flash->success(__('El equipo ha sido guardado.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('El equipo no pudo ser guardado. Por favor, intente nuevamente.'));
}
$pacients = $this->Teams->Pacients->find('list', ['limit' => 200])->all();
$professionals = $this->Teams->Professionals->find('list', ['limit' => 200])->all();
$this->set(compact('team', 'pacients', 'professionals'));
}
And this is the code for add template:
<?php
/**
* @var \App\View\AppView $this
* @var \App\Model\Entity\Team $team
* @var \Cake\Collection\CollectionInterface|string[] $pacients
* @var \Cake\Collection\CollectionInterface|string[] $professionals
*/
?>
<div class="row">
<?php
$this->Breadcrumbs->add([
['title' => 'Inicio', 'url' => ['controller' => 'Home', 'action' => 'index'], 'options' => ['class' => 'breadcrumb-item']],
['title' => 'Equipos', 'url' => ['action' => 'index'], 'options' => ['class' => 'breadcrumb-item']],
['title' => 'Agregar Equipo', 'options' => ['class' => 'breadcrumb-item']],
]);
?>
<?= $this->Breadcrumbs->render(['class' => 'breadcrumb']) ?>
<div class="column-responsive column-80">
<div class="teams form content">
<?= $this->Form->create($team) ?>
<fieldset>
<legend><?= __('Agregar Equipo') ?></legend>
<?php
echo $this->Form->control('pacient_id', ['label' => 'Paciente', 'options' => $pacients, 'empty' => '- Seleccione un paciente -', 'id' => 'pacient']);
echo $this->Html->tag('div', '', ['class'=>'tagPaciente', 'style'=>'display:none', 'id'=>'showPacient']);
echo $this->Form->control('professionals._ids', ['label' => 'Coordinador', 'options' => $professionals, 'empty' => '- Seleccione un profesional -', 'multiple' => false, 'id' => 'professional']);
echo $this->Form->hidden('professionals.0._joinData.profile_id', ['value' => 2]);
echo $this->Form->hidden('professionals.0._joinData.generate', ['value' => 1]);
echo $this->Html->tag('div', '', ['class'=>'tagProfesional', 'style'=>'display:none', 'id'=>'showProfessional']);
echo $this->Form->control('date', ['label' => 'Fecha', 'value' => date('Y-m-d'), 'class' => 'input-date']);
echo $this->Form->control('observations', ['label' => 'Observaciones']);
echo $this->Form->hidden('anulled', ['value' => 0]);
?>
</fieldset>
<?= $this->Form->button(__('Guardar')) ?>
<?= $this->Form->end() ?>
</div>
</div>
</div>
<script>
$(function(){
$('#pacient').change(function(){
$.ajax({
method:"POST",
url:"<?= $this->Url->build(['controller' => 'Pacients', 'action' => 'showPacient']) ?>",
data:{
pacient_id:$(this).val()
},
success:function(data) {
$('#showPacient').html(data);
$('.tagPaciente').show();
},
headers:{
'X-CSRF-Token':$('meta[name="csrfToken"]').attr('content')
}
});
});
})
$(function(){
$('#professional').change(function(){
$.ajax({
method:"POST",
url:"<?= $this->Url->build(['controller' => 'Professionals', 'action' => 'showProfessional']) ?>",
data:{
professional_id:$(this).val()
},
success:function(data) {
$('#showProfessional').html(data);
$('.tagProfesional').show();
},
headers:{
'X-CSRF-Token':$('meta[name="csrfToken"]').attr('content')
}
});
});
})
</script>
How can I solve this issue?