Example #1
0
 /**
  * Prepares component
  *
  * @param \Phalcon\Config $database Database config
  *
  * @throws \Phalcon\Db\Exception
  */
 public static function setup($database)
 {
     if (!isset($database->adapter)) {
         throw new DbException('Unspecified database Adapter in your configuration!');
     }
     $adapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $database->adapter;
     if (!class_exists($adapter)) {
         throw new DbException('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);
     }
 }