当前位置: 动力学知识库 > 问答 > 编程问答 >

php - why yii errorhandler didn't capture CDbexception

问题描述:

In my project , there is a CDbexception message

CDbCommand failed to execute the SQL statement: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'xxx.xxx' doesn't exist

dsiplay on the web page when database table is not exist ,

in here i want use customer page to instead of the CDBexception page,but now i found the Yii errorHandler can't capture this error .

so what additional configuration should be configured in YII framework ?

网友答案:

You could do several thing:

1) Surround the code that could throw the error with a try catch:

try {
    //your sql code
} catch(CDbException $e) {
    // you redirect the user to the appropriate page, ...
}

2) Use the build-in error handler

You activate it in your configuration file:

return array(
    ......
    'components'=>array(
        'errorHandler'=>array(
            'errorAction'=>'site/error',
        ),
    ),
);

In the configuration we used the action site/error so you need to have it in your project. You can create this method like:

public function actionError()
{
    if($error=Yii::app()->errorHandler->error)
        $this->render('error', $error);
}

ps: the code is from the yii guide!

分享给朋友:
您可能感兴趣的文章:
随机阅读: