Hello,
I have created the following code to set the time zone for each connection in MySQL and it works perfectly.
My question is if there is any better way to do this?
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di->setShared(
'db',
function () {
$config = $this->getConfig();
$eventsManager = new EventsManager();
$logger = new FileLogger($config->application->logDir . 'db.log');
// Only apply once per connection
$timeZoneDefined = false;
// Listen all the database events
$eventsManager->attach(
'db:beforeQuery',
function ($event, $connection) use (&$logger, &$eventsManager, &$timeZoneDefined) {
/*
* Log the current query (VERY IMPORTANT LOG FIRST)
*/
$logger->log(
$connection->getSQLStatement(),
Logger::INFO
);
/*
* Set MYSQL TimeZone for all queries
*/
if ($timeZoneDefined === false) {
$connection->setEventsManager(new EventsManager()); // Temp NULL
$connection->execute("SET time_zone = '+00:00'"); // Underground SQL query!!!
$connection->setEventsManager($eventsManager); // Restore event manager
$timeZoneDefined = true;
}
// Let everything take its course...
}
);
$class = 'Phalcon\Db\Adapter\Pdo\\' . $config->database->adapter;
$params = [
'host' => $config->database->host,
'username' => $config->database->username,
'password' => $config->database->password,
'dbname' => $config->database->dbname,
'charset' => $config->database->charset
];
if ($config->database->adapter == 'Postgresql') {
unset($params['charset']);
}
$connection = new $class($params);
// Assign the eventsManager to the db adapter instance
$connection->setEventsManager($eventsManager);
return $connection;
});
Thank you!