예제 #1
0
 /**
  * Registers the module-only services
  *
  * @param Phalcon\DI $di
  */
 public function registerServices($di)
 {
     /**
      * Read configuration
      */
     $config = (include __DIR__ . "/config/config.php");
     $di['view']->setViewsDir(__DIR__ . '/views/');
     /**
      * Database connection is created based in the parameters defined in the configuration file
      */
     $di['db'] = function () use($config) {
         $connection = new DbAdapter(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname));
         $eventsManager = new EventsManager();
         $logger = new FileLogger(__DIR__ . "/logs/db.log");
         //Listen all the database events
         $eventsManager->attach('db:beforeQuery', function ($event, $connection) use($logger) {
             $sqlVariables = $connection->getSQLVariables();
             if (count($sqlVariables)) {
                 $logger->log($connection->getSQLStatement() . ' ' . join(', ', $sqlVariables), Logger::INFO);
             } else {
                 $logger->log($connection->getSQLStatement(), Logger::INFO);
             }
         });
         //Assign the eventsManager to the db adapter instance
         $connection->setEventsManager($eventsManager);
         return $connection;
     };
 }
예제 #2
0
 private function setDB()
 {
     $connection = new DatabaseConnection($this->database->toArray());
     $debug = $this->application->debug;
     if ($debug) {
         $eventsManager = new EventsManager();
         $logger = new FileLogger(__DIR__ . "/../Logs/db.log");
         //Listen all the database events
         $eventsManager->attach('db', function ($event, $connection) use($logger) {
             if ($event->getType() == 'beforeQuery') {
                 $variables = $connection->getSQLVariables();
                 if ($variables) {
                     $logger->log($connection->getSQLStatement() . ' [' . join(',', $variables) . ']', \Phalcon\Logger::INFO);
                 } else {
                     $logger->log($connection->getSQLStatement(), \Phalcon\Logger::INFO);
                 }
             }
         });
         $connection->setEventsManager($eventsManager);
     }
     return $connection;
 }
}, true);
/**
 * Database connection is created based in the parameters defined in the configuration file
 */
$di->set('db', function () use($config) {
    $connection = new DatabaseConnection($config->database->toArray());
    $debug = $config->application->debug;
    if ($debug) {
        $eventsManager = new EventsManager();
        $logger = new FileLogger(APP_PATH . "/app/logs/db.log");
        //Listen all the database events
        $eventsManager->attach('db', function ($event, $connection) use($logger) {
            /** @var Phalcon\Events\Event $event */
            if ($event->getType() == 'beforeQuery') {
                /** @var DatabaseConnection $connection */
                $variables = $connection->getSQLVariables();
                if ($variables) {
                    $logger->log($connection->getSQLStatement() . ' [' . join(',', $variables) . ']', \Phalcon\Logger::INFO);
                } else {
                    $logger->log($connection->getSQLStatement(), \Phalcon\Logger::INFO);
                }
            }
        });
        //Assign the eventsManager to the db adapter instance
        $connection->setEventsManager($eventsManager);
    }
    return $connection;
});
/**
 * If the configuration specify the use of metadata adapter use it or use memory otherwise
 */