Skip to content

Commit

Permalink
added ability to reestablish the database connection: YourModel::rees…
Browse files Browse the repository at this point in the history
…tablish_connection()
  • Loading branch information
kla committed Jul 10, 2010
1 parent 70c1920 commit aed05c2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
12 changes: 12 additions & 0 deletions lib/ConnectionManager.php
Expand Up @@ -34,5 +34,17 @@ public static function get_connection($name=null)
}
return self::$connections[$name];
}

/**
* Drops the connection from the connection manager. Does not actually close it since there
* is no close method in PDO.
*
* @param string $name Name of the connection to forget about
*/
public static function drop_connection($name=null)
{
if (isset(self::$connections[$name]))
unset(self::$connections[$name]);
}
};
?>
10 changes: 10 additions & 0 deletions lib/Model.php
Expand Up @@ -713,6 +713,16 @@ public static function connection()
return static::table()->conn;
}

/**
* Re-establishes the database connection with a new connection.
*
* @return Connection
*/
public static function reestablish_connection()
{
return static::table()->reestablish_connection();
}

/**
* Returns the {@link Table} object for this model.
*
Expand Down
16 changes: 12 additions & 4 deletions lib/Table.php
Expand Up @@ -77,10 +77,7 @@ public function __construct($class_name)
{
$this->class = Reflections::instance()->add($class_name)->get($class_name);

// if connection name property is null the connection manager will use the default connection
$connection = $this->class->getStaticPropertyValue('connection',null);

$this->conn = ConnectionManager::get_connection($connection);
$this->reestablish_connection(false);
$this->set_table_name();
$this->get_meta_data();
$this->set_primary_key();
Expand All @@ -93,6 +90,17 @@ public function __construct($class_name)
$this->callback->register('after_save', function(Model $model) { $model->reset_dirty(); }, array('prepend' => true));
}

public function reestablish_connection($close=true)
{
// if connection name property is null the connection manager will use the default connection
$connection = $this->class->getStaticPropertyValue('connection',null);

if ($close)
ConnectionManager::drop_connection($connection);

return ($this->conn = ConnectionManager::get_connection($connection));
}

public function create_joins($joins)
{
if (!is_array($joins))
Expand Down

0 comments on commit aed05c2

Please sign in to comment.