I have a belongstomany between two tables (Users and Groups, jointable GroupsUsers) this table has these fields: user_id, group_id, net_id, main_gate, created and modified), how can I save the net_id and main_gate fields in this table?
When I try to save it it gives me this warning: Creating default object from empty value.
This is the code for add action in UsersController:
public function add()
{
$user = $this->Users->newEmptyEntity();
if ($this->request->is('post')) {
$image_file_name_url = $this->request->getData('image_file_name_url') ?? null;
$type = $image_file_name_url->getClientMediaType() ?? null;
if($type == "image/gif" || $type == "image/jpeg" || $type == "image/x-png" || empty($type)) {
$size = $image_file_name_url->getSize() ?? 0;
if($size < 2097152) {
$user = $this->Users->patchEntity($user, $this->request->getData(), ['associated' => ['Groups._joinData']]);
debug($user->groups);
if(!empty($this->request->getData('groups.0._joinData'))){
if(!empty($this->request->getData('groups.0._joinData.net_id'))) $user->groups[0[->_joinData->net_id = $this->request->getData('groups.0._joinData.net_id');
if(!empty($this->request->getData('groups.0._joinData.main_gate'))) $user->groups[0]->_joinData->main_gate = $this->request->getData('groups.0._joinData.main_gate');
}
if(!empty($image_file_name_url)){
$user->image_file_name_url = Router::url("/", true) . "upload/" . $this->modelClass . "/" . $image_file_name_url->getClientFilename();
$user->image_file_name = WWW_ROOT . 'upload' . DS . $this->modelClass . DS . $image_file_name_url->getClientFilename();
$user->image_file_name_filename = $image_file_name_url->getClientFilename();
}
if ($this->Users->save($user)) {
if(!empty($image_file_name_url)) $image_file_name_url->moveTo(WWW_ROOT . 'upload' . DS . $this->modelClass . DS . $image_file_name_url->getClientFilename());
$this->Flash->success(__('The user has been saved.'));
$id = $this->Auth->user('id') ?? null;
$action = (!empty($id)) ? 'index' : 'login';
return $this->redirect(['action' => $action]);
}
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}else{
$this->Flash->error(__('Debe seleccionar una image de 2 mb máximo.'));
}
}
}
$profiles = $this->Users->Profiles->find('list', ['limit' => 200])->all();
$groups = $this->Users->Groups->find('list', ['limit' => 200])->all();
$referrer = $this->Users->find('list', ['limit' => 200])->all();
$this->set(compact('user', 'profiles', 'referrer', 'groups'));
}
I retrieve the nets entity with this action:
public function listNet()
{
if ($this->request->is('ajax')) {
$this->autorender = false;
$group_id = $this->request->getData('group_id');
$nets = $this->Users->Groups->Nets->find('list')->where(['group_id' => $group_id]);
$html = '<option value="0">- Seleccione una unidad funcional -</option>\n';
foreach($nets as $key => $value) {
$html .= '<option value="' . $key . '">' . $value . '</option>\n';
}
return $this->response->withStringBody($html);
}
}
And this is the code for add view:
<?php
/**
* @var \App\View\AppView $this
* @var \App\Model\Entity\User $user
* @var \Cake\Collection\CollectionInterface|string[] $profiles
* @var \Cake\Collection\CollectionInterface|string[] $groups
*/
?>
<div class="row">
<aside class="column">
<div class="side-nav">
<h4 class="heading"><?= __('Acciones') ?></h4>
<?= $this->Html->link(__('Listar Usuarios'), ['action' => 'index'], ['class' => 'side-nav-item']) ?>
</div>
</aside>
<div class="column-responsive column-80">
<div class="users form content">
<?= $this->Form->create($user, ['type' => 'file']) ?>
<fieldset>
<legend><?= __('Agregar Usuario') ?></legend>
<?php
echo $this->Form->control('username', ['label' => 'Nombre de usuario']);
echo $this->Form->control('password', ['label' => 'Contraseña']);
echo $this->Form->control('email', ['label' => 'E-mail']);
echo $this->Form->control('firstname', ['label' => 'Nombre']);
echo $this->Form->control('lastname', ['label' => 'Apellido']);
echo $this->Form->control('profile_id', ['label' => 'Perfil', 'options' => $profiles, 'empty' => '-- Seleccione un perfil --', 'id' => 'profileId']);
echo $this->Form->control('referrer_id', ['label' => 'Referente', 'options' => $referrer, 'empty' => '-- Seleccione un referente --']);
echo $this->Form->control('image_file_name_url', ['label' => 'Foto de perfil', 'type' => 'file']);
echo $this->Form->control('active', ['label' => 'Activo']);
echo '<div id="selectGroup" style="display:none;">'.$this->Form->control('groups._ids', ['label' => 'Célula', 'options' => $groups, 'empty' => '-- Seleccione una célula --', 'id' => 'groupsId']).'</div>';
echo '<div id="selectNet" style="display:none;">'.$this->Form->control('groups.0._joinData.net_id', ['label' => 'Unidad funcional', 'id' => 'netId']).'</div>';
echo '<div id="selectGate" style="display:none;">'.$this->Form->control('groups.0._joinData.main_gate', ['label' => 'Nodo', 'options' => ['A' => 'A', 'B' => 'B'], 'empty' => '-- Seleccione un nodo --']).'</div>';
?>
</fieldset>
<?= $this->Form->button(__('Guardar')) ?>
<?= $this->Form->end() ?>
</div>
</div>
</div>
<script>
$(function(){
$('#profileId').change(function(){
profile_id=$(this).val();
switch (profile_id) {
case "1": $('#selectGroup').hide();$('#selectNet').hide();$('#selectGate').hide();
break;
case "2": $('#selectGroup').hide();$('#selectNet').hide();$('#selectGate').hide();
break;
case "3": $('#selectGroup').show();$('#selectNet').hide();$('#selectGate').hide();
$('#groupsId').prop('multiple', 'multiple');
break;
case "4": $('#selectGroup').show();$('#selectNet').show();$('#selectGate').hide();
$('#groupsId').prop('multiple', false);
break;
case "5": $('#selectGroup').show();$('#selectNet').show();$('#selectGate').show();
$('#groupsId').prop('multiple', false);
break;
case "6": $('#selectGroup').show();$('#selectNet').show();$('#selectGate').show();
$('#groupsId').prop('multiple', false);
break;
case "7": $('#selectGroup').show();$('#selectNet').show();$('#selectGate').show();
$('#groupsId').prop('multiple', false);
break;
}
})
})
$(function(){
$('#groupsId').change(function(){
$.ajax({
method:"POST",
url:"<?= $this->Url->build(['controller' => 'Users', 'action' => 'listNet']) ?>",
data:{
group_id:$(this).val()
},
success: function(data) {
$('#netId').html(data);
},
headers:{
'X-CSRF-Token':$('meta[name="csrfToken"]').attr('content')
}
})
})
})
</script>