Exemplo n.º 1
0
 /**
  * Initialize Database connection
  * @param array | Config_Abstract $dbConfig
  * @return Db_Manager_Interface
  */
 protected function _initDb()
 {
     $templatesPath = $this->_config->get('templates');
     $dev = $this->_config->get('development');
     $dbErrorHandler = function (Exception $e) use($templatesPath, $dev) {
         if (Request::isAjax()) {
             Response::jsonError(Lang::lang()->CANT_CONNECT);
         } else {
             $tpl = new Template();
             $tpl->set('error_msg', 'MySQL : ' . $e->getMessage());
             $tpl->set('development', $dev);
             echo $tpl->render($templatesPath . 'public/error.php');
             exit;
         }
     };
     $conManager = new Db_Manager($this->_config);
     try {
         $dbConfig = $conManager->getDbConfig('default');
         $this->_db = $conManager->getDbConnection('default');
         if ($dbConfig->get('adapterNamespace') == 'Db_Adapter') {
             $this->_db->setConnectionErrorHandler($dbErrorHandler);
         }
     } catch (Exception $e) {
         $dbErrorHandler($e);
     }
     /*
      * Store connection config in Registry
      */
     Registry::set('db', $dbConfig, 'config');
     Registry::set('db', $this->_db);
     return $conManager;
 }