Note that there are some explanatory texts on larger screens.

plurals
  1. POManipulating form input values after submission causes multiple instances
    primarykey
    data
    text
    <p>I'm building a form with Yii that updates two models at once.<br> The form takes the inputs for each model as $modelA and $modelB and then handles them separately as described here <a href="http://www.yiiframework.com/wiki/19/how-to-use-a-single-form-to-collect-data-for-two-or-more-models/" rel="nofollow">http://www.yiiframework.com/wiki/19/how-to-use-a-single-form-to-collect-data-for-two-or-more-models/</a></p> <p>This is all good. The difference I have to the example is that $modelA (documents) has to be saved and its ID retrieved and then $modelB has to be saved including the ID from $model A as they are related.</p> <p>There's an additional twist that $modelB has a file which needs to be saved.</p> <p>My action code is as follows:</p> <pre><code>if(isset($_POST['Documents'], $_POST['DocumentVersions'])) { $modelA-&gt;attributes=$_POST['Documents']; $modelB-&gt;attributes=$_POST['DocumentVersions']; $valid=$modelA-&gt;validate(); $valid=$modelB-&gt;validate() &amp;&amp; $valid; if($valid) { $modelA-&gt;save(false); // don't validate as we validated above. $newdoc = $modelA-&gt;primaryKey; // get the ID of the document just created $modelB-&gt;document_id = $newdoc; // set the Document_id of the DocumentVersions to be $newdoc // todo: set the filename to some long hash $modelB-&gt;file=CUploadedFile::getInstance($modelB,'file'); // finish set filename $modelB-&gt;save(false); if($modelB-&gt;save()) { $modelB-&gt;file-&gt;saveAs(Yii::getPathOfAlias('webroot').'/uploads/'.$modelB-&gt;file); } $this-&gt;redirect(array('projects/myprojects','id'=&gt;$_POST['project_id'])); } } ELSE { $this-&gt;render('create',array( 'modelA'=&gt;$modelA, 'modelB'=&gt;$modelB, 'parent'=&gt;$id, 'userid'=&gt;$userid, 'categories'=&gt;$categoriesList )); } </code></pre> <p>You can see that I push the new values for 'file' and 'document_id' into $modelB. What this all works no problem, but... each time I push one of these values into $modelB I seem to get an new instance of $modelA. So the net result, I get 3 new documents, and 1 new version. The new version is all linked up correctly, but the other two documents are just straight duplicates.<br> I've tested removing the $modelB update steps, and sure enough, for each one removed a copy of $modelA is removed (or at least the resulting database entry).<br> I've no idea how to prevent this. </p> <p>UPDATE....</p> <p>As I put in a comment below, further testing shows the number of instances of $modelA depends on how many times the form has been submitted. Even if other pages/views are accessed in the meantime, if the form is resubmitted within a short period of time, each time I get an extra entry in the database. If this was due to some form of persistence, then I'd expect to get an extra copy of the PREVIOUS model, not multiples of the current one. So I suspect something in the way its saving, like there is some counter that's incrementing, but I've no idea where to look for this, or how to zero it each time.</p> <p>Some help would be much appreciated. thanks</p> <p>JMB</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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