James Billson
James Billson

Reputation: 31

Manipulating form input values after submission causes multiple instances

I'm building a form with Yii that updates two models at once.
The form takes the inputs for each model as $modelA and $modelB and then handles them separately as described here http://www.yiiframework.com/wiki/19/how-to-use-a-single-form-to-collect-data-for-two-or-more-models/

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.

There's an additional twist that $modelB has a file which needs to be saved.

My action code is as follows:

if(isset($_POST['Documents'], $_POST['DocumentVersions']))
    {
        $modelA->attributes=$_POST['Documents'];
        $modelB->attributes=$_POST['DocumentVersions'];


        $valid=$modelA->validate();
        $valid=$modelB->validate() && $valid;


        if($valid)
        {

            $modelA->save(false); // don't validate as we validated above.
            $newdoc = $modelA->primaryKey; // get the ID of the document just created

            $modelB->document_id = $newdoc;         // set the Document_id of the DocumentVersions to be $newdoc
            // todo: set the filename to some long hash

            $modelB->file=CUploadedFile::getInstance($modelB,'file');       
            // finish set filename
            $modelB->save(false);

            if($modelB->save()) {
                $modelB->file->saveAs(Yii::getPathOfAlias('webroot').'/uploads/'.$modelB->file);
                }

            $this->redirect(array('projects/myprojects','id'=>$_POST['project_id']));
        }
    }
    ELSE {

    $this->render('create',array(
        'modelA'=>$modelA,
        'modelB'=>$modelB,
        'parent'=>$id,
        'userid'=>$userid,
        'categories'=>$categoriesList
    ));
    }

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.
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).
I've no idea how to prevent this.

UPDATE....

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.

Some help would be much appreciated. thanks

JMB

Upvotes: 2

Views: 620

Answers (1)

James Billson
James Billson

Reputation: 31

OK, I had Ajax validation set to true. This was calling the create action and inserting entries. I don't fully get this, or how I could use ajax validation if I really wanted to without this effect, but... at least the two model insert with relationship works.

Thanks for the comments. cheers JMB

Upvotes: 1

Related Questions