2017-10-13 89 views
0

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

我做了一种通用功能的它:

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, 吕克

编辑:

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

编辑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...') 

我仍然无法想象这个错误是怎么甚至抛出,因为调用findModel有错误的ID是不可能的从前端。

愿这可能有一些待办事项与线:

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

,不知怎的,指引者持有不正确的值?

+0

加'findModel()'函数 –

+0

'$ className'的'findModel()'也会很方便。有一个很大的机会,你有一个查询条件,不限制行被删除,因此问题。 – Bizley

+0

@Bizley谢谢你的回复,不幸的是我不明白你在说什么。你能举个例子吗?我更新了问题以显示实际的$附件模型的$ className :: findModel方法 – Luc

回答

0

始终与备份一起工作!在这种情况下,你输了