title: Trying to save HABTM model data in CakePHP 2.0
body: <p>I have a data model that requires the use of HABTM, as follows:</p>
  
  <pre><code>surveys 
  (
      id int(11) NOT NULL AUTO_INCREMENT,
      user_id int(11) NOT NULL,
      title varchar(50) DEFAULT NULL,
      created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
      modified timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
      PRIMARY KEY (id),
      KEY user_id (user_id)
  );
  
  questions 
  (
      id int(11) NOT NULL AUTO_INCREMENT,
      user_id int(11) NOT NULL,
      title varchar(50) NOT NULL,
      body text NOT NULL,
      created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
      modified timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
      PRIMARY KEY (id),
      KEY user_id (user_id)
  );
  
  questions_surveys  
  (  
      id int(11) NOT NULL AUTO_INCREMENT,  
      survey_id int(11) NOT NULL,  
      question_id int(11) NOT NULL,  
      created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,  
      modified timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',  
      PRIMARY KEY (id),  
      KEY survey_id (survey_id),  
      KEY question_id (question_id)  
  );
  </code></pre>
  
  <p>And the relevant foreign keys:</p>
  
  <pre><code>ALTER TABLE questions_surveys
  ADD CONSTRAINT questions_surveys_ibfk_1 FOREIGN KEY(survey_id) REFERENCES surveys(id);
  
  ALTER TABLE questions_surveys
  ADD CONSTRAINT questions_surveys_ibfk_2 FOREIGN KEY(question_id) REFERENCES questions(id);
  </code></pre>
  
  <p>Questions and Surveys have an HABTM relationship, so that one survey has many questions and one question can be in many different surveys. </p>
  
  <p>In Survey.php: </p>
  
  <pre><code>public $hasAndBelongsToMany = array(
      'Question' =&gt; array(
          'className' =&gt; 'Question',
          'joinTable' =&gt; 'questions_surveys',
          'foreignKey' =&gt; 'survey_id',
          'associationForeignKey' =&gt; 'question_id'
      )
  );
  </code></pre>
  
  <p>In Question.php: </p>
  
  <pre><code>public $hasAndBelongsToMany = array(
      'Survey' =&gt; array(
          'className' =&gt; 'Survey',
          'joinTable' =&gt; 'questions_surveys',
          'foreignKey' =&gt; 'question_id',
          'associationForeignKey' =&gt; 'survey_id'
      )
  );
  </code></pre>
  
  <p>Here's my add controller from SurveysController.php: </p>
  
  <pre><code>public function add()
  {
      $this-&gt;set('fields', $this-&gt;Survey-&gt;getFields());
      $this-&gt;set('users', $this-&gt;Survey-&gt;User-&gt;find('list', array('fields' =&gt; array('id', 'username'))));
      $this-&gt;set('questions', $this-&gt;Question-&gt;find('list', array('fields' =&gt; array('id', 'body'))));
  
      if (!empty($this-&gt;data))
      {
          $this-&gt;Survey-&gt;saveAll($this-&gt;data['Survey']);
  
          foreach($this-&gt;data['Question']['id'] as $question_id)
          {
              $newdata[] = array('Survey' =&gt; array('id' =&gt; $this-&gt;Survey-&gt;getInsertID()), 'Question' =&gt; array('id' =&gt; $question_id));
          }
  
          if ($this-&gt;Survey-&gt;saveAll($newdata))
          {
              $this-&gt;Session-&gt;setFlash('The survey was successfully added!');
              $this-&gt;redirect(array('action'=&gt;'index'));
          }
          else
          {
              $this-&gt;Session-&gt;setFlash('Unable to add survey.');
          }
      }
  }
  </code></pre>
  
  <p>The new survey is saved first, and then every question_survey is added to an array that then adds all of them at once. The data looks like this:</p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [Survey] =&gt; Array
                  (
                      [id] =&gt; 17
                  )
  
              [Question] =&gt; Array
                  (
                      [id] =&gt; 1
                  )
  
          )
  
      [1] =&gt; Array
          (
              [Survey] =&gt; Array
                  (
                      [id] =&gt; 17
                  )
  
              [Question] =&gt; Array
                  (
                      [id] =&gt; 2
                  )
  
          )
  
      [2] =&gt; Array
          (
              [Survey] =&gt; Array
                  (
                      [id] =&gt; 17
                  )
  
              [Question] =&gt; Array
                  (
                      [id] =&gt; 3
                  )
  
          )
  
      [3] =&gt; Array
          (
              [Survey] =&gt; Array
                  (
                      [id] =&gt; 17
                  )
  
              [Question] =&gt; Array
                  (
                      [id] =&gt; 4
                  )
  
          )
  
      [4] =&gt; Array
          (
              [Survey] =&gt; Array
                  (
                      [id] =&gt; 17
                  )
  
              [Question] =&gt; Array
                  (
                      [id] =&gt; 5
                  )
  
          )
  
      [5] =&gt; Array
          (
              [Survey] =&gt; Array
                  (
                      [id] =&gt; 17
                  )
  
              [Question] =&gt; Array
                  (
                      [id] =&gt; 6
                  )
  
          )
  
      [6] =&gt; Array
          (
              [Survey] =&gt; Array
                  (
                      [id] =&gt; 17
                  )
  
              [Question] =&gt; Array
                  (
                      [id] =&gt; 7
                  )
  
          )
  
      [7] =&gt; Array
          (
              [Survey] =&gt; Array
                  (
                      [id] =&gt; 17
                  )
  
              [Question] =&gt; Array
                  (
                      [id] =&gt; 8
                  )
  
          )
  
      [8] =&gt; Array
          (
              [Survey] =&gt; Array
                  (
                      [id] =&gt; 17
                  )
  
              [Question] =&gt; Array
                  (
                      [id] =&gt; 9
                  )
  
          )
  
      [9] =&gt; Array
          (
              [Survey] =&gt; Array
                  (
                      [id] =&gt; 17
                  )
  
              [Question] =&gt; Array
                  (
                      [id] =&gt; 10
                  )
  
          )
  
  )
  </code></pre>
  
  <p>I keep getting this error:</p>
  
  <blockquote>
    <p>Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'QuestionsSurvey.survey_id' in 'where clause'<br>
       SQL Query: SELECT <code>AppModel</code>.<code>question_id</code> FROM <code>engage</code>.<code>questions_surveys</code> AS <code>AppModel</code> WHERE <code>QuestionsSurvey</code>.<code>survey_id</code> = 13</p>
  </blockquote>
  
  <p>As far as I can tell, everything is named in accordance with CakePHP standards, and I tried using 'with' => 'QuestionsSurvey', but get this error:</p>
  
  <blockquote>
    <p>Missing Database Table
    Error: Table app_models for model QuestionsSurvey was not found in datasource default.</p>
  </blockquote>
  
  <p>And 'with' => 'QuestionsSurveys', but get the same error:</p>
  
  <blockquote>
    <p>Missing Database Table
    Error: Table app_models for model QuestionsSurveys was not found in datasource default.</p>
  </blockquote>
  
  <p>I've tried converting the model trio to a hasMany through model (didn't work, it all just said go back to HABTM). </p>
  
  <p>I've used all kinds of different formats for the data <a href="http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveall-array-data-null-array-options-array" rel="nofollow">(CakePHP Saving Your Data)</a> but no luck there either. </p>
  
  <p>I'm stumped. Does anybody know what I'm doing wrong? Also, I apologize for the very long entry and code sections but I wanted to make sure to be thorough. </p>
  
  <p>Thank you for your time!<br>
  Matt</p>