0

I have four models (Location->StatisticCategory->StatisticItem->Statistic) that I'm trying to return in a single query. The results are as expected until the query starts at the level of Location. At that point the data return contains everything down to the 3rd association in StatisticItem, but every single Statistic is null. Any idea why 4th level associations (at least here) are not returning any data?

Below is the code I'm using in my StatisticsController with Containable behavior turned on in AppModel:

$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')
    ));
jRoB
  • 338
  • 1
  • 9

1 Answers1

0

The documentation for containable describes how to contain deeper associations. The following should be about what you're looking for:

$this->Location->find('all', array(
    'contain' => array(
        'StatisticCategory' => array(
            'StatisticItem' => array(
                'Statistic'
            )
        )
    )
));
Brad Koch
  • 19,267
  • 19
  • 110
  • 137
  • I had modified the code to be more readable for this site, but let me edit my post with what I have been using. (It still doesn't work). – jRoB Dec 22 '12 at 03:11
  • While not the exact answer, you did help me plug away in a new direction that ultimately solved it. Thanks Brad! – jRoB Dec 22 '12 at 03:25