title: Multi-level Model associations in CakePHP
body: <p>I am building an eLearning site in CakePHP that has models:  Course, Test, Question, Answer, and others.</p>
  
  <p>Here are my associations:</p>
  
  <ul>
  <li>Course hasMany Test</li>
  <li>Test belongsTo Course</li>
  <li>Question belongsTo Test</li>
  <li>Question hasMany Answer</li>
  <li>Answer belongsTo Question</li>
  </ul>
  
  <p>This model association works fine in most circumstances.  For example, the baked CRUD actions for Course know that there are Test(s) that belong to it, Questions know they have associated Answer(s), etc...  </p>
  
  <p>But I am trying to write a Test management tool that operates within the TestsController and level of association ends at Question.  There are no answers.  In other words, the view I'm working on has this by default from the baked TestsController:</p>
  
  <pre><code>Array
  (
  [Test] =&gt; Array
      (
          [id] =&gt; 1
          [name] =&gt; Test 1
          [course_id] =&gt; 1
          [time_limit] =&gt; 30
          [max_questions] =&gt; 20
          [randomize_order] =&gt; 1
          [num_questions] =&gt; 2
      )
  
  [Course] =&gt; Array
      (
          [id] =&gt; 1
          [course_identifier] =&gt; TLE1001
          [title] =&gt; Child Development I
          [description] =&gt; 
      )
  
  [Question] =&gt; Array
      (
          [0] =&gt; Array
              (
                  [id] =&gt; 1
                  [text] =&gt; What is your name?
                  [test_id] =&gt; 1
                  [order] =&gt; 0
              )
  
          [1] =&gt; Array
              (
                  [id] =&gt; 2
                  [text] =&gt; What is my name?
                  [test_id] =&gt; 1
                  [order] =&gt; 0
              )
  
      )
  
  )
  </code></pre>
  
  <p>As you can see, the $test array in my view stops at Question, and doesn't pull the associated Answer(s) for the Question(s).</p>
  
  <p>I fixed this problem by changing the bottom of my 'view' action in TestsController:</p>
  
  <pre><code>$test = $this-&gt;Test-&gt;read(null, $id);
  $test['Question'] = $this-&gt;Test-&gt;Question-&gt;find('all'); //I added this line...
  $this-&gt;set('test', $test);
  </code></pre>
  
  <p>I added the middle line so it literally replaces all the contents of $test['Question'] manually before it sets the $test array for the view to use.</p>
  
  <p>Here is the result:</p>
  
  <pre><code>Array
  (
  [Test] =&gt; Array
      (
          [id] =&gt; 1
          [name] =&gt; Test 1
          [course_id] =&gt; 1
          [time_limit] =&gt; 30
          [max_questions] =&gt; 20
          [randomize_order] =&gt; 1
          [num_questions] =&gt; 2
      )
  
  [Course] =&gt; Array
      (
          [id] =&gt; 1
          [course_identifier] =&gt; TLE1001
          [title] =&gt; Child Development I
          [description] =&gt; The first years of life offer a window of opportunity for a child’s development. A solid understanding of how children grow and develop is essential to providing a quality care environment.
      )
  
  [Question] =&gt; Array
      (
          [0] =&gt; Array
              (
                  [Question] =&gt; Array
                      (
                          [id] =&gt; 1
                          [text] =&gt; What is your name?
                          [test_id] =&gt; 1
                          [order] =&gt; 0
                      )
  
                  [Test] =&gt; Array
                      (
                          [id] =&gt; 1
                          [name] =&gt; Test 1
                          [course_id] =&gt; 1
                          [time_limit] =&gt; 30
                          [max_questions] =&gt; 20
                          [randomize_order] =&gt; 1
                          [num_questions] =&gt; 2
                      )
  
                  [Answer] =&gt; Array
                      (
                          [0] =&gt; Array
                              (
                                  [id] =&gt; 1
                                  [question_id] =&gt; 1
                                  [text] =&gt; My name is what I say it is.
                                  [correct] =&gt; 1
                              )
  
                          [1] =&gt; Array
                              (
                                  [id] =&gt; 2
                                  [question_id] =&gt; 1
                                  [text] =&gt; My name is Earl.
                                  [correct] =&gt; 
                              )
  
                      )
  
              )
  
          [1] =&gt; Array
              (
                  [Question] =&gt; Array
                      (
                          [id] =&gt; 2
                          [text] =&gt; What is my name?
                          [test_id] =&gt; 1
                          [order] =&gt; 0
                      )
  
                  [Test] =&gt; Array
                      (
                          [id] =&gt; 1
                          [name] =&gt; Test 1
                          [course_id] =&gt; 1
                          [time_limit] =&gt; 30
                          [max_questions] =&gt; 20
                          [randomize_order] =&gt; 1
                          [num_questions] =&gt; 2
                      )
  
                  [Answer] =&gt; Array
                      (
                          [0] =&gt; Array
                              (
                                  [id] =&gt; 3
                                  [question_id] =&gt; 2
                                  [text] =&gt; None of your business
                                  [correct] =&gt; 1
                              )
  
                          [1] =&gt; Array
                              (
                                  [id] =&gt; 4
                                  [question_id] =&gt; 2
                                  [text] =&gt; Jim
                                  [correct] =&gt; 
                              )
  
                      )
  
              )
  
      )
  
  )
  </code></pre>
  
  <p>That works for what I have to accomplish, but it seems ugly to me.  As you can see, the Answer(s) get loaded but I also have duplicate info now because when it loads the Question Model it pulls in the associated Test.  And I'm pretty sure this doesn't jive with CakePHP conventions...</p>
  
  <p>Is there a better way to do this?  Does CakePHP only build associated data array's for model association up to a certain level then stop?</p>
  
  <p>P.S. I tried to add:</p>
  
  <p>var $uses = array('Test', 'Question', 'Answer');</p>
  
  <p>...to the top of my TestsController but it did not fix the problem and seemed to have no effect.</p>
  
  <p>There must be a better way to get CakePHP to load ALL the associated data models AND THEIR associations as well...</p>