/** * Get a singleton Database instance. If configuration is not specified, * it will be loaded from the database configuration file using the same * group as the name. * * @param null $name * @param array $config * @return mixed */ public static function init($name = NULL, array $config = NULL) { if ($name === NULL) { // Use the default instance name $name = Database::$default; } if ($config === NULL) { // Load the configuration for this database $config = Config::load('database_simple'); } // Group information $type = $config['DB_TYPE']; $host = $config['DB_HOST']; $n_me = $config['DB_NAME']; $user = $config['DB_USER']; $pass = $config['DB_PASS']; // Checking if the same if (isset(self::$instances[$name])) { return self::$instances[$name]; } // I've run into problem where // SET NAMES "UTF8" not working on some hostings. // Specifiying charset in DSN fixes the charset problem perfectly! $instance = new Database("{$type}:host={$host};dbname={$n_me};", $user, $pass); $instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Setting Database into $instances to avoid duplication self::$instances[$name] = $instance; return $instance; }
/** * Load database if dbname exist * * @param null $db */ public function __construct($db = NULL) { if ($db) { // Set the instance or name $this->_db = $db; } elseif (!$this->_db) { // Use the default name $this->_db = Database::$default; } $this->_config = Config::load('database_simple'); if (is_string($this->_db)) { // Load the database $this->db = Database::init($this->_db); } }
/** * Open connect to database after model start construct * * @param null $db */ public function __construct($db = null) { if ($db) { // Set the instance or name $this->_db = $db; } elseif (!$this->_db) { // Use the default name $this->_db = Database::$default; } $config = Config::load('database', true); $this->_config = $config[$this->_db]; $this->_config['path'] = $config['path']; if (is_string($this->_db)) { // Load the database $this->db = Database::init($this->_db); } }
/** * Get a singleton Database instance. If configuration is not specified, * it will be loaded from the database configuration file using the same * group as the name. * * @param null $name * @param array $config * @return mixed */ public static function init($name = NULL, array $config = NULL) { if ($name === NULL) { // Use the default instance name $name = Database::$default; } if (!isset(Database::$instances[$name])) { if ($config === NULL) { // Load the configuration for this database $config = Config::load('database')[$name]; } // Set the driver class name $driver = '\\Modules\\Database\\Core\\Drivers\\D' . ucfirst($config['type']); // Create the database connection instance $driver = new $driver($name, $config); // Store the database instance Database::$instances[$name] = $driver; } return Database::$instances[$name]; }
public static function set($name, $value) { Config::set($name, $value); }