HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

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?
Tagged:

Best Answer

  • LincLinc Detroit Admin
    Answer ✓
    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');

Answers

  • LincLinc Detroit Admin
    I would use 2 Form models in the same page.
  • LincLinc Detroit Admin
    Answer ✓
    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="">
    <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.