/** * Get an instance of a database * @return Database */ public static function getInstance() { // create an instance if there is no instance if (self::$instance == null) { self::$instance = new Database(); } return self::$instance; }
/** * Initialize database config * @link http://php.net/manual/pdo.construct.php * @param string $dsn Data Source Name, contains the information required to connect to the database. * @param string $username Username for the DSN string * @param string $password Password for the DSN string * @param array $options A key=>value array of driver-specific connection options. * @throws Error */ public static function initialize($dsn, $username = null, $password = null, $options = array()) { if (self::$instance) { throw new Error('Cannot re-initialize database'); } self::$instance = new Database($dsn, $username, $password, $options); self::$instance->setAttribute(self::ATTR_ERRMODE, self::ERRMODE_EXCEPTION); $currentSqlMode = Database::getInstance()->query('SELECT @@GLOBAL.SQL_MODE')->fetchColumn(); if (strpos($currentSqlMode, 'STRICT_TRANS_TABLES')) { $currentSqlMode = explode(',', $currentSqlMode); $strictTransTable = array_search('STRICT_TRANS_TABLES', $currentSqlMode); unset($currentSqlMode[$strictTransTable]); $statement = self::$instance->prepare('SET SESSION sql_mode = ?'); $statement->bindValue('1', implode(',', $currentSqlMode)); $statement->execute(); } }