# Can't save associated model

**URL:** <https://discourse.cakephp.org/t/cant-save-associated-model/11690>\
**Category:** Need Help\
**Created:** [November 21, 2023, 8:19pm UTC](https://discourse.cakephp.org/t/cant-save-associated-model/11690 "2023-11-21T20:19:46Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdeanquin](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mdeanquin/32/1802_2.png) [@mdeanquin](https://discourse.cakephp.org/u/mdeanquin)\
**Post date:** [November 21, 2023, 8:19pm UTC](https://discourse.cakephp.org/t/cant-save-associated-model/11690/1 "2023-11-21T20:19:47Z")

</div>

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:

```auto
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:

```auto
    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:

```auto
<?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?

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [November 22, 2023, 5:07am UTC](https://discourse.cakephp.org/t/cant-save-associated-model/11690/2 "2023-11-22T05:07:55Z")

</div>

What’s the issue? You’ve shown us what it looks like, but not told us what the problem is with it.

---

<div class="post-metadata">

**Author:** ![mdeanquin](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mdeanquin/32/1802_2.png) [@mdeanquin](https://discourse.cakephp.org/u/mdeanquin)\
**Post date:** [November 22, 2023, 3:24pm UTC](https://discourse.cakephp.org/t/cant-save-associated-model/11690/3 "2023-11-22T15:24:12Z")

</div>

Sorry, the problem is that the newentity method doesn’t create the professionals entity and doesn’t save the record for ProfessionalsTeams table

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [November 23, 2023, 2:17am UTC](https://discourse.cakephp.org/t/cant-save-associated-model/11690/4 "2023-11-23T02:17:44Z")

</div>

I suspect that mixing `professionals._ids` and `professionals.0._joinData` in your form may be causing the problem. Given that the join data appears to have fixed values, can you not set those directly in your post handling code in the controller instead of in the form?

---

<div class="post-metadata">

**Author:** ![mdeanquin](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mdeanquin/32/1802_2.png) [@mdeanquin](https://discourse.cakephp.org/u/mdeanquin)\
**Post date:** [November 23, 2023, 6:15pm UTC](https://discourse.cakephp.org/t/cant-save-associated-model/11690/5 "2023-11-23T18:15:20Z")

</div>

Is still not creating the professionals entity, this is what it shows `debug($team)`:

```auto
APP/Controller\TeamsController.php (line 67)
object(App\Model\Entity\Team) id:0 {
  'pacient_id' => (int) 28
  'professionals' => [
  ]
  'date' => object(Cake\I18n\FrozenDate) id:1 {
    'time' => '2023-11-23 00:00:00.000000-03:00'
    'timezone' => 'America/Argentina/Cordoba'
    'fixedNowTime' => false
  }
  'observations' => 'Prueba de profesionales'
  '_joinData' => object(Cake\ORM\Entity) id:2 {
    'profile_id' => (int) 2
    'generate' => (int) 1
    '[new]' => true
    '[accessible]' => [
      '*' => true,
    ]
    '[dirty]' => [
      'profile_id' => true,
      'generate' => true,
    ]
    '[original]' => [
    ]
    '[virtual]' => [
    ]
    '[hasErrors]' => false
    '[errors]' => [
    ]
    '[invalid]' => [
    ]
    '[repository]' => ''
  }
  'anulled' => (int) 0
  '[new]' => true
  '[accessible]' => [
    'pacient_id' => true,
    'date' => true,
    'observations' => true,
    'created' => true,
    'modified' => true,
    'anulled' => true,
    'pacient' => true,
    'log_teams' => true,
    'professionals_teams' => true,
    'log_professionals' => true,
    'professionals' => true,
  ]
  '[dirty]' => [
    'pacient_id' => true,
    'professionals' => true,
    'date' => true,
    'observations' => true,
    '_joinData' => true,
    'anulled' => true,
  ]
  '[original]' => [
  ]
  '[virtual]' => [
  ]
  '[hasErrors]' => false
  '[errors]' => [
  ]
  '[invalid]' => [
  ]
  '[repository]' => 'Teams'
}

```

This is the code for add action:

```auto
    public function add()
    {
        $team = $this->Teams->newEmptyEntity();
        if ($this->request->is('post')) {
            $team = $this->Teams->patchEntity($team, $this->request->getData(), ['associated' => 'Professionals']);
			$profile_id = 2;
			$generate = 1;
			$joinData = new Entity(['profile_id' => $profile_id, 'generate' => $generate], ['markNew' => true]);
            $team->_joinData = $joinData;
			$team->anulled = 0;
			debug($team);
			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:

```auto
<?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->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']);
                ?>
            </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?

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [November 23, 2023, 11:04pm UTC](https://discourse.cakephp.org/t/cant-save-associated-model/11690/6 "2023-11-23T23:04:01Z")

</div>

Is `professionals._ids` an array in the submitted data, or just an integer? You’re looking to just link to one pre-existing professional? Maybe you should be using `_joinData.professional_id` instead of `_ids`? Sorry, I don’t have time to give a comprehensive answer right now, just throwing out some ideas to look at.

---

<div class="post-metadata">

**Author:** ![mdeanquin](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mdeanquin/32/1802_2.png) [@mdeanquin](https://discourse.cakephp.org/u/mdeanquin)\
**Post date:** [November 24, 2023, 3:23am UTC](https://discourse.cakephp.org/t/cant-save-associated-model/11690/7 "2023-11-24T03:23:19Z")

</div>

Maybe that’s the solución because I want to add only one existing professional

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [November 24, 2023, 2:40pm UTC](https://discourse.cakephp.org/t/cant-save-associated-model/11690/8 "2023-11-24T14:40:02Z")

</div>

See also the [link](https://book.cakephp.org/4/en/orm/saving-data.html#associate-many-to-many-records) function. Set the `_joinData` in the entity you are linking. In their example, `$articlesTable->Tags->link($article, [$tag1, $tag2]);`, you’d set it on `$tag1` and `$tag2`, not `$article`.

---

<div class="post-metadata">

**Author:** ![mdeanquin](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mdeanquin/32/1802_2.png) [@mdeanquin](https://discourse.cakephp.org/u/mdeanquin)\
**Post date:** [November 24, 2023, 7:59pm UTC](https://discourse.cakephp.org/t/cant-save-associated-model/11690/9 "2023-11-24T19:59:19Z")

</div>

I did as you suggested but when I try to save the entities using `$team = $this->Teams->newEntity($this->request->getData(), ['associated' => 'Professionals']);` gives me these errors (`pr($team->getErrors())`):

```auto
Array
(
    [professionals] => Array
        (
            [0] => Array
                (
                    [last_name] => Array
                        (
                            [_required] => This field is required
                        )

                    [name] => Array
                        (
                            [_required] => This field is required
                        )

                    [document_type] => Array
                        (
                            [_required] => This field is required
                        )

                    [document_number] => Array
                        (
                            [_required] => This field is required
                        )

                    [birthday] => Array
                        (
                            [_required] => This field is required
                        )

                    [sex] => Array
                        (
                            [_required] => This field is required
                        )

                    [image_file_name] => Array
                        (
                            [_required] => This field is required
                        )

                    [selected] => Array
                        (
                            [_required] => This field is required
                        )

                    [anulled] => Array
                        (
                            [_required] => This field is required
                        )

                )

        )

)

```

In these errors are involved the fields of professionals table.  
And when I use `$team = $this->Teams->newEntity($this->request->getData(), ['associated' => ['Professionals' => ['onlyIds' => true]]]);` it gives me this exception:

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/8/8cbde91f8be00935d3c11a91725f42459ed5d94c.png)  
This is the code for add action:

```auto
    public function add()
    {
        $team = $this->Teams->newEmptyEntity();
        if ($this->request->is('post')) {
            $team = $this->Teams->newEntity($this->request->getData(), ['associated' => ['Professionals' => ['onlyIds' => true]]]);
			$professional_id = $this->request->getData('professionals.0._joinData.professional_id');
			$profile_id = 2;
			$generate = 1;
			$joinData = new Entity(['professional_id' => $professional_id, 'profile_id' => $profile_id, 'generate' => $generate, 'anulled' => 0], ['markNew' => true]);
            $team->professionals[0]->_joinData = $joinData;
			$team->anulled = 0;
			// debug($team);
			if ($this->Teams->save($team)) {
                $this->Flash->success(__('El equipo ha sido guardado.'));

                return $this->redirect(['action' => 'index']);
            }
            pr($team->getErrors());
			// $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'));
    }

```

How can I avoid the addition of a new record in professionals table?

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [November 24, 2023, 11:16pm UTC](https://discourse.cakephp.org/t/cant-save-associated-model/11690/10 "2023-11-24T23:16:26Z")

</div>

You could use `link`, as I suggested in my previous comment.

---

<div class="post-metadata">

**Author:** ![mdeanquin](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mdeanquin/32/1802_2.png) [@mdeanquin](https://discourse.cakephp.org/u/mdeanquin)\
**Post date:** [November 25, 2023, 1:18pm UTC](https://discourse.cakephp.org/t/cant-save-associated-model/11690/11 "2023-11-25T13:18:47Z")

</div>

Thank you @Zuluru, now it works perfect
