/** * Create Db_Object * @param string $name - object name * @param array $data - object config */ protected function _createObject($name, array $data) { $usePrefix = $data['use_db_prefix']; $connectionManager = new Db_Manager($this->_configMain); $connection = $connectionManager->getDbConnection($data['connection']); $connectionCfg = $connectionManager->getDbConfig($data['connection']); //$db = Model::getGlobalDbConnection(); $db = $connection; $tables = $db->listTables(); $oConfigPath = Db_Object_Config::getConfigPath(); $tableName = $data['table']; if ($usePrefix) { $tableName = $connectionCfg->get('prefix') . $tableName; } if (in_array($tableName, $tables, true)) { Response::jsonError($this->_lang->FILL_FORM, array(array('id' => 'table', 'msg' => $this->_lang->SB_UNIQUE))); } if (file_exists($oConfigPath . strtolower($name) . '.php')) { Response::jsonError($this->_lang->FILL_FORM, array(array('id' => 'name', 'msg' => $this->_lang->SB_UNIQUE))); } /* * Write object config */ if (!Config_File_Array::create($oConfigPath . $name . '.php')) { Response::jsonError($this->_lang->CANT_WRITE_FS); } $cfg = Config::factory(Config::File_Array, $oConfigPath . strtolower($name) . '.php'); /* * Add fields config */ $data['fields'] = array(); $cfg->setData($data); $cfg->save(); try { $cfg = Db_Object_Config::getInstance($name); $cfg->setObjectTitle($data['title']); if (!$cfg->save()) { Response::jsonError($this->_lang->CANT_WRITE_FS); } /* * Build database */ $builder = new Db_Object_Builder($name); $builder->build(); } catch (Exception $e) { Response::jsonError($this->_lang->CANT_EXEC . 'code 2'); } Response::jsonSuccess(); }
/** * 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; }