- Lazy connections possible through Doctrine_Manager::addDSN($dsn, $connectionName). Connections will then be opened automatically when they are requested the first time.
Changes concerning multiple connection control: - injection of the Connection object into the Doctrine_Table constructor, instead of a static lookup. - added optional Connection parameter to save/delete of Doctrine_Record and Doctrine_Collection
This commit is contained in:
parent
a1bed77af2
commit
09d249e549
@ -707,8 +707,11 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function save() {
|
||||
$this->table->getConnection()->saveCollection($this);
|
||||
public function save(Doctrine_Connection $conn = null) {
|
||||
if ($conn == null) {
|
||||
$conn = $this->table->getConnection();
|
||||
}
|
||||
$conn->saveCollection($this);
|
||||
}
|
||||
/**
|
||||
* single shot delete
|
||||
@ -716,8 +719,11 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
|
||||
* uses only one database query to perform this operation
|
||||
* @return boolean
|
||||
*/
|
||||
public function delete() {
|
||||
$ids = $this->table->getConnection()->deleteCollection($this);
|
||||
public function delete(Doctrine_Connection $conn = null) {
|
||||
if ($conn == null) {
|
||||
$conn = $this->table->getConnection();
|
||||
}
|
||||
$ids = $conn->deleteCollection($this);
|
||||
$this->data = array();
|
||||
}
|
||||
/**
|
||||
|
@ -172,10 +172,10 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
|
||||
$class = $name."Table";
|
||||
|
||||
if(class_exists($class) && in_array("Doctrine_Table", class_parents($class))) {
|
||||
return new $class($name);
|
||||
return new $class($name, $this);
|
||||
} else {
|
||||
|
||||
return new Doctrine_Table($name);
|
||||
return new Doctrine_Table($name, $this);
|
||||
}
|
||||
}
|
||||
/**
|
||||
|
@ -32,6 +32,10 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
|
||||
* @var array $connections an array containing all the opened connections
|
||||
*/
|
||||
private $connections = array();
|
||||
/**
|
||||
* @var array $dataSourceNames an array containing all available data source names
|
||||
*/
|
||||
private $dataSourceNames = array();
|
||||
/**
|
||||
* @var integer $index the incremented index
|
||||
*/
|
||||
@ -194,12 +198,27 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
|
||||
* @return object Doctrine_Connection
|
||||
* @throws InvalidKeyException
|
||||
*/
|
||||
public function getConnection($index) {
|
||||
if( ! isset($this->connections[$index]))
|
||||
throw new InvalidKeyException();
|
||||
|
||||
$this->currIndex = $index;
|
||||
return $this->connections[$index];
|
||||
public function getConnection($name) {
|
||||
if (!isset($this->connections[$name])) {
|
||||
if (isset($this->dataSourceNames[$name])) {
|
||||
$conn = Doctrine_DB::getConnection($this->dataSourceNames[$name]); // Establishes the connection
|
||||
$this->openConnection($conn, $name);
|
||||
} else {
|
||||
throw new Doctrine_Manager_Exception("Unknown connection: $name");
|
||||
}
|
||||
}
|
||||
$this->currIndex = $name;
|
||||
return $this->connections[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the dsn of a connection to the list of available data source names.
|
||||
*
|
||||
* @param string $dsn
|
||||
* @param string $name
|
||||
*/
|
||||
public function addDSN($dsn, $name) {
|
||||
$this->dataSourceNames[$name] = $dsn;
|
||||
}
|
||||
public function getSession($index) { return $this->getConnection($index); }
|
||||
|
||||
|
5
Doctrine/Manager/Exception.php
Normal file
5
Doctrine/Manager/Exception.php
Normal file
@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
class Doctrine_Manager_Exception extends Doctrine_Exception { }
|
||||
|
||||
?>
|
@ -826,12 +826,15 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
final public function save() {
|
||||
$this->table->getConnection()->beginTransaction();
|
||||
final public function save(Doctrine_Connection $conn = null) {
|
||||
if ($conn == null) {
|
||||
$conn = $this->table->getConnection();
|
||||
}
|
||||
$conn->beginTransaction();
|
||||
|
||||
$saveLater = $this->table->getConnection()->saveRelated($this);
|
||||
$saveLater = $conn->saveRelated($this);
|
||||
|
||||
$this->table->getConnection()->save($this);
|
||||
$conn->save($this);
|
||||
|
||||
foreach($saveLater as $fk) {
|
||||
$table = $fk->getTable();
|
||||
@ -847,7 +850,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
|
||||
|
||||
$this->saveAssociations();
|
||||
|
||||
$this->table->getConnection()->commit();
|
||||
$conn->commit();
|
||||
}
|
||||
/**
|
||||
* returns an array of modified fields and associated values
|
||||
@ -1060,8 +1063,11 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
|
||||
*
|
||||
* @return boolean true on success, false on failure
|
||||
*/
|
||||
public function delete() {
|
||||
return $this->table->getConnection()->delete($this);
|
||||
public function delete(Doctrine_Connection $conn = null) {
|
||||
if ($conn == null) {
|
||||
$conn = $this->table->getConnection();
|
||||
}
|
||||
return $conn->delete($this);
|
||||
}
|
||||
/**
|
||||
* returns a copy of this object
|
||||
|
@ -137,8 +137,8 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
|
||||
* @throws Doctrine_Table_Exception if there is already an instance of this table
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($name) {
|
||||
$this->connection = Doctrine_Manager::getInstance()->getCurrentConnection();
|
||||
public function __construct($name, Doctrine_Connection $conn) {
|
||||
$this->connection = $conn;
|
||||
|
||||
$this->setParent($this->connection);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user