Reputation: 31
Dears,
I have 2 fields (solicitante and resolvedor) related to the ID field of the users table, how can I display both in the index.ctp? I use this code below, but I do not know how to differentiate the 2 fields, I put only one field because when I put the two, the information repeats itself
My index.ctp
<?= $chamado->has('user') ? $this->Html->link($chamado->user->nome, ['controller' => 'Users', 'action' => 'view', $chamado->user->id]) : '' ?>
My Controller
public function index()
{
$this->paginate = [
'contain' => ['Users']
];
$chamados = $this->paginate($this->Chamados);
$this->set(compact('chamados'));
$this->set('_serialize', ['chamados']);
}
My Model
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('chamados');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Users', [
'foreignKey' => 'solicitante',
'joinType' => 'INNER'
]);
}
follows the screen image:
Upvotes: 2
Views: 773
Reputation: 9398
you can differentiate this way:
$this->belongsTo('Solicitantes', [
'className' => 'Users'
'foreignKey' => 'solicitante',
'joinType' => 'INNER'
]);
$this->belongsTo('Resolvedores', [
'className' => 'Users'
'foreignKey' => 'resolvedor',
'joinType' => 'INNER'
]);
and in your view
<?= $chamado->has('solicitante') ? $this->Html->link($chamado->solicitante->nome, ['controller' => 'Users', 'action' => 'view', $chamado->solicitante->id]) : '' ?>
<?= $chamado->has('resolvedor') ? $this->Html->link($chamado->resolvedor->nome, ['controller' => 'Users', 'action' => 'view', $chamado->resolvedor->id]) : '' ?>
see the manual
https://book.cakephp.org/3.0/en/orm/associations.html#belongsto-associations
Upvotes: 3