/**
  * 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;
 }
Beispiel #2
0
 /**
  * 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);
     }
 }