title: Yii multi model with HAS_MANY Tabular form
body: <p>I have been trying to find a solution to something that I believe is rather simple?  What I would like to do is create a tabular form to collect some records for a survey.</p>
  
  <pre><code>Table: survey_record
  id
  meter_id
  status_id
  create_time
  create_user
  
  Table: survey_inspection
  id
  survey_record_id (FK)
  bay_id
  bay_usage_id
  </code></pre>
  
  <p>So table survey_inspection HAS_MANY inspections from survey_record.
  Assuming an inspector is doing a survey for meter_id = 1001, the user identifies the status_id of the meter then, they have to fill in 4 inspection entries, as 1001 has 4 bays.
  To generate the 4 bays I simply created a <em>for</em> loop.  Which works fine.</p>
  
  <p>View: _form.php</p>
  
  <pre><code>...
  $meter=Meter::model()-&gt;findByPk($model-&gt;meter_id);
  $bays = count($meter-&gt;bays); //retrieves the number of inspections required
  
  &lt;?php echo $form-&gt;dropDownListRow($model,'status_id',$model-&gt;getStatusId(),array('prompt'=&gt;'Select Status ...')); ?&gt;
  
  ...
  &lt;?php for ($i = 1; $i &lt;= $bays; $i++): ?&gt;
  
  &lt;?php echo $form-&gt;textFieldRow($inspection, "[$i]bay_id", array('readonly'=&gt;true, 'value'=&gt;"$i", 'class'=&gt;'span1')); ?&gt;
  
  &lt;?php echo $form-&gt;dropDownListRow($inspection, "[$i]bay_usage_id", $inspection-&gt;getUsageId(), array('prompt'=&gt;'Bay Status ...') ); ?&gt;   
  
  &lt;?php endfor; ?&gt;
  ...
  </code></pre>
  
  <p>However when I submit the form I am only receiving two (not four) results and I can't seem to display the validated fields correctly.  So here's the famous controller file:SurveyRecordController.php</p>
  
  <pre><code>public function actionCreate($survey_id, $meter_id)
  {
  
      $model=new SurveyRecord;
      $inspection = new SurveyInspection;
  
      $model-&gt;survey_id = (int)$survey_id;
      $model-&gt;meter_id  = (int)$meter_id;
  
  
      if(isset($_POST['SurveyRecord'], $_POST['SurveyInspection']))
      {
          $model-&gt;attributes=$_POST['SurveyRecord'];
          $valid= $model-&gt;validate();
  
              $i=1;
              foreach($_POST['SurveyInspection'][$i] as $inspection)
              {
                  $inspection = new SurveyInspection;
  
                  $inspection-&gt;bay_id     =$_POST['SurveyInspection'][$i]['bay_id'];
                  $inspection-&gt;bay_usage_id   =$_POST['SurveyInspection'][$i]['bay_usage_id']; 
  
  
                  $valid= $inspection-&gt;validate() &amp;&amp; $valid; 
  
                  echo '&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;pre&gt;';
                  print_r($_POST['SurveyInspection'][$i]);
                  echo '&lt;/pre&gt;';
  
                  if($valid)
                  {
                      if($model-&gt;save(false))
                      {
                          $inspection-&gt;survey_record_id = $model-&gt;id;
                          $inspection-&gt;save(false); 
                          $this-&gt;redirect(array('/meter')); 
                      }
                  }
                  $i++;
  
                  //$inspection-&gt;attributes=$_POST['SurveyInspection'][$i];
  
              }
  
  
      }
  
      $this-&gt;render('create',array(
          'model'=&gt;$model,
          'inspection'=&gt;$inspection,
      ));
  }
  </code></pre>
  
  <p>I have a funny feeling that I may not be doing the <em>foreach</em> loop correctly as when I view the array $_POST['SurveyInspection'][$i]  I am only returned with two entries when there should be four.</p>
  
  <p>Some information that may be helpful:</p>
  
  <pre>
  PHP version: 5.4.14
  Yii version: 1.1.13
  PostgreSQL version: 9.1.9
  </pre>
  
  <p>Thank you kindly :)</p>