Yii2 删除外键约束数据时,会显示错误。如何以正确的方式获取错误,以便向用户显示

Yii2 删除外键约束数据时,会显示错误。如何以正确的方式获取错误,以便向用户显示,yii2,Yii2,SQLSTATE[23000]:完整性约束冲突:1451无法删除或更新父行:外键约束失败(m\u travellocation\u city,约束location\u city\u ibfk\u 1外键(country\u id)引用location\u country(id) 正在执行的SQL是:从id=2的位置\国家/地区删除 错误信息:数组 ( [0] => 23000 [1] => 1451 [2] =>无法删除或更新父行:外键约束失败(m\u travellocation\u city

SQLSTATE[23000]:完整性约束冲突:1451无法删除或更新父行:外键约束失败(
m\u travel
location\u city
,约束
location\u city\u ibfk\u 1
外键(
country\u id
)引用
location\u country
id
) 正在执行的SQL是:从id=2的位置\国家/地区删除 错误信息:数组 ( [0] => 23000 [1] => 1451 [2] =>无法删除或更新父行:外键约束失败(
m\u travel
location\u city
,约束
location\u city\u ibfk\u 1
外键(
country\u id
)引用
location\u country
id
) )你可以这样做

try {
    // delete command here
} catch (\yii\db\Exception $e) {
    if ($e->errorInfo[1] == 1451) {
        throw new \yii\web\HttpException(400, 'Failed to delete the object.');
    } else {
        throw $e;
    }
}

数据库查询$connection=Yii::$app->db$model=$connection->createCommand('DELETE FROM location__countrywhere id=:userid')$模型->bindParam(':userid',$userid)$userid=2;如果($model->execute()==false){抛出新的ServerErrorHttpException('由于未知原因未能删除该对象');}如何获取它是否删除的布尔值?