我正在運行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);
,不知怎的,指引者持有不正確的值?
加'findModel()'函數 –
'$ className'的'findModel()'也會很方便。有一個很大的機會,你有一個查詢條件,不限制行被刪除,因此問題。 – Bizley
@Bizley謝謝你的回覆,不幸的是我不明白你在說什麼。你能舉個例子嗎?我更新了問題以顯示實際的$附件模型的$ className :: findModel方法 – Luc