Saffrongeek’s Diarylog
…..memoirs moments opened…

civicrm- dbhandle function I use a lot of times

In my cron class scripts, I very fondly make use of the function for dbhandle , got from civicrm files itself.

static function &dbHandle( &$config ) {
CRM_Core_Error::ignoreException( );
$db_uf = DB::connect($config->userFrameworkDSN);
CRM_Core_Error::setCallback();
if ( ! $db_uf ||
DB::isError( $db_uf ) ) {
CRM_Core_Error::statusBounce( ts( “Cannot connect to db via %1. Please check the CIVICRM_UF_DSN value in your   civicrm.settings.php file”,
array( 1 => $db_uf->getMessage( ) ) ) );
}
return $db_uf;
}// static dbhandle function

Once I have this function , all I need to do is

$config =& CRM_Core_Config::singleton( );
CRM_Core_Error::ignoreException( );

$db_uf =& self::dbHandle( $config );

Note: It is always a good practise to close connection . This can be done as below:

$db_uf->disconnect();




No Responses Yet to “civicrm- dbhandle function I use a lot of times”

Leave a Reply