Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Try Vanilla Forums Cloud product
Vanilla 2.6 is here! It includes security fixes and requires PHP 7.0. We have therefore ALSO released Vanilla 2.5.2 with security patches if you are still on PHP 5.6 to give you additional time to upgrade.

How to save form data thru multiple models

CsabbencsCsabbencs
edited September 2011 in Development
I have a form based on a model.
I'd like to save the form values as usual, plus some parts of the form to another table for which I have another model.
I'd like to handle this 2 "DB operation" as a single transaction so if something bad happens to any of them I could rollback.
So, in pseudo-code-like written:
try { $this->Database->BeginTransaction(); ... $this->Form->SetModel($this->MyFirstModel); $this->Form->Save(); ... $this->Form->SetModel($this->MySecondModel); $this->Form->Save(); ... $this->Database->CommitTransaction(); } catch (Exception $Ex) { $this->Database->RollbackTransaction(); throw $Ex; }
Is it the correct way?
Should I rather forget the models and do the job by running separate DB calls?
try { $this->Database->BeginTransaction(); ... $this->Database->Query(); ... $this->Database->Query(); ... $this->Database->CommitTransaction(); } catch (Exception $Ex) { $this->Database->RollbackTransaction(); throw $Ex; }
Or should I do something totally different?

Comments

  • LincLinc Director of Development Detroit Vanilla Staff
    I would use 2 Form models in the same page.
  • LincLinc Director of Development Detroit Vanilla Staff
    So: $this->FormOtherModel = new Gdn_Form(); and then mix-n-match the form fields in your view:

    echo $this->Form->Input('Foo');
    echo $this->FormOtherModel->Input('Bar');
  • LincLinc Director of Development Detroit Vanilla Staff
    edited September 2011
    Also: Use the 'Developers' category for stuff like this so we can find it easier. (moved)
  • Back to this... I have created the 2 forms since then, of course...

    What I found now is that if you put multiple forms on the same page, though you can create different form IDs for the different forms, finally, every single form will have an input field with the ID Form_hpt and another with the ID Form_TransientKey, so these IDs will be multiplied on the page which is against html standards and the page will never pass w3c validator.

    My question is, if I changed Form_hpt and Form_TransientKey somewhere in the core so that they would be related to a CLASS not an ID, would form functionality still work?

  • I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • CsabbencsCsabbencs
    edited October 2012

    No. I've already checked that.
    As you can see in the generated code below, only Form ID gets 'MyPrefix', input IDs stick without getting 'MyPrefix'.

    <form id="Form_MyPrefix" method="post" action="">
    <div>
    <input type="hidden" id="Form_TransientKey" name="MyPrefix/TransientKey" value="KFTBQ...">
    <input type="hidden" id="Form_hpt" name="MyPrefix/hpt" value="" >
    </div>
    </form>
    
Sign In or Register to comment.