Users running a non-download version of Vanilla (pulled from github), on branch release/2019.016 or master from the last 2 weeks should upgrade to release/2019.017 or latest master for security reasons. Downloaded official open sources releases are not affected.

How to save form data thru multiple models

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?


  • LincLinc Detroit Admin
    I would use 2 Form models in the same page.
  • LincLinc Detroit Admin
    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 Detroit Admin
    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="">
    <input type="hidden" id="Form_TransientKey" name="MyPrefix/TransientKey" value="KFTBQ...">
    <input type="hidden" id="Form_hpt" name="MyPrefix/hpt" value="" >
Sign In or Register to comment.