- 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
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function save() {
|
public function save(Doctrine_Connection $conn = null) {
|
||||||
$this->table->getConnection()->saveCollection($this);
|
if ($conn == null) {
|
||||||
|
$conn = $this->table->getConnection();
|
||||||
|
}
|
||||||
|
$conn->saveCollection($this);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* single shot delete
|
* 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
|
* uses only one database query to perform this operation
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function delete() {
|
public function delete(Doctrine_Connection $conn = null) {
|
||||||
$ids = $this->table->getConnection()->deleteCollection($this);
|
if ($conn == null) {
|
||||||
|
$conn = $this->table->getConnection();
|
||||||
|
}
|
||||||
|
$ids = $conn->deleteCollection($this);
|
||||||
$this->data = array();
|
$this->data = array();
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
@ -172,10 +172,10 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
|
|||||||
$class = $name."Table";
|
$class = $name."Table";
|
||||||
|
|
||||||
if(class_exists($class) && in_array("Doctrine_Table", class_parents($class))) {
|
if(class_exists($class) && in_array("Doctrine_Table", class_parents($class))) {
|
||||||
return new $class($name);
|
return new $class($name, $this);
|
||||||
} else {
|
} 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
|
* @var array $connections an array containing all the opened connections
|
||||||
*/
|
*/
|
||||||
private $connections = array();
|
private $connections = array();
|
||||||
|
/**
|
||||||
|
* @var array $dataSourceNames an array containing all available data source names
|
||||||
|
*/
|
||||||
|
private $dataSourceNames = array();
|
||||||
/**
|
/**
|
||||||
* @var integer $index the incremented index
|
* @var integer $index the incremented index
|
||||||
*/
|
*/
|
||||||
@ -194,12 +198,27 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
|
|||||||
* @return object Doctrine_Connection
|
* @return object Doctrine_Connection
|
||||||
* @throws InvalidKeyException
|
* @throws InvalidKeyException
|
||||||
*/
|
*/
|
||||||
public function getConnection($index) {
|
public function getConnection($name) {
|
||||||
if( ! isset($this->connections[$index]))
|
if (!isset($this->connections[$name])) {
|
||||||
throw new InvalidKeyException();
|
if (isset($this->dataSourceNames[$name])) {
|
||||||
|
$conn = Doctrine_DB::getConnection($this->dataSourceNames[$name]); // Establishes the connection
|
||||||
$this->currIndex = $index;
|
$this->openConnection($conn, $name);
|
||||||
return $this->connections[$index];
|
} 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); }
|
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
|
* @return void
|
||||||
*/
|
*/
|
||||||
final public function save() {
|
final public function save(Doctrine_Connection $conn = null) {
|
||||||
$this->table->getConnection()->beginTransaction();
|
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) {
|
foreach($saveLater as $fk) {
|
||||||
$table = $fk->getTable();
|
$table = $fk->getTable();
|
||||||
@ -847,7 +850,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
|
|||||||
|
|
||||||
$this->saveAssociations();
|
$this->saveAssociations();
|
||||||
|
|
||||||
$this->table->getConnection()->commit();
|
$conn->commit();
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* returns an array of modified fields and associated values
|
* 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
|
* @return boolean true on success, false on failure
|
||||||
*/
|
*/
|
||||||
public function delete() {
|
public function delete(Doctrine_Connection $conn = null) {
|
||||||
return $this->table->getConnection()->delete($this);
|
if ($conn == null) {
|
||||||
|
$conn = $this->table->getConnection();
|
||||||
|
}
|
||||||
|
return $conn->delete($this);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* returns a copy of this object
|
* 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
|
* @throws Doctrine_Table_Exception if there is already an instance of this table
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct($name) {
|
public function __construct($name, Doctrine_Connection $conn) {
|
||||||
$this->connection = Doctrine_Manager::getInstance()->getCurrentConnection();
|
$this->connection = $conn;
|
||||||
|
|
||||||
$this->setParent($this->connection);
|
$this->setParent($this->connection);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user