title: Utilising Yii2.0 checkboxlist for Parent-child relation models
body: <p>i have a Questions model and Answers model in which Question to Answer is one-many relation. How do i implement checkboxlist in Yii2 so that it will display in a form as follows：</p>
  
  <pre><code>Form: 
  
  Question 1 description
       [checkbox] Answer 1 description
       [checkbox] Answer 2 description
  
  Question 2 Description
       [checkbox] Answer 3 description
       [checkbox] Answer 4 description
  
  Question 3 Description
       [checkbox] Answer 5 description
       [checkbox] Answer 6 description
  
  [Save button]
  </code></pre>
  
  <p>When i click save and for eg. Answer 1 and Answer 3 is checked, post should return "1,3" in a string. How do i do that using checkboxlist? I tried with my codes but right now it only captures values of Answer 5 and Answer 6 if they are checked. If Answer 1 to Answer 4 is checked, it they are not captured. Should be something wrong with my loop.</p>
  
  <p>Hope someone can help me please.. 
  Thanks alot</p>
  
  <p>Updated:I restructured my codes but still it only captures values from the answers belonging to last question:</p>
  
  <pre><code>&lt;?php
  $form = ActiveForm::begin([
      'id' =&gt; 'form',
      'type' =&gt; ActiveForm::TYPE_HORIZONTAL,
  
  ]);
  ?&gt;
  
  &lt;?php
  
  $qnamodel = $questionmodel-&gt;joinWith('answermodel')-&gt;all();
  $selected_array = array_map('intval', explode(',', $selected_list));    
  foreach($qnamodel as $num=&gt;$per_qn)
  {
  
          echo '&lt;div class="form-group"&gt;&lt;div class="col-sm-9"&gt;'.$per_qn-&gt;QN_DESC.'&lt;/div&gt;&lt;/div&gt;';
  
          foreach($per_qn-&gt;answermodel as $per_ans)
          {
  
              $arr_list[$per_qn-&gt;QA_TABLE_ID][$per_ans-&gt;QA_ANS_TABLE_ID] = $per_ans-&gt;ANS_DESC;
          }
  
  
          $recordmodel-&gt;ANS_TABLE_ID = $selected_array;
  
      echo $form-&gt;field($recordmodel, 'ANS_TABLE_ID', ['template' =&gt; "{label}\n{input}\n{hint}"])-&gt;checkboxList($arr_list[$per_qn-&gt;QA_TABLE_ID], $selected_array, ['separator' =&gt; '&lt;p&gt;'])-&gt;label(false);
  
  
  echo '&lt;p&gt;&lt;br /&gt;';
  }
  
  ?&gt;
  
  &lt;?= Html::submitButton('save', ['class' =&gt; 'btn btn-primary']) ?&gt;
  &lt;?phpgmst
  ActiveForm::end();
  ?&gt;
  
  
  In Question Model
      public function getAnswermodels()
      {
          return $this-&gt;hasMany(Answermodel::className(), ['QN_TABLE_ID' =&gt; 'QA_TABLE_ID']);
      }
  
  In Answer Model
      public function getQNTABLE()
      {
          return $this-&gt;hasOne(Questionmodel::className(), ['QA_TABLE_ID' =&gt; 'QN_TABLE_ID']);
      }
  </code></pre>