I have two tables with a belongstomany association (Profiles and SourcingEvents, join table SourcingEventsProfiles), the primary key for SourcingEventsProfiles is sourcing_event_id, profile_id and main_gate, I want to save one record to SourcingEvents and various records to SourcingEventsProfiles and no records in Profiles, for that I use a table named ProfilesGates that is not related to the other tables, the problem is that in ProfilesGates I have records where profile_id is repeated because there are some profiles that have two main_gate (A or B), how can I solve this issue?
This is the code for add action:
public function add()
{
$this->loadModel("ProfilesGates");
$sourcingEvent = $this->SourcingEvents->newEmptyEntity();
if ($this->request->is('post')) {
$sourcingEvent = $this->SourcingEvents->newEntity($this->request->getData(), [
'associated' => [
'Profiles._joinData' => [
'accessibleFields' => ['sourcing_event_id' => true, 'profile_id' => true, 'main_gate' => true],
]
]
]);
if(!empty($sourcingEvent->profiles)) {
$joinData = [];
foreach($sourcingEvent->profiles as $key => $data){
if(!empty($data->_joinData->profile_id)){
$profile_id = $data->_joinData->profile_id;
$main_gate = $data->_joinData->main_gate;
$joinData[$key] = new Entity(['profile_id' => $profile_id, 'main_gate' => $main_gate], ['markNew' => true]);
}
}
$sourcingEvent->_joinData = $joinData;
}
debug($sourcingEvent);
if ($this->SourcingEvents->save($sourcingEvent, ['associated' => 'SourcingEventsProfiles'])) {
$this->Flash->success(__('El evento ha sido creado'));
return $this->redirect(array('action' => 'index'));
}
$this->Flash->error(__('No se pudo crear el evento'));
}
$profiles = $this->ProfilesGates->find('all')->contain('Profiles');
$this->set(compact('sourcingEvent', 'profiles'));
}
This is the code for add view:
<?php
/**
* @var \App\View\AppView $this
* @var \App\Model\Entity\SourcingEvent $sourcingEvent
* @var \Cake\Collection\CollectionInterface|string[] $profiles
*/
?>
<div class="row">
<div class="column-responsive column-80">
<div class="sourcingEvents form content">
<?= $this->Form->create($sourcingEvent) ?>
<fieldset>
<legend><?= __('Dar de alta evento de abastecimiento') ?></legend>
<?php
echo $this->Form->control('description',['label'=>'Descripción del evento']);
echo $this->Form->button('Quitar selección',['type' => 'button', 'id' => 'quitar', 'class' => 'button float-right']);
echo $this->Form->button('Seleccionar todos',['type' => 'button', 'id' => 'seleccionar', 'class' => 'button float-right']);
?>
<div class="table-responsive">
<table>
<thead>
<tr>
<th>Perfil</th>
<th>Nodo</th>
<th>Seleccionar</th>
</tr>
</thead>
<tbody>
<?php
$i=0;
foreach ($profiles as $profile):
$color = '';
switch ($profile->profile_id) {
case '4':
$color = '#6D147A';
break;
case '5':
$color = '#CC7400';
break;
case '6':
$color = '#2E2EBF';
break;
default:
$color = '#30773D';
break;
}
?>
<tr>
<td align="center" style="background-color:<?= $color ?>;color:#fff"><?= $profile->profile->name ?></td>
<td align="center"><?= !empty($profile->main_gate) ? '#'.$profile->main_gate : ' - ' ?></td>
<td align="center">
<?= $this->Form->checkbox('profiles.'.$i.'._joinData.profile_id', ['value'=>$profile->profile_id, 'hiddenField' => false, 'class' => 'profile']) ?>
<?= $this->Form->hidden('profiles.'.$i.'._joinData.main_gate', ['value'=>$profile->main_gate]) ?>
</td>
</tr>
<?php
$i++;
endforeach;
?>
</tbody>
</table>
</div>
</fieldset>
<?= $this->Form->button(__('Guardar')) ?>
<?= $this->Form->end() ?>
</div>
</div>
</div>
<script>
$(function(){
$('#seleccionar').click(function() {
$('.profile').prop('checked', true);
});
});
$(function(){
$('#quitar').click(function() {
$('.profile').prop('checked', false);
});
});
</script>
And this is what it shows debug($sourcingEvent):
APP/Controller/SourcingEventsController.php (line 75)
object(App\Model\Entity\SourcingEvent) id:0 {
'description' => 'Prueba de evento'
'profiles' => [
(int) 0 => object(App\Model\Entity\Profile) id:1 {
'_joinData' => object(App\Model\Entity\SourcingEventsProfile) id:2 {
'profile_id' => (int) 4
'[new]' => true
'[accessible]' => [
'created' => true,
'modified' => true,
'sourcing_event' => true,
'profile' => true,
'sourcing_event_id' => true,
'profile_id' => true,
'main_gate' => true,
]
'[dirty]' => [
'profile_id' => true,
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => false
'[errors]' => [
]
'[invalid]' => [
]
'[repository]' => 'SourcingEventsProfiles'
}
'[new]' => true
'[accessible]' => [
'name' => true,
'description' => true,
'referrer_profile_id' => true,
'created' => true,
'modified' => true,
'profiles_gates' => true,
'users' => true,
'sourcing_events' => true,
]
'[dirty]' => [
'_joinData' => true,
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => false
'[errors]' => [
]
'[invalid]' => [
]
'[repository]' => 'Profiles'
},
(int) 1 => object(App\Model\Entity\Profile) id:3 {
'_joinData' => object(App\Model\Entity\SourcingEventsProfile) id:4 {
'profile_id' => (int) 5
'main_gate' => 'A'
'[new]' => true
'[accessible]' => [
'created' => true,
'modified' => true,
'sourcing_event' => true,
'profile' => true,
'sourcing_event_id' => true,
'profile_id' => true,
'main_gate' => true,
]
'[dirty]' => [
'profile_id' => true,
'main_gate' => true,
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => false
'[errors]' => [
]
'[invalid]' => [
]
'[repository]' => 'SourcingEventsProfiles'
}
'[new]' => true
'[accessible]' => [
'name' => true,
'description' => true,
'referrer_profile_id' => true,
'created' => true,
'modified' => true,
'profiles_gates' => true,
'users' => true,
'sourcing_events' => true,
]
'[dirty]' => [
'_joinData' => true,
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => false
'[errors]' => [
]
'[invalid]' => [
]
'[repository]' => 'Profiles'
},
(int) 2 => object(App\Model\Entity\Profile) id:5 {
'_joinData' => object(App\Model\Entity\SourcingEventsProfile) id:6 {
'profile_id' => (int) 5
'main_gate' => 'B'
'[new]' => true
'[accessible]' => [
'created' => true,
'modified' => true,
'sourcing_event' => true,
'profile' => true,
'sourcing_event_id' => true,
'profile_id' => true,
'main_gate' => true,
]
'[dirty]' => [
'profile_id' => true,
'main_gate' => true,
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => false
'[errors]' => [
]
'[invalid]' => [
]
'[repository]' => 'SourcingEventsProfiles'
}
'[new]' => true
'[accessible]' => [
'name' => true,
'description' => true,
'referrer_profile_id' => true,
'created' => true,
'modified' => true,
'profiles_gates' => true,
'users' => true,
'sourcing_events' => true,
]
'[dirty]' => [
'_joinData' => true,
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => false
'[errors]' => [
]
'[invalid]' => [
]
'[repository]' => 'Profiles'
},
(int) 3 => object(App\Model\Entity\Profile) id:7 {
'_joinData' => object(App\Model\Entity\SourcingEventsProfile) id:8 {
'profile_id' => (int) 6
'main_gate' => 'A'
'[new]' => true
'[accessible]' => [
'created' => true,
'modified' => true,
'sourcing_event' => true,
'profile' => true,
'sourcing_event_id' => true,
'profile_id' => true,
'main_gate' => true,
]
'[dirty]' => [
'profile_id' => true,
'main_gate' => true,
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => false
'[errors]' => [
]
'[invalid]' => [
]
'[repository]' => 'SourcingEventsProfiles'
}
'[new]' => true
'[accessible]' => [
'name' => true,
'description' => true,
'referrer_profile_id' => true,
'created' => true,
'modified' => true,
'profiles_gates' => true,
'users' => true,
'sourcing_events' => true,
]
'[dirty]' => [
'_joinData' => true,
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => false
'[errors]' => [
]
'[invalid]' => [
]
'[repository]' => 'Profiles'
},
(int) 4 => object(App\Model\Entity\Profile) id:9 {
'_joinData' => object(App\Model\Entity\SourcingEventsProfile) id:10 {
'profile_id' => (int) 6
'main_gate' => 'B'
'[new]' => true
'[accessible]' => [
'created' => true,
'modified' => true,
'sourcing_event' => true,
'profile' => true,
'sourcing_event_id' => true,
'profile_id' => true,
'main_gate' => true,
]
'[dirty]' => [
'profile_id' => true,
'main_gate' => true,
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => false
'[errors]' => [
]
'[invalid]' => [
]
'[repository]' => 'SourcingEventsProfiles'
}
'[new]' => true
'[accessible]' => [
'name' => true,
'description' => true,
'referrer_profile_id' => true,
'created' => true,
'modified' => true,
'profiles_gates' => true,
'users' => true,
'sourcing_events' => true,
]
'[dirty]' => [
'_joinData' => true,
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => false
'[errors]' => [
]
'[invalid]' => [
]
'[repository]' => 'Profiles'
},
(int) 5 => object(App\Model\Entity\Profile) id:11 {
'_joinData' => object(App\Model\Entity\SourcingEventsProfile) id:12 {
'profile_id' => (int) 7
'main_gate' => 'A'
'[new]' => true
'[accessible]' => [
'created' => true,
'modified' => true,
'sourcing_event' => true,
'profile' => true,
'sourcing_event_id' => true,
'profile_id' => true,
'main_gate' => true,
]
'[dirty]' => [
'profile_id' => true,
'main_gate' => true,
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => false
'[errors]' => [
]
'[invalid]' => [
]
'[repository]' => 'SourcingEventsProfiles'
}
'[new]' => true
'[accessible]' => [
'name' => true,
'description' => true,
'referrer_profile_id' => true,
'created' => true,
'modified' => true,
'profiles_gates' => true,
'users' => true,
'sourcing_events' => true,
]
'[dirty]' => [
'_joinData' => true,
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => false
'[errors]' => [
]
'[invalid]' => [
]
'[repository]' => 'Profiles'
},
(int) 6 => object(App\Model\Entity\Profile) id:13 {
'_joinData' => object(App\Model\Entity\SourcingEventsProfile) id:14 {
'profile_id' => (int) 7
'main_gate' => 'B'
'[new]' => true
'[accessible]' => [
'created' => true,
'modified' => true,
'sourcing_event' => true,
'profile' => true,
'sourcing_event_id' => true,
'profile_id' => true,
'main_gate' => true,
]
'[dirty]' => [
'profile_id' => true,
'main_gate' => true,
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => false
'[errors]' => [
]
'[invalid]' => [
]
'[repository]' => 'SourcingEventsProfiles'
}
'[new]' => true
'[accessible]' => [
'name' => true,
'description' => true,
'referrer_profile_id' => true,
'created' => true,
'modified' => true,
'profiles_gates' => true,
'users' => true,
'sourcing_events' => true,
]
'[dirty]' => [
'_joinData' => true,
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => false
'[errors]' => [
]
'[invalid]' => [
]
'[repository]' => 'Profiles'
},
]
'_joinData' => [
(int) 0 => object(Cake\ORM\Entity) id:15 {
'profile_id' => (int) 4
'main_gate' => null
'[new]' => true
'[accessible]' => [
'*' => true,
]
'[dirty]' => [
'profile_id' => true,
'main_gate' => true,
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => false
'[errors]' => [
]
'[invalid]' => [
]
'[repository]' => ''
},
(int) 1 => object(Cake\ORM\Entity) id:16 {
'profile_id' => (int) 5
'main_gate' => 'A'
'[new]' => true
'[accessible]' => [
'*' => true,
]
'[dirty]' => [
'profile_id' => true,
'main_gate' => true,
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => false
'[errors]' => [
]
'[invalid]' => [
]
'[repository]' => ''
},
(int) 2 => object(Cake\ORM\Entity) id:17 {
'profile_id' => (int) 5
'main_gate' => 'B'
'[new]' => true
'[accessible]' => [
'*' => true,
]
'[dirty]' => [
'profile_id' => true,
'main_gate' => true,
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => false
'[errors]' => [
]
'[invalid]' => [
]
'[repository]' => ''
},
(int) 3 => object(Cake\ORM\Entity) id:18 {
'profile_id' => (int) 6
'main_gate' => 'A'
'[new]' => true
'[accessible]' => [
'*' => true,
]
'[dirty]' => [
'profile_id' => true,
'main_gate' => true,
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => false
'[errors]' => [
]
'[invalid]' => [
]
'[repository]' => ''
},
(int) 4 => object(Cake\ORM\Entity) id:19 {
'profile_id' => (int) 6
'main_gate' => 'B'
'[new]' => true
'[accessible]' => [
'*' => true,
]
'[dirty]' => [
'profile_id' => true,
'main_gate' => true,
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => false
'[errors]' => [
]
'[invalid]' => [
]
'[repository]' => ''
},
(int) 5 => object(Cake\ORM\Entity) id:20 {
'profile_id' => (int) 7
'main_gate' => 'A'
'[new]' => true
'[accessible]' => [
'*' => true,
]
'[dirty]' => [
'profile_id' => true,
'main_gate' => true,
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => false
'[errors]' => [
]
'[invalid]' => [
]
'[repository]' => ''
},
(int) 6 => object(Cake\ORM\Entity) id:21 {
'profile_id' => (int) 7
'main_gate' => 'B'
'[new]' => true
'[accessible]' => [
'*' => true,
]
'[dirty]' => [
'profile_id' => true,
'main_gate' => true,
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => false
'[errors]' => [
]
'[invalid]' => [
]
'[repository]' => ''
},
]
'[new]' => true
'[accessible]' => [
'description' => true,
'created' => true,
'modified' => true,
'profiles' => true,
]
'[dirty]' => [
'description' => true,
'profiles' => true,
'_joinData' => true,
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => false
'[errors]' => [
]
'[invalid]' => [
]
'[repository]' => 'SourcingEvents'
}