2011-11-08 41 views
0

檢索字段我有以下三個數據庫表:如何從鏈接模型

Products 
######## 
id 
title 
artist_id 

Arists 
###### 
id 
profile 
person_id 

People 
###### 
id 
first_name 
last_name 

在我Product模型如何創建一個方法來一起退回產品title藝術家的first_name

我已經建立了以下模型關聯:

Product belongs to Artist 
Artist belongs to Person 

回答

0

中可容納肯定是這樣的去篩選相關記錄。確保將$ actsAs = array('Containable')添加到您的模型或app_model中。

然後,你可以做這樣的事情:

$this->Product->find('all', array(
    'contain' => array(
     'Artist' => array(
      'Person' => array(
       'id', 
       'first_name' 
      ) 
     ) 
    ) 
)); 
0

假設你已經設置在這些模型的關係,你只需要設置recursive

$this->Product->recursive = 2; 
print_r($this->Product->find('all')); 
+0

什麼是實際的語法只是呼應了名字? – freshest

+0

請參閱'print_r'查看路徑。如果你使用'$ products = $ this-> Product-> find('all')'我想它會出現在'$ products [0] ['Artist'] ['People'] ['first_name']'中。 – gustavotkg

+0

使用'$ products [0] ['Artist'] ['Person'] ['first_name']'工作,但是使用這種方法會拖出大量不必要的數據,有沒有更好的方法來做到這一點? – freshest