Update existing records with belongsToMany->link instead of creating new ones in cakephp 4.4

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?

link is to create a link. If you want to update that data, you might unlink and then link? Or you can always create a table class for the join table (also read up on the “through” configuration on belongsToMany associations if you do this) and call save directly on that with the join entity.

Thank you @Zuluru, that worked perfect.
I know about the through configuration in belongsToMany associations, I am already using it.

To update existing records with belongsToMany->link instead of creating new ones in CakePHP 4.4, you can follow these steps:

  1. Fetch the existing records: Retrieve the records you want to update from the database. You can use the Table->find() method to query for specific records or all records for a given model.
  2. Check for existing associations: For each record, check if it already has an association with the related entity. You can use the has() method on the belongsToMany relationship to determine if the association exists.
  3. Update existing associations: If the association already exists, you can update it using the link() method on the belongsToMany relationship. The link() method takes two arguments: the ID of the record to link and an array of IDs for the related entities.
  4. Create new associations: If the association doesn’t exist, you can create it using the link() method. The link() method will create the association and insert the necessary records into the join table.

@alexcray Please refrain from posting ChatGPT and alike generated answers, they’re usually never completely correct, and if you don’t know CakePHP enough to spot and solve the errors in the answers, then they will just create more confusion for the people here seeking help.

Neither does the link() method take IDs, nor is there a need to check for existing relations if you’re using link() anyways.

The key to updating a joint using the link() method, is that a) all involved records are already persisted, and b) that the joint entity is present on the target entities, and c) that for all involved entities the primary and foreign keys are present and unchanged. Only if all those prerequisites are met, link() will update the join record instead of creating a new one.