I make a join with three tables but when I see the array it doesn’t have the belongsTo arrays.
This is the code of the join:
$options['joins'] = array(array('table' => 'userx_groups','alias' => 'UserxGroup','type' => 'LEFT','foreignKey' => false,'conditions' => array('User.id = UserxGroup.user_id')),array('table' => 'groups','alias' => 'Group','type' => 'LEFT','foreignKey' => false,'conditions' => array('Group.id = UserxGroup.group_id')),array('table' => 'nets','alias' => 'Net','type' => 'LEFT','foreignKey' => false,'conditions' => array('Net.id = UserxGroup.net_id')));
And when I see the array it shows me this:
Array ( [id] => 11 [user_id] => 42 [created] => 2020-04-20 11:46:24 [modified] => 2020-05-05 10:11:20 [net_id] => [group_id] => 2 [main_gate] => )
This is the code for the view of users:
<h1>Recursos humanos</h1>
<?php echo $this->Html->link('Agregar recurso humano',array('controller'=>'Users','action'=>'add'),array('class'=>'btn btn-success')); ?>
<br/><br/>
<table class='table table-striped table-bordered table-hover table-sortable'>
<thead>
<tr>
<th>Nivel</th>
<th>Foto</th>
<th>Email</th>
<th>Nombre</th>
<th>Apellido</th>
<th>Rol</th>
<th>Celula</th>
<th>UF</th>
<th>Nodo</th>
<th>Refiere</th>
<th>Estado</th>
<th>Aislado</th>
<th style="min-width: 150px">Acciones</th>
</tr>
</thead>
<!-- Here is where we loop through our $posts array, printing out post info -->
<tbody>
<?php
foreach ($model as $User):
$color = '';
switch ($User['User']['profile_id']) {
case '99':
$color = '#000000';
break;
case '1':
$color = '#041d54';
break;
case '2':
$color = '#540404';
break;
case '3':
$color = '#6D147A';
break;
case '4':
$color = '#CC7400';
break;
case '5':
$color = '#2E2EBF';
break;
default:
$color = '#30773D';
break;
}
$colorAlert = '';
switch($User['User']['status']){
case 1: $colorAlert = '#18FE00';
break;
case 2: $colorAlert = '#FFFF00';
break;
case 3: $colorAlert = '#FF8000';
break;
case 4: $colorAlert = '#FF0000';
break;
default: $colorAlert = '#FFFFFF';
break;
}
$group = '';
$net = '';
$main_gate = '';
if($User['User']['profile_id'] >= 2){
foreach($User['UserxGroup'] as $userxGroup):
if(!empty($group)) $group .= '<br />';
$group .= $userxGroup['Group']['name'];
if($User['User']['profile_id'] >= 3) $net = $userxGroup['Net']['name'];
$main_gate = $userxGroup['main_gate'];
endforeach;
}
?>
<tr <?php if ($User['User']['active'] != true) echo 'style=\'background-color:#eaeaea;\'' ?>>
<td><?php echo $User['User']['profile_id']; ?></td>
<td><?php echo $this->Upload->uploadImage($User, 'User.image',
array('style' => 'thumb'),
array('class' => 'img-circle', 'style'=>"width: 60px")); ?></td>
<td><?php echo $User['User']['email']; ?></td>
<td><?php echo $User['User']['firstname']; ?></td>
<td><?php echo $User['User']['lastname']; ?></td>
<td style="background-color:<?php echo $color; ?>;color:#fff"><?php echo $User['Profile']['name']; ?></td>
<td><?php echo $group; ?></td>
<td><?php echo $net; ?></td>
<td><?php if ($User['User']['profile_id'] > 3) { echo '#';echo $main_gate; } else {echo ' - ';} ?></td>
<td><?php echo $User['Referrer']['firstname']; ?> <?php echo $User['Referrer']['lastname']; ?></td>
<td style="background-color:<?php echo $colorAlert; ?>;"><?php echo $statusAlertEnum[$User['User']['status']]; ?></td>
<td><?php echo $User['User']['isolated'] ? 'Sí' : 'No'; ?></td>
<td>
<?php if ($User['User']['profile_id'] > 2) {
echo $this->Html->link('<span class=\'glyphicon glyphicon-globe\'></span>',array('action' => 'setgeo', $User['User']['id']),array('escape' => false,'class' => 'btn btn-info','title' => 'Setear Georeferenciación') // This line will parse rather then output HTML
);
}?>
<?php echo $this->Html->link('<span class=\'glyphicon glyphicon-pencil\'></span>',array('action' => 'edit', $User['User']['id']),array('escape' => false,'class' => 'btn btn-primary','title' => 'Editar') // This line will parse rather then output HTML
);
?>
<?php /*echo $this->Html->link('<span class=\'glyphicon glyphicon-pencil\'></span> Cambiar contraseña',array('action' => 'changeuserpwd', $User['User']['id']),array('escape' => false,'class' => 'btn btn-primary','title' => 'Editar contraseña') // This line will parse rather then output HTML
);*/
?>
<?php echo $this->Form->postLink('<span class=\'glyphicon glyphicon-remove\'></span>',array('action' => 'delete', $User['User']['id']),array('confirm' => 'Esta seguro?','escape' => false,'class' => 'btn btn-danger','title' => 'Eliminar')
);
?>
</td>
</tr>
<?php
endforeach;
?>
<?php unset($User); ?>
</tbody>
</table>
And this is the code of the controller:
$options['joins'] = array(array('table' => 'userx_groups','alias' => 'UserxGroup','type' => 'LEFT','foreignKey' => false,'conditions' => array('User.id = UserxGroup.user_id')),array('table' => 'groups','alias' => 'Group','type' => 'LEFT','foreignKey' => false,'conditions' => array('Group.id = UserxGroup.group_id')),array('table' => 'nets','alias' => 'Net','type' => 'LEFT','foreignKey' => false,'conditions' => array('Net.id = UserxGroup.net_id')));
$options['conditions'] = array('User.profile_id IN' => array(1,2,3,4,5,6));
$users = $this->User->find('all',$options);
$this->set('model',$users);
This is the code of model UserxGroup:
<?php
class UserxGroup extends AppModel {
var $name = 'UserxGroup';
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
),
'Net' => array(
'className' => 'Net',
'foreignKey' => 'net_id'
),
'Group' => array(
'className' => 'Group',
'foreignKey' => 'group_id'
)
);
}
?>
What should I do to have the belongsTo arrays?