I could finally save the net_id field in the joindata but now I can’t retreive the nets association with joindata, how do I have to do to solve this issue?
This is the code for index action:
public function index()
{
$this->paginate = [
'contain' => ['Profiles', 'Referrer', 'Groups']
];
$users = [];
$usersTable = new UsersTable();
if ($this->controllerUser['isAdmin'] || $this->controllerUser['isGeneralCoordinator']){
$users = $this->paginate($usersTable->find('all')->distinct(['Users.id'])->leftJoinWith('Groups')->where(['Users.profile_id IN' => [1,2,3,4,5,6,7]]));
} else if ($this->controllerUser['isGroupCoordinator']){
$users = $this->paginate($this->Users->find('search', ['search' => $this->request->getQuery()])->leftJoinWith('Groups')
->where(['Users.profile_id IN' => [3,4,5,6,7], 'Groups.id in' => $this->controllerUser['group_id']])->group(['Users.id']));
} else if ($this->controllerUser['isNetCoordinator']){
$users = $this->paginate($this->Users->find('search', ['search' => $this->request->getQuery()])->leftJoinWith('Groups')
->where(['Users.profile_id IN' => [4,5,6,7], 'Groups.id' => $this->controllerUser['group_id'],
'GroupsUsers.net_id' => $this->controllerUser['net_id']])->group(['Users.id']));
} else if ($this->controllerUser['isNodeCoordinator']){
$users = $this->paginate($this->Users->find('search', ['search' => $this->request->getQuery()]))->leftJoinWith('Groups')
->where(['Users.profile_id IN' => [4,5,6,7], 'Groups.id' => $this->controllerUser['group_id'], 'GroupsUsers.net_id' => $this->controllerUser['net_id'],
'GroupsUsers.main_gate' => $this->controllerUser['main_gate']]->group(['Users.id']));
} else if ($this->controllerUser['isTacticOperator']){
$users = $this->paginate($this->Users->find('search', ['search' => $this->request->getQuery()]))->leftJoinWith('Groups')
->where(['Users.profile_id IN' => [4,5,6,7], 'Groups.id' => $this->controllerUser['group_id'], 'GroupsUsers.net_id' => $this->controllerUser['net_id'],
'GroupsUsers.main_gate' => $this->controllerUser['main_gate']]->group(['Users.id']));
} else if ($this->controllerUser['isSanitaryAgent'] ){
$users = $this->paginate($this->Users->find('search', ['search' => $this->request->getQuery()]))->leftJoinWith('Groups')
->where(['Users.profile_id IN' => [4,5,6,7], 'Groups.id' => $this->controllerUser['group_id'], 'GroupsUsers.net_id' => $this->controllerUser['net_id'],
'GroupsUsers.main_gate' => $this->controllerUser['main_gate']]->group(['Users.id']));
}
$this->set(compact('users'));
$this->set('_serialize', ['users']);
}
This is the code for index view:
<?php
/**
* @var \App\View\AppView $this
* @var iterable<\App\Model\Entity\User> $users
*/
?>
<div class="users index content">
<?= $this->Html->link(__('New User'), ['action' => 'add'], ['class' => 'button float-right']) ?>
<h3><?= __('Users') ?></h3>
<div class="table-responsive">
<table>
<thead>
<tr>
<th><?= $this->Paginator->sort('image_file_name_url') ?></th>
<th><?= $this->Paginator->sort('username') ?></th>
<th><?= $this->Paginator->sort('email') ?></th>
<th><?= $this->Paginator->sort('firstname') ?></th>
<th><?= $this->Paginator->sort('lastname') ?></th>
<th><?= $this->Paginator->sort('created') ?></th>
<th><?= $this->Paginator->sort('modified') ?></th>
<th><?= $this->Paginator->sort('profile_id') ?></th>
<th><?= $this->Paginator->sort('group_id') ?></th>
<th><?= $this->Paginator->sort('net_id') ?></th>
<th><?= $this->Paginator->sort('main_gate') ?></th>
<th><?= $this->Paginator->sort('referrer_id') ?></th>
<th><?= $this->Paginator->sort('active') ?></th>
<th><?= $this->Paginator->sort('map_lat') ?></th>
<th><?= $this->Paginator->sort('map_long') ?></th>
<th><?= $this->Paginator->sort('code') ?></th>
<th><?= $this->Paginator->sort('status') ?></th>
<th><?= $this->Paginator->sort('isolated') ?></th>
<th class="actions"><?= __('Actions') ?></th>
</tr>
</thead>
<tbody>
<?php
$i=0;
foreach ($users as $user):
$group = "";
$net = "";
$gate = "";
if($user->has("groups")):
foreach ($user->groups as $groups):
if(!empty($group)) $group .= "\n";
$group .= $groups->group_name;
$net = $groups->_joinData->nets->net_name;
$gate = $groups->_joinData->main_gate;
endforeach;
endif;
?>
<tr>
<td><?= $user->image_file_name_url === null ? '' : $this->Html->image($user->image_file_name_url, ['class' => 'img-circle']) ?></td>
<td><?= h($user->username) ?></td>
<td><?= h($user->email) ?></td>
<td><?= h($user->firstname) ?></td>
<td><?= h($user->lastname) ?></td>
<td><?= h($user->created) ?></td>
<td><?= h($user->modified) ?></td>
<td><?= $user->has('profile') ? $this->Html->link($user->profile->name, ['controller' => 'Profiles', 'action' => 'view', $user->profile->id]) : '' ?></td>
<td><?= (!empty($group)) ? h($group) : '' ?></td>
<td><?= (!empty($net)) ? h($net) : '' ?></td>
<td><?= (!empty($gate)) ? h($gate) : '' ?></td>
<td><?= $user->has('referrer') ? $this->Html->link($user->referrer->full_name, ['controller' => 'Users', 'action' => 'view', $user->referrer->id]) : '' ?></td>
<td><?= h($user->active) ?></td>
<td><?= $user->map_lat === null ? '' : $this->Number->format($user->map_lat) ?></td>
<td><?= $user->map_long === null ? '' : $this->Number->format($user->map_long) ?></td>
<td><?= h($user->code) ?></td>
<td><?= $user->status === null ? '' : $this->Number->format($user->status) ?></td>
<td><?= $user->isolated === null ? '' : $this->Number->format($user->isolated) ?></td>
<td class="actions">
<?= $this->Html->link(__('Setear Georeferenciación'), ['action' => 'setgeo', $user->id]) ?>
<?= $this->Html->link(__('View'), ['action' => 'view', $user->id]) ?>
<?= $this->Html->link(__('Edit'), ['action' => 'edit', $user->id]) ?>
<?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $user->id], ['confirm' => __('Are you sure you want to delete {0}?', $user->full_name)]) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
And this is what it shows debug($groups->_joinData):
ROOT/templates/Users/index.php (line 44)
object(App\Model\Entity\GroupsUser) id:0 {
'user_id' => (int) 4
'group_id' => (int) 1
'net_id' => (int) 1
'main_gate' => ''
'created' => object(Cake\I18n\FrozenTime) id:1 {
'time' => '2023-03-30 21:00:26.000000-03:00'
'timezone' => 'America/Argentina/Cordoba'
'fixedNowTime' => false
}
'modified' => object(Cake\I18n\FrozenTime) id:2 {
'time' => '2023-03-30 21:00:26.000000-03:00'
'timezone' => 'America/Argentina/Cordoba'
'fixedNowTime' => false
}
'[new]' => false
'[accessible]' => [
'net_id' => true,
'main_gate' => true,
'created' => true,
'modified' => true,
'user' => true,
'group' => true,
'net' => true,
]
'[dirty]' => [
]
'[original]' => [
]
'[virtual]' => [
]
'[hasErrors]' => false
'[errors]' => [
]
'[invalid]' => [
]
'[repository]' => 'GroupsUsers'
}