Commit 33d8f27e authored by zYne's avatar zYne

Started seamless changing of the name Session to Connection

parent c9b90179
...@@ -30,9 +30,9 @@ require_once("EventListener.php"); ...@@ -30,9 +30,9 @@ require_once("EventListener.php");
*/ */
class Doctrine_Manager extends Doctrine_Configurable implements Countable, IteratorAggregate { class Doctrine_Manager extends Doctrine_Configurable implements Countable, IteratorAggregate {
/** /**
* @var array $session an array containing all the opened sessions * @var array $connections an array containing all the opened connections
*/ */
private $sessions = array(); private $connections = array();
/** /**
* @var integer $index the incremented index * @var integer $index the incremented index
*/ */
...@@ -143,20 +143,20 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera ...@@ -143,20 +143,20 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
$this->attributes[Doctrine::ATTR_CREATE_TABLES] = $old; $this->attributes[Doctrine::ATTR_CREATE_TABLES] = $old;
} }
/** /**
* openSession * openConnection
* opens a new session and saves it to Doctrine_Manager->sessions * opens a new connection and saves it to Doctrine_Manager->sessions
* *
* @param PDO $pdo PDO database driver * @param PDO $pdo PDO database driver
* @param string $name name of the session, if empty numeric key is used * @param string $name name of the connection, if empty numeric key is used
* @return Doctrine_Session * @return Doctrine_Session
*/ */
final public function openSession(PDO $pdo, $name = null) { public function openConnection(PDO $pdo, $name = null) {
// initialize the default attributes // initialize the default attributes
$this->setDefaultAttributes(); $this->setDefaultAttributes();
if($name !== null) { if($name !== null) {
$name = (string) $name; $name = (string) $name;
if(isset($this->sessions[$name])) if(isset($this->connections[$name]))
throw new InvalidKeyException(); throw new InvalidKeyException();
} else { } else {
...@@ -165,31 +165,34 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera ...@@ -165,31 +165,34 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
} }
switch($pdo->getAttribute(PDO::ATTR_DRIVER_NAME)): switch($pdo->getAttribute(PDO::ATTR_DRIVER_NAME)):
case "mysql": case "mysql":
$this->sessions[$name] = new Doctrine_Session_Mysql($this,$pdo); $this->connections[$name] = new Doctrine_Session_Mysql($this,$pdo);
break; break;
case "sqlite": case "sqlite":
$this->sessions[$name] = new Doctrine_Session_Sqlite($this,$pdo); $this->connections[$name] = new Doctrine_Session_Sqlite($this,$pdo);
break; break;
case "pgsql": case "pgsql":
$this->sessions[$name] = new Doctrine_Session_Pgsql($this,$pdo); $this->connections[$name] = new Doctrine_Session_Pgsql($this,$pdo);
break; break;
case "oci": case "oci":
$this->sessions[$name] = new Doctrine_Session_Oracle($this,$pdo); $this->connections[$name] = new Doctrine_Session_Oracle($this,$pdo);
break; break;
case "mssql": case "mssql":
$this->sessions[$name] = new Doctrine_Session_Mssql($this,$pdo); $this->connections[$name] = new Doctrine_Session_Mssql($this,$pdo);
break; break;
case "firebird": case "firebird":
$this->sessions[$name] = new Doctrine_Session_Firebird($this,$pdo); $this->connections[$name] = new Doctrine_Session_Firebird($this,$pdo);
break; break;
case "informix": case "informix":
$this->sessions[$name] = new Doctrine_Session_Informix($this,$pdo); $this->connections[$name] = new Doctrine_Session_Informix($this,$pdo);
break; break;
endswitch; endswitch;
$this->currIndex = $name; $this->currIndex = $name;
return $this->sessions[$name]; return $this->connections[$name];
}
public function openSession(PDO $pdo, $name = null) {
return $this->openConnection($pdo, $name);
} }
/** /**
* getSession * getSession
...@@ -197,47 +200,56 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera ...@@ -197,47 +200,56 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
* @return object Doctrine_Session * @return object Doctrine_Session
* @throws InvalidKeyException * @throws InvalidKeyException
*/ */
final public function getSession($index) { public function getConnection($index) {
if( ! isset($this->sessions[$index])) if( ! isset($this->connections[$index]))
throw new InvalidKeyException(); throw new InvalidKeyException();
$this->currIndex = $index; $this->currIndex = $index;
return $this->sessions[$index]; return $this->connections[$index];
} }
public function getSession($index) { return $this->getConnection($index); }
/** /**
* closes the session * closes the session
* *
* @param Doctrine_Session $session * @param Doctrine_Session $session
* @return void * @return void
*/ */
final public function closeSession(Doctrine_Session $session) { public function closeConnection(Doctrine_Session $session) {
$session->close(); $session->close();
unset($session); unset($session);
} }
public function closeSession(Doctrine_Session $session) { $this->closeConnection($session); }
/** /**
* getSessions * getConnections
* returns all opened sessions * returns all opened connections
* *
* @return array * @return array
*/ */
final public function getSessions() { public function getConnections() {
return $this->sessions; return $this->connections;
}
public function getSessions() {
return $this->connections;
} }
/** /**
* setCurrentSession * setCurrentConnection
* sets the current session to $key * sets the current connection to $key
* *
* @param mixed $key the session key * @param mixed $key the connection key
* @throws InvalidKeyException * @throws InvalidKeyException
* @return void * @return void
*/ */
final public function setCurrentSession($key) { public function setCurrentConnection($key) {
$key = (string) $key; $key = (string) $key;
if( ! isset($this->sessions[$key])) if( ! isset($this->connections[$key]))
throw new InvalidKeyException(); throw new InvalidKeyException();
$this->currIndex = $key; $this->currIndex = $key;
} }
public function setCurrentSession($key) {
$this->setCurrentConnection($key);
}
/** /**
* count * count
* returns the number of opened sessions * returns the number of opened sessions
...@@ -245,7 +257,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera ...@@ -245,7 +257,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
* @return integer * @return integer
*/ */
public function count() { public function count() {
return count($this->sessions); return count($this->connections);
} }
/** /**
* getIterator * getIterator
...@@ -254,22 +266,23 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera ...@@ -254,22 +266,23 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
* @return ArrayIterator * @return ArrayIterator
*/ */
public function getIterator() { public function getIterator() {
return new ArrayIterator($this->sessions); return new ArrayIterator($this->connections);
} }
/** /**
* getCurrentSession * getCurrentConnection
* returns the current session * returns the current connection
* *
* @throws Doctrine_Session_Exception if there are no open sessions * @throws Doctrine_Session_Exception if there are no open sessions
* @return Doctrine_Session * @return Doctrine_Session
*/ */
final public function getCurrentSession() { public function getCurrentConnection() {
$i = $this->currIndex; $i = $this->currIndex;
if( ! isset($this->sessions[$i])) if( ! isset($this->connections[$i]))
throw new Doctrine_Session_Exception(); throw new Doctrine_Session_Exception();
return $this->sessions[$i]; return $this->connections[$i];
} }
public function getCurrentSession() { return $this->getCurrentConnection(); }
/** /**
* __toString * __toString
* returns a string representation of this object * returns a string representation of this object
...@@ -279,7 +292,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera ...@@ -279,7 +292,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
public function __toString() { public function __toString() {
$r[] = "<pre>"; $r[] = "<pre>";
$r[] = "Doctrine_Manager"; $r[] = "Doctrine_Manager";
$r[] = "Sessions : ".count($this->sessions); $r[] = "Sessions : ".count($this->connections);
$r[] = "</pre>"; $r[] = "</pre>";
return implode("\n",$r); return implode("\n",$r);
} }
......
<?php
class Article extends Doctrine_Record {
public function setTableDefinition() {
$this->hasColumn("title","string", 200);
// maps to TINYINT on mysql
$this->hasColumn("section", "enum", 2);
$this->setEnumValues("section", array("PHP","Python","Java","Ruby"));
}
}
$article = new Article;
$article->title = 'My first php article';
// doctrine auto-converts the section to integer when the
// record is being saved
$article->section = 'PHP';
$article->save();
// on insert query with values 'My first php article' and 0
// would be issued
?>
Doctrine has drivers for every PDO-supported database. The supported databases are:
<li>FreeTDS / Microsoft SQL Server / Sybase
<li>Firebird/Interbase 6
<li>Informix
<li>Mysql
<li>Oracle
<li>Odbc
<li>PostgreSQL
<li>Sqlite
Doctrine offers enum data type emulation for all databases. The enum data type of Doctrine maps to
integer on database. Doctrine takes care of converting the enumerated value automatically to its valuelist equivalent when a record is being fetched
and the valuelist value back to its enumerated equivalent when record is being saved.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment