Database::newFromContext PHP Method

newFromContext() public static method

Creates a Database object, opens the connection and returns the instance.
public static newFromContext ( TestSwarmContext $context )
$context TestSwarmContext
    public static function newFromContext(TestSwarmContext $context)
    {
        $dbConf = $context->getConf()->database;
        $db = new self();
        $db->context = $context;
        $db->host = $dbConf->host;
        $db->username = $dbConf->username;
        $db->password = $dbConf->password;
        $db->dbname = $dbConf->database;
        $db->open();
        return $db;
    }

Usage Example

Example #1
0
 /**
  * Get the Database object
  * @return Database
  */
 public function getDB()
 {
     if ($this->db === null) {
         // Check if there is a database lock
         $lock = $this->dbLock();
         if ($lock) {
             throw new SwarmException("Database is temporarily locked for maintenance (since: " . strftime("%c", $lock) . ")");
         }
         $this->db = Database::newFromContext($this);
     }
     return $this->db;
 }