title: Yii2 Pass Dynamic Model to View
body: <p>I have a Create Payslip page and I want to pass a dynamic model since this page/view requires more than one earning items. </p>
  
  <p>My <code>actionCreateNew</code> in my <strong>controller</strong>:</p>
  
  <pre><code>$session = Yii::$app-&gt;session;  
  $model = new Payslip();
  $model-&gt;user_id = $id;
  $model1 = new EarningDetails();
  $model2 = new DeductionDetails();
  $items = PayrollItems::find()-&gt;where(['store_id' =&gt; $id])-&gt;andwhere(['active' =&gt; 1])-&gt;andwhere(['payroll_type' =&gt; 'Earnings'])-&gt;all();
  $count = count($items);
  for($i = 1; $i &lt;= $count; $i++){
      $earning[$i] = new EarningDetails();
      $earning[$i]-&gt;earning_item_id = $items[$i];
  }
  </code></pre>
  
  <p>and here's my <code>return</code> statement in the same action controller:</p>
  
  <pre><code>return $this-&gt;render('create', [
      'model' =&gt; $model,
      'model1' =&gt; $model1,
      'model2' =&gt; $model2,
      'items' =&gt; $items, 
  ]);
  </code></pre>
  
  <p>and in my <strong>view</strong>:</p>
  
  <pre><code>&lt;?php 
      $earnings = PayrollItems::find()-&gt;where(['store_id' =&gt; $session['store_id']])-&gt;orwhere(['store_id' =&gt; null])-&gt;where(['payroll_type' =&gt; 'Earnings'])-&gt;all();
      //$earningslistData = ArrayHelper::map($earnings,'payroll_items_id', 'payroll_name');
      $earningslistData = ArrayHelper::map($earnings,'payroll_items_id',function($items, $defaultValue) {
          return $items-&gt;payroll_name;
      });
      for($i = 1; $i &lt;= $count; $i++) {
          echo $form-&gt;field($earning[$i], 'earning_item_id')-&gt;widget(Select2::classname(), [
              'data' =&gt; $earningslistData,
              'options' =&gt; ['placeholder' =&gt; 'Select an Earning ', 'id' =&gt; 'earning_item'],
              'pluginOptions' =&gt; [
                  'allowClear' =&gt; true
              ],
          ]);
      }
  ?&gt;
  </code></pre>
  
  <p>Problem is, how do I return a dynamic model? So I could use it in my view. In my return statement above, there's no <code>$earning</code> since it's a dynamic model and I don't know to return it.</p>
  
  <p>If you have any other implementation about dynamic models, please pleas let me know.</p>