protected function SavePersonForceUpdate($strFormId, $strControlId, $strParameter) { if ($strParameter == '1') { $objPerson = $this->objPerson1; $objPerson->FirstName = $this->txtFirstName1->Text; $objPerson->LastName = $this->txtLastName1->Text; } else { $objPerson = $this->objPerson2; $objPerson->FirstName = $this->txtFirstName2->Text; $objPerson->LastName = $this->txtLastName2->Text; } // Do the Save, Forcing the Update $objPerson->Save(false, true); // Reload the Person so that it's fresh if ($strParameter == '1') { $this->objPerson1 = PersonWithLock::Load(5); } else { $this->objPerson2 = PersonWithLock::Load(5); } }
_p(QApplication::$ScriptName); ?> "> <p>Saving a Single Object will perform the save normally<br /> <input type="submit" id="single" name="single" value="Save 1 Object"/></p> <p>Attempting to save a Two Instances of the Same Object will throw an <strong>Optimistic Locking Exception</strong><br /> <input type="submit" id="double" name="double" value="Save 2 Objects (same Instance)"/></p> <p>Using <strong>$blnForceUpdate</strong> to avoid the <strong>Optimistic Locking Exception</strong><br/> <input type="submit" id="double_force" name="double_force" value="Force Update Second Object"/></p> </form> <?php // Load the Two Person objects (same instance -- let them both be Person ID #4) $objPerson1 = PersonWithLock::Load(5); $objPerson2 = PersonWithLock::Load(5); // Some RDBMS Vendors' TIMESTAMP is only precise to the second // Let's force a delay to the next second to ensure timestamp functionality // Note: on most web applications, because Optimistic Locking are more application user- // level constraints instead of systematic ones, this delay is inherit with the web // application paradigm. The following delay is just to simulate that paradigm. $dttNow = new QDateTime(QDateTime::Now); while ($objPerson1->SysTimestamp == $dttNow->qFormat(QDateTime::FormatIso)) { $dttNow = new QDateTime(QDateTime::Now); } // Make Changes to the Object so that the Save Will Update Something if ($objPerson1->FirstName == 'Al') { $objPerson1->FirstName = 'Alfred'; $objPerson2->FirstName = 'Al'; } else { $objPerson1->FirstName = 'Al';