【问题标题】:Yii2 - Data deletedYii2 - 数据已删除
【发布时间】:2017-10-16 20:36:44
【问题描述】:

我正在运行一个 Yii2 应用程序。今天我遇到了一个问题,一个包含至少 250 个条目的整个表完全是空的。该表由文件信息条目(原始文件名、新文件名)组成。因此,每个条目都 - 在逻辑上 - 链接到文件系统中的文件。我检查了文件系统的文件,发现文件也被删除了。所以我得出结论,数据在 yii2 应用程序中被删除。我有一个将被称为 (POST) 的操作来删除一个条目。

我为它做了一个通用函数:

public function actionDelete($id, $className)
{
    $this->findModel($id, $className)->delete();       
    return $this->redirect(Yii::$app->request->referrer);
}

在视图中,我有一个带有操作列的文件附件列表。每个动作列都有这个方法调用:

echo TagHelper::deleteButton($attachment, Yii::t('app', 'Deleting a File'));

$attachment 是型号。

deleteButton 看起来像这样:

public static function deleteButton($model, $text, $view = null, $controller = 'delete/delete-check') {
    return Html::a('<span class="glyphicon glyphicon-trash"></span>', FALSE, ['value' => Url::to([$controller, 'id' => $model->id, 'className' => get_class($model), 'view' => $view]),
                'role' => 'button', 'title' => $text,
                'class' => 'showModalButton btn-link'
    ]);
}

这将打开一个带有视图delete/delete-check 的模态窗口打开一个模态窗口,如下所示:

<div class="delete-check">

    <?php $form = ActiveForm::begin(['id' => 'delete-check-form', 
        'method' => 'post', 
        'action'=>['delete', 'id' => $model->id, 'className' => get_class($model)]
    ]); ?>  

    <?php if ($model->deleteable()): ?>
        <p><?= Yii::t('app', 'You are going to delete the following entry:') ?></p>
        <div class="well well-sm"><?= $model ?></div>
        <p><?= Yii::t('app', "In the system there aren't any references to this entry found. Deleting this entry won't lead to any problems." ) ?></p>
        <p><?= Yii::t('app', "Deleting this entry is <mark>definitive</mark> and can't be undone." ) ?></p>

        <div class="form-group text-right">
            <?= Html::submitButton(Yii::t('app', 'Delete'), ['class' => 'btn btn-warning']) ?>
        </div>

    <?php else: ?>
        <p><?= Yii::t('app', "You can't delete the entry:") ?> </p>
        <div class="well well-sm"><?= $model ?></div>
        <p><?= Yii::t('app', "There are the following references found in the system:" ) ?></p>

            <?php echo $this->render('/' . $view . '/_reference.php', ['model' => $model]); ?>

        <div class="form-group text-right">           
          <?= Html::button(Yii::t('app', 'Ok'), ['data-dismiss' => 'modal', 'class' => 'btn btn-info']); ?>
        </div>
    <?php endif; ?>

    <?php ActiveForm::end(); ?>
</div>

这可能是问题吗?

应用程序本身管理着 40 多个用户。他们为自己的帐户输入不同的数据。因此,不可能从该表中删除超过 250 个条目,因为用户甚至看不到这些条目。他只看到自己的条目。

所以我的问题是,是否有可能在不规则的情况下以某种方式调用删除操作?

我真的被困在这里,因为我不知道从哪里开始调查。一些线索?

干杯, 卢克

编辑:

findModel 函数:

protected function findModel($id, $className)
{
    if (($model = $className::findModel($id)) !== null) {
        return $model;
    } else {
        throw new NotFoundHttpException('The requested page does not exist.');
    }
}

$attachment 模型的$className::findModel() 方法的实际调用:

public static function findModel($id)
{
    if (($model = EnsembleProposalHealthAttachment::findOne($id)) !== null) {
       if (Yii::$app->user->can("admin") || Yii::$app->user->id == $model->ensembleProposal->ensemble->theater->user_id) {
            return $model;
        } else {
            throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.'));
        }
    } else {
        throw new NotFoundHttpException('The requested page does not exist.');
    }
}

编辑2: 我查看了 yii2 日志文件,发现了一些有趣的异常(来自不同的模型,具有相同的删除逻辑),这可能属于该问题。

2017-10-09[][][][error][yii\web\HttpException:404] yii\web\NotFoundHttpException: The requested page does not exist. in models/EnsembleProposalProductionAttachment.php:93
Stack trace:
#0 controllers/DeleteController.php(73): app\models\EnsembleProposalProductionAttachment::findModel('168')
#1 controllers/DeleteController.php(66): app\controllers\DeleteController->findModel('168', 'app\\models\\Ense...')
#2 [internal function]: app\controllers\DeleteController->actionDelete('168', 'app\\models\\Ense...')

我仍然无法想象这个错误是如何引发的,因为无法从前端调用具有错误 id 的 findModel。

这可能与这条线有关:

return $this->redirect(Yii::$app->request->referrer);

在某种程度上,引荐来源网址持有不正确的值?

【问题讨论】:

  • 添加findModel()函数
  • 所涉及的$className 中的findModel() 也很方便。很有可能您有一个查询条件不限制要删除的行因此问题。
  • @Bizley 感谢您的回复,不幸的是我不明白您在说什么。你能举个例子吗?我更新了问题以显示实际 $attachment 模型的 $className::findModel 方法
  • 这看起来不错,所以我的怀疑是不对的。其他事情正在发生。
  • @Bizley 好的,刚刚又更新了我的问题。

标签: php yii2


【解决方案1】:

始终使用备份!在这种情况下你就输了

【讨论】:

    猜你喜欢
    • 2022-07-22
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 2011-07-11
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多