Yii2 删除 ActiveRecord 错误消息有时是数组,有时不是?
Yii2 delete ActiveRecord error message sometimes array, sometimes not?
我在 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
作为数组抛出,我不知道为什么。
View/index:
if (\Yii::$app->session->getFlash('deleteError') !== null) {
echo Alert::widget([
'options' => ['class' => 'alert-danger'],
'body' => \Yii::$app->session->getFlash('deleteError'),
])
;}
错误信息:
PHP Notice – yii\base\ErrorException
Array to string conversion
我不知道为什么有时是数组有时不是。所有控制器都是相同的,但对于某些型号,它可以工作,而对于某些则不能。我可以实施 if is_array then ...[0] otherwise ...
的解决方法,但我觉得这不是解决问题的正确方法。模型如何更改此错误消息的数据类型?你能给我指出正确的方向吗?非常感谢。
首先,你实际上从来没有在这里使用模型验证,所以它与模型无关。其次,$msg
可能没问题,问题是你如何使用 getFlash()
。此方法 可能 return 数组,这是预期的并记录在案:
The flash message or an array of messages if addFlash was used.
您应该遍历 getFlash()
结果以获取字符串形式的实际消息,或者使用将处理此问题并从 getFlash()
结果生成多个警报的小部件。
您也可以使用 setFlash()
而不是 addFlash()
。但我不推荐这样做 - 它很容易覆盖以前的一些闪光,你不应该盲目地假设 getFlash()
结果将是一个字符串。
我在 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
作为数组抛出,我不知道为什么。
View/index:
if (\Yii::$app->session->getFlash('deleteError') !== null) {
echo Alert::widget([
'options' => ['class' => 'alert-danger'],
'body' => \Yii::$app->session->getFlash('deleteError'),
])
;}
错误信息:
PHP Notice – yii\base\ErrorException
Array to string conversion
我不知道为什么有时是数组有时不是。所有控制器都是相同的,但对于某些型号,它可以工作,而对于某些则不能。我可以实施 if is_array then ...[0] otherwise ...
的解决方法,但我觉得这不是解决问题的正确方法。模型如何更改此错误消息的数据类型?你能给我指出正确的方向吗?非常感谢。
首先,你实际上从来没有在这里使用模型验证,所以它与模型无关。其次,$msg
可能没问题,问题是你如何使用 getFlash()
。此方法 可能 return 数组,这是预期的并记录在案:
The flash message or an array of messages if addFlash was used.
您应该遍历 getFlash()
结果以获取字符串形式的实际消息,或者使用将处理此问题并从 getFlash()
结果生成多个警报的小部件。
您也可以使用 setFlash()
而不是 addFlash()
。但我不推荐这样做 - 它很容易覆盖以前的一些闪光,你不应该盲目地假设 getFlash()
结果将是一个字符串。