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
4

2 回答 2

0

Containable 绝对是过滤相关记录的方法。确保将 $actsAs = array('Containable') 添加到您的模型或 app_model 中。

然后您可以执行以下操作:

$this->Product->find('all', array(
    'contain' => array(
        'Artist' => array(
            'Person' => array(
                'id',
                'first_name'
            )
        )
    )
));
于 2011-11-08T23:15:04.490 回答
0

假设您已经在这些模型中设置了关系,您只需要设置它recursive

$this->Product->recursive = 2;
print_r($this->Product->find('all'));
于 2011-11-08T13:35:04.657 回答