Example #1
0
 /**
  * Connect to the database when creating the PDOFactory instance
  *
  * Sets the PDO connection to return ORM_PDOStatement objects when preparing
  * SQL.
  *
  * Uses configuration settings from the Configuration class. An example setup:
  *
  * <i>Ini File (test.ini):</i>
  * @code
  * [database]
  * name = "test_db"
  * host = "localhost"  #This is optional [default='localhost']
  * user = "******"
  * pass = "******"
  * prefix = "pgsql" # optional DSN prefix (default='mysql')
  * @endcode
  * 
  * <i>Alternative Method:</i>
  * @code
  * [database]
  * dsn  = "sqlite:/opt/databases/mydb.sq3"
  * user = "******"
  * pass = "******"
  * @endcode
  *
  * \n\n
  * <i>PHP:</i>
  * @code
  * Configuration::Load('test.ini');
  *
  * // Note database is not specified as this uses the default "database"
  * $query = PDOFactory::Get("SELECT * FROM rabbits");
  * @endcode
  *
  * @throws ORMPDOInvalidDatabaseConfigurationException if
  *      configuration details are not present or invalid
  * 
  * @param string $databaseConfig
  *      Name of the group to load from the Configuration (normally this would
  *      be "database").
  * @return PDOFactory
  */
 private function __construct($databaseConfig)
 {
     if (!Configuration::GroupExists($databaseConfig)) {
         throw new ORMPDOInvalidDatabaseConfigurationException("No database configuration details for {$databaseConfig}");
     }
     $db_name = Configuration::$databaseConfig()->name;
     $db_host = Configuration::$databaseConfig()->host ?: 'localhost';
     $db_user = Configuration::$databaseConfig()->user;
     $db_pswd = Configuration::$databaseConfig()->pass;
     $db_prefix = Configuration::$databaseConfig()->prefix ?: 'mysql';
     $dsn = Configuration::$databaseConfig()->dsn;
     $dsn = $dsn ?: "{$db_prefix}:host={$db_host};dbname={$db_name};";
     $this->_setDatabaseType($dsn);
     try {
         $this->_db = new \PDO($dsn, $db_user, $db_pswd);
         $this->_db->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true);
         $this->_db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
         $this->_db->setAttribute(\PDO::ATTR_STATEMENT_CLASS, array(__NAMESPACE__ . '\\ORM_PDOStatement'));
     } catch (\PDOException $e) {
         throw new \ORM\Exceptions\ORMPDOInvalidDatabaseConfigurationException("[{$databaseConfig}] " . $e->getMessage());
     }
 }