我有以下数据库表:
products
********
id
title
artist_id
artists
*******
id
profile
rating
person_id
people
******
id
full_name
使用以下模型关联:
Product Model
*************
public $belongsTo = array(
'Artist' => array(
'className' => 'Artist'
'foreignKey' => 'artist_id'
)
);
Artist Model
************
public $belongsTo = array(
'Person' => array(
'className' => 'Person'
'foreignKey' => 'person_id'
)
);
public $hasMany = array(
'Product' => array(
'className' => 'Product'
'foreignKey' => 'product_id'
)
);
Person Model
************
public $hasOne = array(
'Artist' => array(
'className' => 'Artist'
'foreignKey' => 'person_id'
)
);
我已使用以下方法将三个模型中的每一个设置为使用包含:
public $actsAs = array('Containable');
当我使用以下内容获取产品详细信息以及艺术家姓名时:
$this->Product->find('first', array('conditions' => array('Product.id' => $id), 'contain' => 'Person.full_name'))
我收到警告:
Model "Product" is not associated with model "Person"
我只收到产品详细信息,即没有艺术家的名字。为什么会这样?
答案 0 :(得分:2)
这种情况正在发生,因为您在find
模型上调用了Product
操作,并且Person
与Product
不是直接,但间接地,它与Artist
的关系。在这种情况下,您的contain
数组应类似于以下内容:
$this->Product->find('first', array(
'conditions' => array('Product.id' => $id),
'contain' => array('Artist' => 'Person.full_name')
));
这将返回类似
的结果array(
[Product] => array(...),
[Artist] => array(
[Person] => array(
'full_name' => '...'
)
)
)