我有四个模型(Location->StatisticCategory->StatisticItem->Statistic),我试图在单个查询中返回它们。结果与预期一致,直到查询在 Location 级别开始。此时,数据返回包含 StatisticItem 中直到第三个关联的所有内容,但每个 Statistic 都为空。知道为什么第 4 级关联(至少在这里)没有返回任何数据吗?
下面是我在我的 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')
));