Yii2 - 数据被删除

时间:2017-10-13 04:37:15

标签: php yii2

我正在运行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个条目是不可能的,因为用户甚至看不到这些条目。他只看到自己的作品。

所以我的问题是,是否有可能以不正常的方式调用删除操作?

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

cheerz, LUC

修改

findModel函数:

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

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

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

EDIT2: 我查看了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);

以某种方式引用者持有不正确的值?

1 个答案:

答案 0 :(得分:0)

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