On every form generation, I store some information about the form. The form then receives a build ID so I can retrieve any state data (such as if an error occurs) again and again.
The table is like this:
DROP TABLE IF EXISTS form_states;
CREATE TABLE form_states (
bid INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
form_id VARCHAR(255) NOT NULL DEFAULT '',
data MEDIUMTEXT NOT NULL,
created INT NOT NULL DEFAULT 0,
expired INT NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE INDEX bid_form_id ON form_states (bid, form_id);
try {
$formState = new Base_FormState;
$formState->setFormId($this->getFormId());
$formState->setCreated(time());
$formState->setData(!is_null($oldState) ? serialize($oldState) : serialize($this));
$formState->store();
// Add Build ID
$this->createValues(array('build_id' => $formState->getBid()));
}
catch (fValidationException $e) {
}
What's weird is that if I use:
CREATE UNIQUE INDEX bid_form_id ON form_states (bid, form_id);
#!text/html
The following problems were found: Bid, Form ID: The values specified must be a unique combination, however the specified combination already exists
On a page where 6 forms with the same form_id (same form but different parameters), every other form has no build_id (2,4,6) and for each I get an fValidation exception. I don't understand how this can happen when there's no real threading in PHP, I thought the fORM was performing transactions and/or locking the table anyway, and I don't specify the build ID in creating the form state anywhere.
Only 2 functions call the above code: one when the form is about to be rendered, and one after the form has been processed in order to update the state (but store a new record and get a new build ID).
For now I've decided on not having the UNIQUE INDEX. But this was working before, so I'm not sure what changed.