From 09d249e5492027aacb0c9f073c0ab0508e66cec5 Mon Sep 17 00:00:00 2001 From: romanb Date: Fri, 22 Sep 2006 19:23:57 +0000 Subject: [PATCH] - 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 --- Doctrine/Collection.php | 14 ++++++++++---- Doctrine/Connection.php | 4 ++-- Doctrine/Manager.php | 31 +++++++++++++++++++++++++------ Doctrine/Manager/Exception.php | 5 +++++ Doctrine/Record.php | 20 +++++++++++++------- Doctrine/Table.php | 4 ++-- 6 files changed, 57 insertions(+), 21 deletions(-) create mode 100644 Doctrine/Manager/Exception.php diff --git a/Doctrine/Collection.php b/Doctrine/Collection.php index 527a82319..bce015796 100644 --- a/Doctrine/Collection.php +++ b/Doctrine/Collection.php @@ -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(); } /** diff --git a/Doctrine/Connection.php b/Doctrine/Connection.php index 9d15ee2b5..29267765a 100644 --- a/Doctrine/Connection.php +++ b/Doctrine/Connection.php @@ -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); } } /** diff --git a/Doctrine/Manager.php b/Doctrine/Manager.php index 9a1a32a26..2787c973e 100644 --- a/Doctrine/Manager.php +++ b/Doctrine/Manager.php @@ -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); } diff --git a/Doctrine/Manager/Exception.php b/Doctrine/Manager/Exception.php new file mode 100644 index 000000000..af3ca2ca4 --- /dev/null +++ b/Doctrine/Manager/Exception.php @@ -0,0 +1,5 @@ + \ No newline at end of file diff --git a/Doctrine/Record.php b/Doctrine/Record.php index e5c3170ae..d9cae3121 100644 --- a/Doctrine/Record.php +++ b/Doctrine/Record.php @@ -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 diff --git a/Doctrine/Table.php b/Doctrine/Table.php index cf2f08b91..0f59bb9d0 100644 --- a/Doctrine/Table.php +++ b/Doctrine/Table.php @@ -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);