2017-10-20 15 views
0

ディアーズ、CakePHPの3 - index.ctpの複数の関係同じテーブル表示

Iは、ユーザーテーブルのIDフィールドに関連する2つのフィールド(solicitanteとresolvedor)を有し、どのようindex.ctpの両方を表示することができ? 私は2つを入れたときに、情報が

マイindex.ctp

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

繰り返さので、私は以下のコードを使用しますが、私は2つのフィールドを区別する方法がわからない、私は一つのフィールドを置きます私のコントローラ

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

を見ます
関連する問題