yii 如何在Yi2中使用乐观锁编写删除操作?

amrnrhlw  于 2023-03-08  发布在  其他
关注(0)|答案(1)|浏览(242)

我在Yii2中使用乐观锁定编写待办事项应用程序。
我现在需要编写删除操作。
我试着写模型,控制器,也为这个目的的看法。
我使用此文档链接:www.example.com我的型号: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 $permitValue = null;

    public static function tableName()
    {
        return 'records';
    }

    public function behaviors()
    {
        return [
            [
                'class' => OptimisticLockBehavior::class,
                'value' => $this->permitValue
            ],
        ];
    }

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

    public function rules()
    {
        return [
            [ ['title', 'priority'], 'required'],
            [ ['title', 'priority'], 'trim'],
            ['permitValue', 'safe']
        ];
    }
}

我的控制器:

public function actionDelete($id, $version)
    {
        $model = RecordModel::findOne(['id'=>$id, 'version'=>$version]);
        $model->permitValue = $version;
        try {
            $model->delete();
            Yii::$app->session->setFlash('success', 'Successfully deleted!');
            return $this->redirect('/');
        }catch (StaleObjectException $e) {
            return var_dump('<pre>' . $e . '</pre>');die();
            $model = RecordModel::findOne($model->id);
            Yii::$app->session->setFlash('error', 'Conflict,
                item was changed by another user, your changes will be lost. Edit again or Cancel');
            return $this->render('edit', [
                'model' => $model,
            ]);
        }
    }

我的edit.php,从那里我调用删除操作:

<h1>Edit page</h1>
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<?= \app\widgets\Alert::widget();?>
<? $form = ActiveForm::begin();?>
<?= $form->field($model, 'title')->textarea(['rows'=>5])->label('Title');?>
<?= $form->field($model, 'priority')->label('Priority');?>
<?= $form->field($model, 'done')->checkbox(['class' => 'mb-3 form-group'])?>
<?= Html::activeHiddenInput($model, 'version');?>
<?=   Html::submitButton('Save', ['class' => 'btn btn-success']);?>
<?    ActiveForm::end();?>
<?= Html::a('Cancel', '/', ['class' => 'btn btn-warning right'])?>
<?= Html::a('Delete', ['delete', 'id' => $model->id, 'version' => $model->version], ['class' => 'btn btn-danger right'])?>
<?= Html::a('Edit again', ['edit', 'id' => $model->id], ['class' => 'btn btn-primary right'])?>

我的调试栏总是显示(version = 0)-它不会改变:"从records中删除,其中(id = 100)和(version = 0)"
救命啊!

roqulrg3

roqulrg31#

public function actionDelete($id, $version){
        $model = RecordModel::findOne($id);
        $transaction = \Yii::$app->db->beginTransaction();
        try {
            $model->permitValue = $version;
            $model->delete();
            Yii::$app->session->setFlash('success', 'Successfully deleted!');
            return $this->redirect('/');
        }catch (StaleObjectException $e) {
            return var_dump('<pre>' . $e . '</pre>');die();
            $model = RecordModel::findOne($model->id);
            Yii::$app->session->setFlash('error', 'Conflict,
                item was changed by another user, your changes will be lost. Edit again or Cancel');
            return $this->render('edit', [
                'model' => $model,
            ]);
        }
    }

相关问题