public static function initDb() { $config = \Phalcon\DI::getDefault()->get('config'); $connection = new \Phalcon\Db\Adapter\Pdo\Mysql($config->database->toArray()); if (getenv('APPLICATION_ENV') == 'devel') { $eventsManager = new \Phalcon\Events\Manager(); $eventsManager->attach('db', function ($event, $connection) { if ($event->getType() == 'beforeQuery') { //Start a profile with the active connection error_log($connection->getSQLStatement() . "\n" . json_encode($connection->getSQLVariables())); } }); $connection->setEventsManager($eventsManager); } return $connection; }
}; $di["session"] = function () { $session = new \Phalcon\Session\Adapter\Files(); $session->start(); return $session; }; $di["db"] = function () use($di) { $config = $di->get("config")->get("common")["db"]; $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config["host"], "username" => $config["username"], "password" => $config["password"], "dbname" => $config["dbname"], "charset" => $config["charset"])); $eventsManager = new \Phalcon\Events\Manager(); $dblog = $di->get("config")->get("common")["dblog"]; $logger = new \Phalcon\Logger\Adapter\File(__DIR__ . $dblog); $eventsManager->attach('db:beforeQuery', function ($event, $connection) use($logger) { $sqlVariables = $connection->getSQLVariables(); if (count($sqlVariables)) { $logger->log($connection->getSQLStatement() . ' ' . join(', ', $sqlVariables), \Phalcon\Logger::INFO); } else { $logger->log($connection->getSQLStatement(), \Phalcon\Logger::INFO); } }); $connection->setEventsManager($eventsManager); return $connection; }; $di["dbBackupTool"] = function () use($di) { $config = $di->get("config")->get("common")["db"]; return new \Phalcon_wifi\Common\Ext\DBManager($config["username"], $config["password"], $config["host"], $config["dbname"]); }; $di["filter"] = function () { return new \Phalcon_wifi\Common\Ext\Filter(); }; $di["validateCodeCreator"] = function () use($di) {
protected function _getDI() { Phalcon\DI::reset(); $di = new Phalcon\DI(); $di->set('modelsManager', function () { return new Phalcon\Mvc\Model\Manager(); }); $di->set('modelsMetadata', function () { return new Phalcon\Mvc\Model\Metadata\Memory(); }); $di->set('db', function () { require 'unit-tests/config.db.php'; //return new Twm\Db\Adapter\Pdo\Mssql($configMssql); $connection = new Phalcon\Db\Adapter\Pdo\Mysql($configMysql); $eventsManager = new Phalcon\Events\Manager(); //Listen all the database events $eventsManager->attach('db', function ($event, $connection) { if ($event->getType() == 'beforeQuery') { echo $connection->getSQLStatement(); } }); //Assign the eventsManager to the db adapter instance $connection->setEventsManager($eventsManager); return $connection; }); return $di; }