Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The equivalent "batchCreate" method of the <a href="http://www.yiiframework.com/doc/guide/1.1/en/form.table" rel="noreferrer" title="http://www.yiiframework.com/doc/guide/1.1/en/form.table">"batchUpdate" from the guide</a> could be something like this:</p> <pre class="lang-php prettyprint-override"><code>public function actionBatchCreate() { $models=array(); // since you know how many models $i=0; while($i&lt;5) { $models[]=Modelname::model(); // you can also allocate memory for the model with `new Modelname` instead // of assigning the static model } if (isset($_POST['Modelname'])) { $valid=true; foreach ($_POST['Modelname'] as $j=&gt;$model) { if (isset($_POST['Modelname'][$j])) { $models[$j]=new Modelname; // if you had static model only $models[$j]-&gt;attributes=$model; $valid=$models[$j]-&gt;validate() &amp;&amp; $valid; } } if ($valid) { $i=0; while (isset($models[$i])) { $models[$i++]-&gt;save(false);// models have already been validated } // anything else that you want to do, for example a redirect to admin page $this-&gt;redirect(array('modelname/admin')); } } $this-&gt;render('batch-create-form',array('models'=&gt;$models)); } </code></pre> <p>The only concern here is that a new instance <strong>has</strong> to be created for each model that you are saving, using <code>new</code>. The rest of the logic can be implemented in any way you wish, for example in the above example all the models are being validated, and then saved, whereas you could have stopped validation if any model was invalid, or maybe directly saved the models, letting validation happen during <code>save</code> call. So the logic will really depend on your app's ux.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload