2017-10-20 122 views
0

逗人,cakephp的3 - 在index.ctp數倍的關係相同的表顯示

我有2名相關的用戶表的ID字段的字段(solicitante和resolvedor),我怎樣才能在index.ctp同時顯示? 我用下面這段代碼,但我不知道如何區分2場,我把只有一個字段,因爲當我把二,信息重演

我index.ctp

<?= $chamado->has('user') ? $this->Html->link($chamado->user->nome, ['controller' => 'Users', 'action' => 'view', $chamado->user->id]) : '' ?> 

我的控制器

public function index() 
{ 

    $this->paginate = [ 
     'contain' => ['Users'] 
    ]; 

    $chamados = $this->paginate($this->Chamados); 


    $this->set(compact('chamados')); 
    $this->set('_serialize', ['chamados']); 
} 

我的模型

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' 
    ]); 


} 

跟隨S creen圖像:

Index.ctp screen

回答

0

可以區分這種方式:

$this->belongsTo('Solicitantes', [ 
    'className' => 'Users' 
    'foreignKey' => 'solicitante', 
    'joinType' => 'INNER' 
]); 

$this->belongsTo('Resolvedores', [ 
    'className' => 'Users' 
    'foreignKey' => 'resolvedor', 
    'joinType' => 'INNER' 
]); 

,並在您的視圖

<?= $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]) : '' ?> 

請參閱手冊

https://book.cakephp.org/3.0/en/orm/associations.html#belongsto-associations