我的问题是当我调用$data时,我无法检索评论,它像这样返回,为什么评论不返回?
Array(
[0] => Array
(
[Event] => Array
(
[id] => 1
[url_id] => 22cbb58298af0fe62284324ccc020023
[name] => 高尾山ハイキング
[date] => 2017-05-06 00:00:00
[memo] => 行こうぜ
[is_available] => 1
[created] => 2017-05-03 21:15:30
[modified] => 2017-05-03 21:15:30
)
[Comment] => Array
(
)
))
顺便说一下,这是事件控制器和事件有很多评论 已经在模型上完成了一些设置
$data = $this->Event->find('all',[
'conditions' => [
'Event.url_id' => $eventURL
],
'contain' =>[
'Comment'=>[
'conditions' => ['Comment.event_id' =>$eventURL],
],
],
]);
您不必在contain
密钥中手动添加foreign_key。
如果您的模型设置正确,并且您在Comment
表中有Event
hasMany
记录,这应该会为您提供期望的内容。
$data = $this->Event->find('all', [
'conditions' => [
'Event.url_id' => $eventURL
],
'contain' =>[
'Comment'
]
]);
如果您的数据库中有一个event_id
为1
的Comment
,您应该得到它。
如果没有,请设置Configure::write('debug', 2)
并检查您的SQL调试输出,生成了什么查询。