我正在尝试在单个查询中返回四个模型(Location-> StatisticCategory-> StatisticItem-> Statistic)。结果是预期的,直到查询从Location级别开始。此时,数据返回包含StatisticItem中第3个关联的所有内容,但每个统计信息都为空。知道为什么第四级关联(至少在这里)没有返回任何数据吗?
下面是我在我的StatisticsController中使用的代码,在AppModel中启用了Containable行为:
$stats = $this->Location->find('all', array(
'fields' => array(
'Location.id',
'Location.location'
),
'conditions' => array(),
'recursive' => 2,
'contain' => array(
'StatisticCategory' => array(
'fields' => array(
'StatisticCategory.id',
'StatisticCategory.location_id',
'StatisticCategory.category'
),
'StatisticItem' => array(
'fields' => array(
'StatisticItem.id',
'StatisticItem.statistic_category_id',
'StatisticItem.item'
),
'Statistic' => array(
'fields' => array(
'Statistic.id',
'Statistic.statistic_item_id',
'Statistic.date',
'Statistic.number'
),
'conditions' => array(
'Statistic.date' => $date
)
),
'order' => array('StatisticItem.item')
),
'order' => array('StatisticCategory.category')
)
),
'order' => array('Location.location')
));
答案 0 :(得分:0)
可包含文档描述了如何contain deeper associations。以下应该是您正在寻找的:
$this->Location->find('all', array(
'contain' => array(
'StatisticCategory' => array(
'StatisticItem' => array(
'Statistic'
)
)
)
));