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();




Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: