Commit 6fffa9e6 authored by zYne's avatar zYne

Manual codes updated Session -> Connection

parent acced2b9
......@@ -67,8 +67,15 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
/**
* constructor
*/
public function __construct(Doctrine_Table $table) {
*
* @param Doctrine_Table|string $table
*/
public function __construct($table) {
if( ! ($table instanceof Doctrine_Table))
$table = Doctrine_Manager::getInstance()
->getCurrentConnection()
->getTable($table);
$this->table = $table;
$name = $table->getAttribute(Doctrine::ATTR_COLL_KEY);
......
......@@ -22,13 +22,13 @@ $manager = Doctrine_Manager::getInstance();
$manager->setAttribute(Doctrine::ATTR_LISTENER,new MyListener());
// setting session level listener
$session = $manager->openSession($dbh);
// setting connection level listener
$conn = $manager->openConnection($dbh);
$session->setAttribute(Doctrine::ATTR_LISTENER,new MyListener2());
$conn->setAttribute(Doctrine::ATTR_LISTENER,new MyListener2());
// setting factory level listener
$table = $session->getTable("User");
$table = $conn->getTable("User");
$table->setAttribute(Doctrine::ATTR_LISTENER,new MyListener());
?>
......@@ -25,19 +25,19 @@ interface iDoctrine_EventListener {
public function onWakeUp(Doctrine_Record $record);
public function onClose(Doctrine_Session $session);
public function onPreClose(Doctrine_Session $session);
public function onClose(Doctrine_Connection $conn);
public function onPreClose(Doctrine_Connection $conn);
public function onOpen(Doctrine_Session $session);
public function onOpen(Doctrine_Connection $conn);
public function onTransactionCommit(Doctrine_Session $session);
public function onPreTransactionCommit(Doctrine_Session $session);
public function onTransactionCommit(Doctrine_Connection $conn);
public function onPreTransactionCommit(Doctrine_Connection $conn);
public function onTransactionRollback(Doctrine_Session $session);
public function onPreTransactionRollback(Doctrine_Session $session);
public function onTransactionRollback(Doctrine_Connection $conn);
public function onPreTransactionRollback(Doctrine_Connection $conn);
public function onTransactionBegin(Doctrine_Session $session);
public function onPreTransactionBegin(Doctrine_Session $session);
public function onTransactionBegin(Doctrine_Connection $conn);
public function onPreTransactionBegin(Doctrine_Connection $conn);
public function onCollectionDelete(Doctrine_Collection $collection);
public function onPreCollectionDelete(Doctrine_Collection $collection);
......
<?php
$table = $session->getTable("User");
$table = $conn->getTable("User");
$table->setEventListener(new MyListener2());
......
......@@ -17,7 +17,7 @@ class Email extends Doctrine_Record {
$this->hasColumn("address","string",150,"email|unique");
}
}
$session = Doctrine_Manager::getInstance()->openSession(new PDO("dsn","username","password"));
$conn = Doctrine_Manager::getInstance()->openConnection(new PDO("dsn","username","password"));
$user = new User();
$user->name = "this is an example of too long name";
......@@ -36,7 +36,7 @@ $user->Email->address = "drink@drinkmore.info";
$user->save(); // saved
$user = $session->create("User");
$user = $conn->create("User");
$user->Email->address = "drink@drinkmore.info"; // not unique!
$user->save(); // throws a Doctrine_Validator_Exception
?>
<?php
$table = $session->getTable("User");
$table = $conn->getTable("User");
$users = $table->findAll();
......
<?php
$table = $session->getTable("User");
$table = $conn->getTable("User");
$table->setAttribute(Doctrine::ATTR_FETCHMODE, Doctrine::FETCH_IMMEDIATE);
......@@ -7,7 +7,7 @@ $users = $table->findAll();
// or
$users = $session->query("FROM User-I"); // immediate collection
$users = $conn->query("FROM User-I"); // immediate collection
foreach($users as $user) {
print $user->name;
......@@ -20,7 +20,7 @@ $users = $table->findAll();
// or
$users = $session->query("FROM User-L"); // lazy collection
$users = $conn->query("FROM User-L"); // lazy collection
foreach($users as $user) {
print $user->name;
......@@ -32,7 +32,7 @@ $users = $table->findAll();
// or
$users = $session->query("FROM User-B"); // batch collection
$users = $conn->query("FROM User-B"); // batch collection
foreach($users as $user) {
print $user->name;
......@@ -44,7 +44,7 @@ $users = $table->findAll();
// or
$users = $session->query("FROM User-O"); // offset collection
$users = $conn->query("FROM User-O"); // offset collection
foreach($users as $user) {
print $user->name;
......
<?php
$users = $session->query("FROM User");
$users = $conn->query("FROM User");
// now lets load phonenumbers for all users
......
<?php
// Doctrine_Manager controls all the sessions
// Doctrine_Manager controls all the connections
$manager = Doctrine_Manager::getInstance();
// open first session
// open first connection
$session = $manager->openSession(new PDO("dsn","username","password"), "session 1");
$conn = $manager->openConnection(new PDO("dsn","username","password"), "connection 1");
// open second session
// open second connection
$session2 = $manager->openSession(new PDO("dsn2","username2","password2"), "session 2");
$conn2 = $manager->openConnection(new PDO("dsn2","username2","password2"), "connection 2");
$manager->getCurrentSession(); // $session2
$manager->getCurrentConnection(); // $conn2
$manager->setCurrentSession("session 1");
$manager->setCurrentConnection("connection 1");
$manager->getCurrentSession(); // $session
$manager->getCurrentConnection(); // $conn
// iterating through sessions
// iterating through connections
foreach($manager as $session) {
foreach($manager as $conn) {
}
?>
<?php
// Doctrine_Manager controls all the sessions
// Doctrine_Manager controls all the connections
$manager = Doctrine_Manager::getInstance();
// Doctrine_Session
// a script may have multiple open sessions
// Doctrine_Connection
// a script may have multiple open connections
// (= multiple database connections)
$dbh = new PDO("dsn","username","password");
$session = $manager->openSession();
$conn = $manager->openConnection();
// or if you want to use Doctrine Doctrine_DB and its
// performance monitoring capabilities
$dsn = "schema://username:password@dsn/dbname";
$dbh = Doctrine_DB::getConnection($dsn);
$session = $manager->openSession();
$conn = $manager->openConnection();
?>
......@@ -2,26 +2,26 @@
// find all users
$coll = $session->query("FROM User");
$coll = $conn->query("FROM User");
// find all users with only their names (and primary keys) fetched
$coll = $session->query("FROM User(name)");
$coll = $conn->query("FROM User(name)");
// find all groups
$coll = $session->query("FROM Group");
$coll = $conn->query("FROM Group");
// find all users and user emails
$coll = $session->query("FROM User.Email");
$coll = $conn->query("FROM User.Email");
// find all users and user emails with only user name and
// age + email address loaded
$coll = $session->query("FROM User(name, age).Email(address)");
$coll = $conn->query("FROM User(name, age).Email(address)");
// find all users, user email and user phonenumbers
$coll = $session->query("FROM User.Email, User.Phonenumber");
$coll = $conn->query("FROM User.Email, User.Phonenumber");
?>
<?php
// select all users and load the data directly (Immediate fetching strategy)
$coll = $session->query("FROM User-I");
$coll = $conn->query("FROM User-I");
// or
$coll = $session->query("FROM User-IMMEDIATE");
$coll = $conn->query("FROM User-IMMEDIATE");
// select all users and load the data in batches
$coll = $session->query("FROM User-B");
$coll = $conn->query("FROM User-B");
// or
$coll = $session->query("FROM User-BATCH");
$coll = $conn->query("FROM User-BATCH");
// select all user and use lazy fetching
$coll = $session->query("FROM User-L");
$coll = $conn->query("FROM User-L");
// or
$coll = $session->query("FROM User-LAZY");
$coll = $conn->query("FROM User-LAZY");
?>
......@@ -2,10 +2,10 @@
// find the first ten users and their emails
$coll = $session->query("FROM User, User.Email LIMIT 10");
$coll = $conn->query("FROM User, User.Email LIMIT 10");
// find the first ten users starting from the user number 5
$coll = $session->query("FROM User LIMIT 10 OFFSET 5");
$coll = $conn->query("FROM User LIMIT 10 OFFSET 5");
?>
......@@ -2,5 +2,5 @@
// retrieve all users with only their properties id and name loaded
$users = $session->query("FROM User(id, name)");
$users = $conn->query("FROM User(id, name)");
?>
<?php
$session = Doctrine_Manager::getInstance()->openSession(new PDO("dsn","username","password"));
$conn = Doctrine_Manager::getInstance()->openConnection(new PDO("dsn","username","password"));
$query = new Doctrine_Query($session);
$query = new Doctrine_Query($conn);
$query->from("User-b")
->where("User.name LIKE 'Jack%'")
......
<?php
// find all users, sort by name descending
$coll = $session->query("FROM User ORDER BY User.name DESC");
$coll = $conn->query("FROM User ORDER BY User.name DESC");
// find all users sort by name ascending
$coll = $session->query("FROM User ORDER BY User.name ASC");
$coll = $conn->query("FROM User ORDER BY User.name ASC");
// or
$coll = $session->query("FROM User ORDER BY User.name");
$coll = $conn->query("FROM User ORDER BY User.name");
// find all users and their emails, sort by email address
$coll = $session->query("FROM User, User.Email ORDER BY User.Email.address");
$coll = $conn->query("FROM User, User.Email ORDER BY User.Email.address");
// find all users and their emails, sort by user name and email address
$coll = $session->query("FROM User, User.Email ORDER BY User.name, User.Email.address");
$coll = $conn->query("FROM User, User.Email ORDER BY User.name, User.Email.address");
?>
......@@ -4,24 +4,24 @@
// find all groups where the group primary key is bigger than 10
$coll = $session->query("FROM Group WHERE Group.id > 10");
$coll = $conn->query("FROM Group WHERE Group.id > 10");
// find all users where users where user name matches a regular expression,
// REGEXP keyword must be supported by the underlying database
$coll = $session->query("FROM User WHERE User.name REGEXP '[ad]'");
$coll = $conn->query("FROM User WHERE User.name REGEXP '[ad]'");
// find all users and their associated emails where SOME of the users phonenumbers
// (the association between user and phonenumber tables is Many-To-Many) starts with 123
$coll = $session->query("FROM User, User.Email WHERE User.Phonenumber.phonenumber LIKE '123%'");
$coll = $conn->query("FROM User, User.Email WHERE User.Phonenumber.phonenumber LIKE '123%'");
// multiple conditions
$coll = $session->query("FROM User WHERE User.name LIKE '%Jack%' && User.Email.address LIKE '%@drinkmore.info'");
$coll = $conn->query("FROM User WHERE User.name LIKE '%Jack%' && User.Email.address LIKE '%@drinkmore.info'");
// nesting conditions
$coll = $session->query("FROM User WHERE (User.name LIKE '%Jack%' || User.name LIKE '%John%') && User.Email.address LIKE '%@drinkmore.info'");
$coll = $conn->query("FROM User WHERE (User.name LIKE '%Jack%' || User.name LIKE '%John%') && User.Email.address LIKE '%@drinkmore.info'");
?>
<?php
$query = new Doctrine_RawSql($session);
$query = new Doctrine_RawSql($conn);
$query->parseQuery("SELECT {entity.*}, {phonenumber.*}
FROM entity
......
<?php
$query = new Doctrine_RawSql($session);
$query = new Doctrine_RawSql($conn);
$query->select('{entity.name}')
->from('entity');
......
<?php
$query = new Doctrine_RawSql($session);
$query = new Doctrine_RawSql($conn);
$query->parseQuery("SELECT {entity.name} FROM entity");
......
<?php
$user = $session->create("User");
$user = $conn->create("User");
// alternative way:
$table = $session->getTable("User");
$table = $conn->getTable("User");
$user = $table->create();
......
<?php
$table = $session->getTable("User");
$table = $conn->getTable("User");
$user = $table->find(2);
......
<?php
$table = $session->getTable("User");
$table = $conn->getTable("User");
// find by primary key
......@@ -19,5 +19,5 @@ foreach($table->findByDql("name LIKE '%John%'") as $user) {
// finding objects with DQL
$users = $session->query("FROM User WHERE User.name LIKE '%John%'");
$users = $conn->query("FROM User WHERE User.name LIKE '%John%'");
?>
<?php
$table = $session->getTable("User");
$table = $conn->getTable("User");
$user = $table->find(2);
......
......@@ -2,10 +2,10 @@
$user = new User();
$user->name = "Jack";
$group = $session->create("Group");
$group = $conn->create("Group");
$group->name = "Drinking Club";
// saves all the changed objects into database
$session->flush();
$conn->flush();
?>
<?php
$manager = Doctrine_Manager::getInstance();
// open new session
// open new connection
$session = $manager->openSession(new PDO("dsn","username","password"));
$conn = $manager->openConnection(new PDO("dsn","username","password"));
// getting a table object
$table = $session->getTable("User");
$table = $conn->getTable("User");
?>
<?php
switch($session->getState())
case Doctrine_Session::STATE_ACTIVE:
// session open and zero open transactions
switch($conn->getState())
case Doctrine_Connection::STATE_ACTIVE:
// connection open and zero open transactions
break;
case Doctrine_Session::STATE_ACTIVE:
case Doctrine_Connection::STATE_ACTIVE:
// one open transaction
break;
case Doctrine_Session::STATE_BUSY:
case Doctrine_Connection::STATE_BUSY:
// multiple open transactions
break;
case Doctrine_Session::STATE_CLOSED:
// session closed
case Doctrine_Connection::STATE_CLOSED:
// connection closed
break;
endswitch;
?>
......@@ -2,9 +2,9 @@
// select all users
$session->query("FROM User");
$conn->query("FROM User");
// select all users where user email is jackdaniels@drinkmore.info
$session->query("FROM User WHERE User.Email.address = 'jackdaniels@drinkmore.info'");
$conn->query("FROM User WHERE User.Email.address = 'jackdaniels@drinkmore.info'");
?>
......@@ -4,18 +4,18 @@ class UserTable extends Doctrine_Table {
* you can add your own finder methods here
*/
public function findByName($name) {
return $this->getSession()->query("FROM User WHERE name LIKE '%$name%'");
return $this->getConnection()->query("FROM User WHERE name LIKE '%$name%'");
}
}
class User extends Doctrine_Record { }
$session = Doctrine_Manager::getInstance()
->openSession(new PDO("dsn","username","password"));
$conn = Doctrine_Manager::getInstance()
->openConnection(new PDO("dsn","username","password"));
// doctrine will now check if a class called UserTable exists
// and if it inherits Doctrine_Table
$table = $session->getTable("User");
$table = $conn->getTable("User");
print get_class($table); // UserTable
......
<?php
$table = $session->getTable("User");
$table = $conn->getTable("User");
// find by primary key
......
<?php
$table = $session->getTable('User');
$table = $conn->getTable('User');
// getting column names
......
<?php
$sess = Doctrine_Manager::getInstance()->openSession(new PDO("dsn","username","password"));
$sess = Doctrine_Manager::getInstance()->openConnection(new PDO("dsn","username","password"));
// select first ten rows starting from the row 20
......
<?php
try {
$session->beginTransaction();
$conn->beginTransaction();
$user->save();
$session->beginTransaction();
$conn->beginTransaction();
$group->save();
$email->save();
$session->commit();
$conn->commit();
$session->commit();
$conn->commit();
} catch(Exception $e) {
$session->rollback();
$conn->rollback();
}
?>
......@@ -2,7 +2,7 @@
// works only if you use doctrine database handler
$dbh = $session->getDBH();
$dbh = $conn->getDBH();
$times = $dbh->getExecTimes();
......
<?php
$sess = Doctrine_Manager::getInstance()->openSession(new PDO("dsn","username","password"));
$sess = Doctrine_Manager::getInstance()->openConnection(new PDO("dsn","username","password"));
// gets the next ID from a sequence
......
<?php
$sess = Doctrine_Manager::getInstance()->openSession(new PDO("dsn","username","password"));
$sess = Doctrine_Manager::getInstance()->openConnection(new PDO("dsn","username","password"));
try {
$sess->beginTransaction();
......
......@@ -39,7 +39,7 @@ $user->save();
$user->Groupuser->delete();
$groups = new Doctrine_Collection($session->getTable("Group"));
$groups = new Doctrine_Collection($conn->getTable("Group"));
$groups[0]->name = "Third Group";
......
......@@ -5,7 +5,7 @@
*/
$users->delete();
/**
* On session drivers other than mysql doctrine would now perform three queries
* On connection drivers other than mysql doctrine would now perform three queries
* regardless of how many users, emails and phonenumbers there are
*
* the queries would look something like:
......
......@@ -62,7 +62,7 @@ class EntityListener extends Doctrine_EventListener {
$manager = Doctrine_Manager::getInstance();
$session = $manager->openSession(new PDO("DSN","username","password"));
$conn = $manager->openConnection(new PDO("DSN","username","password"));
$user = new User();
......
<?php
$sess = $manager->openSession(Doctrine_DB::getConnection("schema://username:password@hostname/database"));
$sess = $manager->openConnection(Doctrine_DB::getConnection("schema://username:password@hostname/database"));
// get session state:
// get connection state:
switch($sess):
case Doctrine_Session::STATE_BUSY:
case Doctrine_Connection::STATE_BUSY:
// multiple open transactions
break;
case Doctrine_Session::STATE_ACTIVE:
case Doctrine_Connection::STATE_ACTIVE:
// one open transaction
break;
case Doctrine_Session::STATE_CLOSED:
case Doctrine_Connection::STATE_CLOSED:
// closed state
break;
case Doctrine_Session::STATE_OPEN:
case Doctrine_Connection::STATE_OPEN:
// open state and zero open transactions
break;
endswitch;
......@@ -21,10 +21,10 @@ endswitch;
$dbh = $sess->getDBH();
// flushing the session
// flushing the connection
$sess->flush();
// print lots of useful info about session:
// print lots of useful info about connection:
print $sess;
?>
<?php
$session->beginTransaction();
$conn->beginTransaction();
$user = new User();
$user->name = 'New user';
$user->save();
$user = $session->getTable('User')->find(5);
$user = $conn->getTable('User')->find(5);
$user->name = 'Modified user';
$user->save();
$session->commit(); // all the queries are executed here
$conn->commit(); // all the queries are executed here
?>
<?php
function saveUserAndGroup(Doctrine_Session $session, User $user, Group $group) {
$session->beginTransaction();
function saveUserAndGroup(Doctrine_Connection $conn, User $user, Group $group) {
$conn->beginTransaction();
$user->save();
$group->save();
$session->commit();
$conn->commit();
}
try {
$session->beginTransaction();
$conn->beginTransaction();
saveUserAndGroup($session,$user,$group);
saveUserAndGroup($session,$user2,$group2);
saveUserAndGroup($session,$user3,$group3);
saveUserAndGroup($conn,$user,$group);
saveUserAndGroup($conn,$user2,$group2);
saveUserAndGroup($conn,$user3,$group3);
$session->commit();
$conn->commit();
} catch(Doctrine_Exception $e) {
$session->rollback();
$conn->rollback();
}
?>
<?php
$session->beginTransaction();
$conn->beginTransaction();
$user = new User();
$user->name = 'New user';
$user->save();
$user = $session->getTable('User')->find(5);
$user = $conn->getTable('User')->find(5);
$user->name = 'Modified user';
$user->save();
$pending = $session->getInserts(); // an array containing one element
$pending = $conn->getInserts(); // an array containing one element
$pending = $session->getUpdates(); // an array containing one element
$pending = $conn->getUpdates(); // an array containing one element
$session->commit(); // all the queries are executed here
$conn->commit(); // all the queries are executed here
?>
......@@ -210,6 +210,5 @@ class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase {
$this->assertEqual($q->getQuery(),
"SELECT photo.id AS photo__id, photo.name AS photo__name FROM photo LEFT JOIN phototag ON photo.id = phototag.photo_id LEFT JOIN tag ON tag.id = phototag.tag_id WHERE photo.id IN (SELECT DISTINCT photo.id FROM photo LEFT JOIN phototag ON photo.id = phototag.photo_id LEFT JOIN tag ON tag.id = phototag.tag_id WHERE tag.id = ? LIMIT 100) AND tag.id = ? ORDER BY photo.id DESC");
}
}
?>
......@@ -28,7 +28,7 @@ require_once("QueryLimitTestCase.php");
error_reporting(E_ALL);
$test = new GroupTest("Doctrine Framework Unit Tests");
/**
$test->addTestCase(new Doctrine_RecordTestCase());
$test->addTestCase(new Doctrine_SessionTestCase());
......@@ -66,7 +66,7 @@ $test->addTestCase(new Doctrine_CollectionTestCase());
$test->addTestCase(new Doctrine_QueryTestCase());
$test->addTestCase(new Doctrine_RawSql_TestCase());
*/
$test->addTestCase(new Doctrine_Query_Limit_TestCase());
//$test->addTestCase(new Doctrine_Cache_FileTestCase());
......
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