我有以下三个数据库表:
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
我有以下三个数据库表:
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
Containable 绝对是过滤相关记录的方法。确保将 $actsAs = array('Containable') 添加到您的模型或 app_model 中。
然后您可以执行以下操作:
$this->Product->find('all', array(
'contain' => array(
'Artist' => array(
'Person' => array(
'id',
'first_name'
)
)
)
));
假设您已经在这些模型中设置了关系,您只需要设置它recursive:
$this->Product->recursive = 2;
print_r($this->Product->find('all'));