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:
Is it the correct way?
Should I rather forget the models and do the job by running separate DB calls?
Or should I do something totally different?
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?
0
Best Answer
-
Linc AdminSo: $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');0
Answers
echo $this->Form->Input('Foo');
echo $this->FormOtherModel->Input('Bar');
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?
Does this help?
http://vanillaforums.org/discussion/comment/166506/#Comment_166506
I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.
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'.