I want to execute an update instead of an insert into with belongsToMany->link or another method when I have an existing record in the join table, I tried passing in the _joinData entity the id of the join table and markNew = false, but is still adding new records.
This is the code for addProfessional action:
public function addProfessional($id = null)
{
$coordinador = $this->Teams->find('all')->matching('Professionals.Professions')->where(['ProfessionalsTeams.team_id' => $id, 'ProfessionalsTeams.profile_id' => 2])->first();
$team = $this->Teams->get($id);
if ($this->request->is('post')) {
$guardar = false;
$team = $this->Teams->patchEntity($team, $this->request->getData(), ['associated' => 'Professionals']);
foreach($team->professionals as $key => $professional){
$professional_id = $this->request->getData('professionals.'.$key.'.id');
$professionals_team_id = $this->request->getData('professionals.'.$key.'._joinData.id');
$profile_id = $this->request->getData('professionals.'.$key.'._joinData.profile_id');
$generate = 0;
$arrayEntity = !empty($professionals_team_id) ? ['id' => $professionals_team_id, 'profile_id' => $profile_id, 'generate' => $generate, 'anulled' => 0] : ['profile_id' => $profile_id, 'generate' => $generate, 'anulled' => 0];
$markNew = ['markNew' => empty($professionals_team_id)];
$joinData = new Entity($arrayEntity, $markNew);
$professional->_joinData = $joinData;
$guardar = $this->Teams->Professionals->link($team, [$professional]);
if($guardar){
$professional->selected = 0;
$guardar = $this->Teams->Professionals->save($professional);
$professionalsTeam = $this->Teams->ProfessionalsTeams->find()->where(['team_id' => $team->id, 'professional_id' => $professional_id])->first();
$logProfessionalsTeam = $this->Teams->LogProfessionalsTeams->newEmptyEntity();
$logProfessionalsTeam->user_id = $this->user_id;
$logProfessionalsTeam->operation = empty($professionals_team_id) ? "A" : "M";
$logProfessionalsTeam->professionals_team_id = $professionalsTeam->id;
$logProfessionalsTeam->team_id = $team->id;
$logProfessionalsTeam->professional_id = $professional_id;
$logProfessionalsTeam->profile_id = $profile_id;
$logProfessionalsTeam->generate = $generate;
$logProfessionalsTeam->anulled = 0;
$guardar = $this->Teams->LogProfessionalsTeams->link($team, [$logProfessionalsTeam]);
}
if(!$guardar) break;
}
if($guardar){
$this->Flash->success(__('Los profesionales han sido agregados al equipo.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Los profesionales no pudieron ser agregados al equipo. Por favor, intente nuevamente.'));
}
$professions = $this->Professions->find('list');
if(empty($professionals)) $professionals = $this->paginate($this->Teams->Professionals->find('all')->contain('Professions')
->where(['Professionals.id !=' => $coordinador->_matchingData['Professionals']->id, 'Professionals.selected' => 0]));
$professionalsSelected = $this->Teams->find('all')->matching('ProfessionalsTeams')->select('ProfessionalsTeams.professional_id')
->where(['ProfessionalsTeams.team_id' => $id, 'ProfessionalsTeams.profile_id !=' => 2]);
if(!empty($professionalsSelected->count()))
$this->Teams->Professionals->updateAll(['Professionals.selected' => 1], ['Professionals.id in' => $professionalsSelected]);
$selected = $this->Teams->Professionals->find('all')->contain(['Professions', 'ProfessionalsTeams' => function(Query $q) use ($id){
return $q->where(['ProfessionalsTeams.team_id' => $id]);
}])->where(['selected' => 1])->all();
$paciente = $this->Teams->get($id, ['contain' => 'Pacients']);
$profiles = $this->Teams->ProfessionalsTeams->Profiles->find('list')->where(['Profiles.name in' => ['Profesional', 'Secretaria']]);
$date = new FrozenDate(date('Y-m-d'));
$this->set('date', $date);
$count = $selected->count();
$this->set('count', $count);
$this->set('id', $id);
$this->set(compact('team', 'professions', 'professionals', 'selected', 'coordinador', 'paciente', 'profiles'));
}
How can I solve this issue?