0

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)?

Eutherpy
  • 4,471
  • 7
  • 40
  • 64

1 Answers1

0

How to use OptimisticLockingActiveRecord:

  1. Inherit your model from OptimisticLockingActiveRecord class

    class MyModel extends OptimisticLockingActiveRecord
    
    
    {
        ...
    
    
    }
    
  2. 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'); ?>
        ...
    
  3. 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