yiiyii2optimistic-locking

How to write update action using Optimistic Locking in Yii2?


I am writing todo app in Yii2 using optimistic Locking.

I need to write update action now. I tried to write model, controller and also view for this purpose.

I use this documentation link :https://www.yiiframework.com/doc/guide/2.0/en/db-active-record#optimistic-locks My model:

<?php

namespace app\models;

use yii\behaviors\OptimisticLockBehavior;
use yii\db\ActiveRecord;

class RecordModel extends ActiveRecord
{
    public static function tableName()
    {
        return 'records';
    }
    public function behaviors()
    {
        return [
            OptimisticLockBehavior::class,
        ];
    }

    public function optimisticLock()
    {
        return 'version';
    }

}

My controller:

<?php


namespace app\controllers;
use Yii;
use app\models\RecordModel;
use yii\db\StaleObjectException;

class RecordController extends AppController
{
    public function actionUpdate($id=1)
    {

        $model = RecordModel::find()->where(['id' => $id])->one();
        //debug($model);die();

        try {
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
                Yii::$app->session->setFlash('success', 'Success');
                return $this->refresh();
            } else {
                return $this->render('update', [
                    'model' => $model,
                ]);
            }
        } catch (StaleObjectException $e) {
            Yii::$app->session->setFlash('error', 'Error');
        }
    }

My update.php file

<h1>index</h1>
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<?php if( Yii::$app->session->hasFlash('success') ): ?>
    <div
            class="alert alert-success alert-dismissible"
            role="alert"> <button type="button" class="close"
                                  data-dismiss="alert"
                                  aria-label="Close"><span
                    aria-hidden="true">&times;</span></button>
        <?php echo Yii::$app->session->getFlash('success'); ?>
    </div>
<?php endif; ?>
<?php if( Yii::$app->session->hasFlash ('error') ): ?>
    <div class="alert alert-danger alert-dismissible"
         role="alert"> <button type="button"
                               class="close" data-dismiss="alert" aria-label="Close"><span
                    aria-hidden="true">&times;</span></button>
        <?php echo Yii::$app->session->getFlash('error'); ?>
    </div>
<?php endif; ?>

<? $form = ActiveForm::begin();?>
<?= $form->field($model, 'text')->label('Text');?>
<? echo Html::activeHiddenInput($model, 'version');?>

<?=   Html::submitButton('Send', ['class' => 'btn btn-success']);?>
<?    ActiveForm::end();?>

But it seems my code does not do what i want. Please, help!


Solution

  • In your update.php view, make sure that you are including the version attribute as a hidden input field:
    
    public function actionUpdate($id){
        $model = RecordModel::findOne($id);
        try {
            if ($model->load(Yii::$app->request->post())){
    
                $model->text = Yii::$app->request->post('RecordModel')['text'];
                $model->version = Yii::$app->request->post('RecordModel')['version'];
    
                if ($model->save()) {
                    Yii::$app->session->setFlash('success', 'Record updated successfully.');
                } else {
                    Yii::$app->session->setFlash('error', 'Failed to update record.');
                }
                return $this->redirect(['index']);
            }
        } catch (StaleObjectException $e) {
            Yii::$app->session->setFlash('error', 'Record has been modified by another user. Please reload the record and try again.');
        }
    
        return $this->render('update', [
            'model' => $model,
        ]);
    }