I have enabled the optimistic locking feature in my Yii aplication by accident. Now it is causing problems, and I want to disable it. Is there a painless way to do this (i.e. update my models and CRUD without having to create them again)?
Asked
Active
Viewed 300 times
1 Answers
0
How to use OptimisticLockingActiveRecord:
Inherit your model from OptimisticLockingActiveRecord class
class MyModel extends OptimisticLockingActiveRecord { ... }
Add optimistic locking field to the update form view file:
<?php $form=$this->beginWidget('CActiveForm'); ?> <?php echo $form->hiddenField($model, 'lock_version'); ?> <?php echo $form->error($model, 'lock_version'); ?> ...
Handle StaleObjectError in the controller:
...
if (isset($_POST['MyModel'])) { $model->attributes = $_POST['SiteSettingsData']; $model->lock_version = $_POST['MyModel']['lock_version']; try { $model->save(); } catch(StaleObjectError $e) { $model->addError('lock_version', Yii::t('app', 'Site settings have been updated by another user')); }
...

Waqas Amjad
- 195
- 1
- 13