2016-04-26 26 views
0

我在CakePHP的模型:CakePHP的從表中獲取數據類belongs_to的

class PagesTable extends Table 
{ 
    public function initialize(array $config) 
{ 
    $this->addBehavior('Timestamp'); 

    $this->belongsTo('Pagecats'); 

    $this->belongsToMany('Users'); 
} 

和另一種模式PagecatsTable:

class PagecatsTable extends Table 
{ 
public function initialize(array $config) 
{ 
    $this->addBehavior('Timestamp'); 
} 

它的表有兩個coloumns idcategory

如何在我的PagesController中的函數中獲得category

回答

0

在你的PagesController中,你想要頁面及其關聯的記錄嗎?如果是這樣,你可以嘗試下面提到的代碼:

// PagesController.php 

$data = $this->Pages->find() 
    ->contain('Pagecats') 
    ->all(); 

pr($data->toArray()); // Check if you're getting the desired result. 
+0

'Pagecats'有一個'類別'coloumn。我想要它的價值,爲我目前的頁面 –