【发布时间】:2018-06-20 09:37:07
【问题描述】:
我在 Controller 中有这个,用 giiant 生成的(我认为它是正确的):
public function actionDelete($id) {
try {
$this->findModel($id)->delete();
} catch (\Exception $e) {
$msg = (isset($e->errorInfo[2])) ? $e->errorInfo[2] : $e->getMessage();
\Yii::$app->getSession()->addFlash('deleteError', $msg);
return $this->redirect(Url::previous());
}
$isPivot = strstr('$id', ',');
if ($isPivot == true) {
return $this->redirect(Url::previous());
} elseif (isset(\Yii::$app->session['__crudReturnUrl']) && \Yii::$app->session['__crudReturnUrl'] != '/') {
Url::remember(null);
$url = \Yii::$app->session['__crudReturnUrl'];
\Yii::$app->session['__crudReturnUrl'] = null;
return $this->redirect($url);
} else {
return $this->redirect(['index']);
}
}
现在我的问题是,它有时会抛出 $msg 作为数组,我不知道为什么。
查看/索引:
if (\Yii::$app->session->getFlash('deleteError') !== null) {
echo Alert::widget([
'options' => ['class' => 'alert-danger'],
'body' => \Yii::$app->session->getFlash('deleteError'),
])
;}
错误信息:
PHP 通知 – yii\base\ErrorException
数组到字符串的转换
我不知道为什么它有时是数组,有时不是。所有控制器都是相同的,但对于某些模型它可以工作,而对于某些模型则不能。我可以实现if is_array then ...[0] otherwise ... 的解决方法,但我觉得这不是解决问题的正确方法。模型如何更改此错误消息的数据类型?你能指出我正确的方向吗?非常感谢。
【问题讨论】:
-
您能否向我们展示您遇到的错误示例?
-
你能不能展示一下
findModel()函数是什么样的,或者它是GII默认生成的函数
标签: php activerecord error-handling yii2 delete-row