It is necessary to check the connection to the database before the querying, if there is no connection - need to make a reconnect, and then immediately repeat this query.
I wrote EventsManager but I don't know how to execute the current query. I do not know where to get and in what format.
$di->set('db', function () use ($config) {
$connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array(
'host' => $this->get('config')->database->host,
'username' => $this->get('config')->database->username,
'password' => $this->get('config')->database->password,
'dbname' => $this->get('config')->database->dbname,
'charset' => $this->get('config')->database->charset,
'notNullValidations' => false,
));
$eventsManager = new \Phalcon\Events\Manager();
//Listen all the database events
$eventsManager->attach('db', function($event, $connection) {
if ($event->getType() == 'beforeQuery') {
$sql = // ???? HERE NEED TO GET THE CURRENT QUERY
try {
$connection->executeQuery($sql);
} catch (\PDOException $e) {
if (strpos($e->getMessage(), 'MySQL server has gone away') !== false) {
$connection->connect();
$connection->executeQuery($sql);
}
}
}
});
//Assign the eventsManager to the db adapter instance
$connection->setEventsManager($eventsManager);
return $connection;
});