2016-04-25 17 views
0

我在CakePHP中是新的,我有這樣的問題:在CakePHP中,如何檢索鏈接表信息

使用博客和bookmarkers教程,我做了一個應用程序來管理的文章和用戶。我已將用戶的文章表與user_id鏈接起來。當我得到我的文章時,我有創建它的user_id。但是,我想要檢索用戶的名稱而不是隻有id。

我讀到How to retrieve information when linking models through a table in CakePHP 3.0?這個問題,但我不明白如何在我自己的項目上做到這一點。

這是(要顯示我想要的信息)在ArticlesController我的「索引」功能:

/** 
* Index method 
* 
* @return \Cake\Network\Response|null 
*/ 
public function index() 
{ 
    $this->paginate = [ 
     'contain' => ['Users'] 
    ]; 
    $articles = $this->paginate($this->Articles); 


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

誰能幫助我?

非常感謝!

回答

2

在/Articles/index.ctp:

<?php foreach ($articles as $article): ?> 
    <!-- HTML code here --> 
    <?= h($article->users->name) ?> 
    <!-- HTML code here --> 
<?php endforeach; ?> 
+1

哦葉!那太簡單了! :-P非常感謝您的幫助! – wyllyjon