如何在 Yii2 中使用乐观锁编写更新操作?

问题描述 投票:0回答:1

我正在使用乐观锁定在 Yii2 中编写待办事项应用程序。

我现在需要写更新动作。 为此,我尝试编写模型、控制器和视图。

我使用这个文档链接:https://www.yiiframework.com/doc/guide/2.0/en/db-active-record#optimistic-locks 我的模特:

<?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';
    }

}

我的控制器:

<?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');
        }
    }

我的 update.php 文件

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

但似乎我的代码没有做我想要的。 请帮忙!

yii yii2 optimistic-locking
1个回答
0
投票
In your update.php view, make sure that you are including the version attribute as a hidden input field:
    


<?php echo Html::activeHiddenInput($model, 'version'); ?>

    public function actionUpdate($id)
     {
        $model = RecordModel::findOne($id);
        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) {
                 $model = RecordModel::findOne($model->id);
                 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,
                ]);
              }
        }
© www.soinside.com 2019 - 2024. All rights reserved.