Example #1
0
 /**
  * Get an instance of a preexisting Connection or a new instance.
  *
  * @param  array $group
  * @return \HMC\Database\Connection
  */
 public static function get($group = false)
 {
     // Determining if exists or it's not empty, then use default group defined in config
     $group = !$group ? array('type' => \HMC\Config::DATABASE_TYPE(), 'host' => \HMC\Config::DATABASE_HOST(), 'name' => \HMC\Config::DATABASE_NAME(), 'user' => \HMC\Config::DATABASE_USER(), 'pass' => \HMC\Config::DATABASE_PASS()) : $group;
     // Group information
     $type = $group['type'];
     $host = $group['host'];
     $name = $group['name'];
     $user = $group['user'];
     $pass = $group['pass'];
     // ID for database based on the group information
     $id = "{$type}.{$host}.{$name}.{$user}.{$pass}";
     // Checking if the same
     if (isset(self::$instances[$id])) {
         return self::$instances[$id];
     }
     try {
         // 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 Connection("{$type}:host={$host};dbname={$name};charset=utf8", $user, $pass);
         $instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
         // Setting Database into $instances to avoid duplication
         self::$instances[$id] = $instance;
         return $instance;
     } catch (PDOException $e) {
         $msg = Logger::buildExceptionMessage($e);
         \HMC\Error::showError(501, $msg);
     }
 }