예제 #1
0
 /**
  * Get connection
  *
  * Returns a database connection. If the connection is not established, uses
  * options provided to establish one.
  *
  * @throws AeSessionDriverDatabaseException #400 if database options are
  *                                          invalid
  *
  * @return AeInterface_Database
  */
 public function getConnection()
 {
     if ($this->_connection instanceof AeInterface_Database && !$this->_connection->isConnected()) {
         // *** The connection is lost, try to reestablish it
         $this->_connection = null;
     }
     if (!$this->_connection instanceof AeInterface_Database) {
         // *** Establish connection first
         if ($this->_options instanceof AeArray) {
             // *** An array of settings
             if (isset($this->_options['name'])) {
                 // *** Connection name and settings file path assumed
                 $this->_connection = AeDatabase::getConnection($this->_options['name'], $this->_options['settings']);
             } else {
                 if (isset($this->_options['driver'])) {
                     // *** Connection options array assumed
                     $this->_connection = AeDatabase::getInstance($this->_options['driver'], $this->_options['username'], $this->_options['password'], $this->_options['options']->value);
                 } else {
                     // *** Invalid options array
                     throw new AeSessionDriverDatabaseException('Invalid database connection options', 400);
                 }
             }
         } else {
             if ($this->_options instanceof AeCallback) {
                 // *** A callback
                 $this->_connection = $this->_options->call();
                 if (!$this->_connection instanceof AeInterface_Database) {
                     // *** Invalid callback return value
                     throw new AeSessionDriverDatabaseException('Invalid database connection options', 400);
                 }
             } else {
                 // *** Invalid options value
                 throw new AeSessionDriverDatabaseException('Invalid database connection options', 400);
             }
         }
     }
     return $this->_connection;
 }
예제 #2
0
 public static function getInstance($class, AeInterface_Database $database)
 {
     if (!$database->isConnected()) {
         throw new AeOrmModelException('Database connection must be established first', 401);
     }
     if (!is_subclass_of($class, 'AeOrm_Model')) {
         throw new AeOrmModelException('Class must be a subclass of AeOrm_Model', 405);
     }
     static $models = array();
     $key = md5($database->getClass() . $class);
     if (!isset($models[$key])) {
         $models[$key] = new $class($database);
     }
     return $models[$key];
 }