我做下一个查询:
$this->find('all',array(
'contain' => array(
'User' => array('id','facebook_id','name','username','Author' => array('first_name','last_name','code','photo')),
'Tab.code'
),
'fields' => array('Comment.date','Comment.time','Comment.content'),
'conditions' => array(
'Tab.code' => $code
),
'limit' => $limit,
'offset' => $offset
));
评论...
public $belongsTo = array(
'User' => array('foreignKey' => 'user_id'),
'Tab' => array('foreignKey' => 'tab_id')
);
用户...
public $hasOne = array('Author' => array('foreignKey' => 'id'));
和作者...
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'id'
)
);
这会返回类似这样的内容(在 json 中)...
[
{
"Comment": {
"date": "2001-12-15",
"time": "17:32:12",
"content": "..."
},
"User": {
"id": "29",
"facebook_id": "1234",
"name": "User 29",
"username": "user29"
},
"Tab": {
"code": "llibre-de-prova-29",
"id": "229"
}
}
...
]
...虽然我希望附加到用户出现一个带有我指定的字段的作者。你知道为什么作者没有出现吗?我在代码上做了其他包含两个级别的递归,并且所有工作都按预期工作。
感谢您的帮助和关注!