我想在插入时捕获数据库异常(如外键或重复异常)。
try {
$command = Yii::app()->db->createCommand()
->insert('table1', array(
'sid'=>$sid,
'val1'=>$val1
));
} catch (CDbException $e) {
die();
}
但是在我抓住它之前,命令会抛出异常。
...\framework\db\CDbCommand.php(358)
346 {
347 if($this->_connection->enableProfiling)
348 Yii::endProfile('system.db.CDbCommand.execute('.$this->getText().$par.')','system.db.CDbCommand.execute');
349
350 $errorInfo=$e instanceof PDOException ? $e->errorInfo : null;
351 $message=$e->getMessage();
352 Yii::log(Yii::t('yii','CDbCommand::execute() failed: {error}. The SQL statement executed was: {sql}.',
353 array('{error}'=>$message, '{sql}'=>$this->getText().$par)),CLogger::LEVEL_ERROR,'system.db.CDbCommand');
354
355 if(YII_DEBUG)
356 $message.='. The SQL statement executed was: '.$this->getText().$par;
357
358 throw new CDbException(Yii::t('yii','CDbCommand failed to execute the SQL statement: {error}',
359 array('{error}'=>$message)),(int)$e->getCode(),$errorInfo);
360 }
361 }