Sunday, January 14, 2018

Overriding MysqliManager.php

Overriding the stock SQL Managers


It took me a while to figure out how to override the core MysqliManager class so that I could add some code to email me the SQL errors that usually just go to my logs.  At first I tried just creating the custom file, expecting it to be read like others I had created, but I found that you actually  have to tell the system to use your custom class in the config_override.php file like this



<?php
/***CONFIGURATOR***/
$sugar_config['dbconfig']['db_manager'] = 'CustomMysqliManager';
/***CONFIGURATOR***/
After that you can just create the standard custom file in custom/include/database/CustomMysqliManager.php like this overriding as little or as much as you want.
<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
require_once('include/database/MysqliManager.php');
/**
* MySQL manager implementation for mysql extension
*/
class CustomMysqliManager extends MysqliManager
{
public function registerError($userMessage, $message, $dieOnError = false)
{
//EMail me the error message
$GLOBALS['developerCommunication']->send($message, $userMessage);
//Call the parent function
parent::registerError($userMessage, $message, $dieOnError);
}
/**
* This method is called by every method that runs a query.
* If slow query dumping is turned on and the query time is beyond
* the time limit, we will log the query. This function may do
* additional reporting or log in a different area in the future.
*
* @param string $query query to log
* @param array $boundData prepared statement bound data
* @return boolean true if the query was logged, false otherwise
*/
public function dump_slow_queries($query, $boundData = null)
{
//run the parent function
$returnValue = parent::dump_slow_queries($query, $boundData);
//do my custom stuff
if ($returnValue) {
$message = "Slow Query (time: {$this->query_time}\n{$query}";
//EMail me the error message
$GLOBALS['developerCommunication']->send($message, '', false);
}
return $returnValue;
}
}

No comments:

Post a Comment