CakePHP Containable 오류 : 두 수준의 연결이 작동하지 않습니다.
-
12-12-2019 - |
문제
다음 쿼리를 수행합니다.
$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"
}
}
...
]
.
... 사용자에게 첨부 된 것으로 기대되는 동안 내가 지정한 필드가있는 작성자가 나타납니다.저자가 왜 나타나지 않는지 아십니까?나는 그 코드에 대한 두 가지 수준의 재귀 수준과 예상대로 작동하는 것으로 다른 것들이 포함되었다.
도움을 주셔서 감사합니다!
해결책
모델에 포함 가능한 동작이 첨부되어 있는지 확인하십시오.다음 줄이 포함되어야합니다.
$actsAs = array('Containable');
.
스님이 어려운 일반적인 오타 인 "Acts"에서 "s"를 생략하지 않았는지 확인하십시오.또한 다음과 같이 비행기에 수신 가능한 동작을 첨부 할 수도 있습니다.
$this->YourModel->Behaviors->attach('Containable');
$this->YourModel->find('all', array('contain' => array(...)));
.
은 "포함"이 아닌 배열 키로 "포함"을 입력하는지 확인하십시오.
다른 팁
'필드'키의 필드를 지정해야합니다.추가 find()
호출처럼 취급하십시오.
$this->find('all', array(
'contain' => array(
'User' => array(
'fields' => array('id','facebook_id','name','username'),
'Author' => array(
'fields' => 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
));
. $this->find('all',array(
'contain' => array(
'User' => array('id','facebook_id','name','username','Author' =>array( 'AuthorModelName'=>array('first_name','last_name','code','photo'))),
'Tab.code'
),
. 모든 외래 키는 필드 배열에 있어야합니다
'fields' => array('Comment.date','Comment.time','Comment.content', 'Comment.user_id', ....)
. 제휴하지 않습니다 StackOverflow