예제 #1
1
 /**
  * Prepares component
  *
  * @param $database
  *
  * @throws \Phalcon\Exception
  */
 public static function setup($database)
 {
     if (!isset($database->adapter)) {
         throw new \Phalcon\Exception('Unspecified database Adapter in your configuration!');
     }
     $adapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $database->adapter;
     if (!class_exists($adapter)) {
         throw new \Phalcon\Exception('Invalid database Adapter!');
     }
     $configArray = $database->toArray();
     unset($configArray['adapter']);
     self::$_connection = new $adapter($configArray);
     self::$_databaseConfig = $database;
     if ($database->adapter == 'Mysql') {
         self::$_connection->query('SET FOREIGN_KEY_CHECKS=0');
     }
     if (Migrations::isConsole()) {
         $profiler = new Profiler();
         $eventsManager = new EventsManager();
         $eventsManager->attach('db', function ($event, $connection) use($profiler) {
             if ($event->getType() == 'beforeQuery') {
                 $profiler->startProfile($connection->getSQLStatement());
             }
             if ($event->getType() == 'afterQuery') {
                 $profiler->stopProfile();
             }
         });
         self::$_connection->setEventsManager($eventsManager);
     }
 }