Zend_Db_Exception - how to get adapter
In my project on PHP with Zend Framework i have many mysql servers and many pdo_mysql adapters. In one moment i'm catching exc开发者_JAVA技巧eption (Zend_Db_Statement_Exception). How i can to determinate which adapter throws this exception ?
There's nothing in Zend_Exceptions classes to get the origin of the Exception, but the the getTrace()
method. You can use this getTrace to get the Zend_Db_Select object object, and if your version of Zend Framework is not too old you have the getAdapter class on it (if you do not have the getAdapter on Zend_Db_Select it's not a very hard to code method, as $this->_adapter is present). So here is a code which can be used on the catch section to get details on the adapter configuration:
} catch (Exception $e) {
foreach($e->getTrace() as $trace) {
if($trace['class']=='Zend_Db_Adapter_Abstract' || 'Zend_Db_Adapter_Pdo_Abstract'==$trace['class']) {
$zendDbSelect = $trace['args'][0];
$zendDbAdapter = $zendDbSelect->getAdapter();
$conn = $zendDbAdapter->getConfig();
//output adapter configuration, more useful things could be done
// with that if you want
Zend_Debug::dump($conn);
// stop the loop on traces
break;
}
}
// to something else with the exception if you want
}
精彩评论