From c9b90179855bf6015b01048186e77ade1d50d7e2 Mon Sep 17 00:00:00 2001 From: zYne Date: Thu, 17 Aug 2006 09:42:18 +0000 Subject: [PATCH] Docs updated, more tests for DQL LIMIT --- Doctrine/Query.php | 6 +++--- Doctrine/Table.php | 4 ++++ ...ponents - Collection - Deleting collection.php | 2 +- ...mponents - Query - FROM - selecting tables.php | 14 +++++++++++++- ...sic Components - Record - Deleting records.php | 10 +++------- ...nts - Record - Retrieving existing records.php | 13 ++++++------- ...sic Components - Record - Updating records.php | 15 +++++++-------- .../Basic Components - Table - Custom finders.php | 6 ++++-- .../Basic Components - Table - Finder methods.php | 15 ++++++++------- manual/documentation.php | 8 +++----- tests/QueryLimitTestCase.php | 5 +++++ 11 files changed, 57 insertions(+), 41 deletions(-) diff --git a/Doctrine/Query.php b/Doctrine/Query.php index 806c10b1a..d48f4567a 100644 --- a/Doctrine/Query.php +++ b/Doctrine/Query.php @@ -247,7 +247,7 @@ class Doctrine_Query extends Doctrine_Hydrate { * * @return string */ - final public function getQuery() { + public function getQuery() { if(empty($this->parts["select"]) || empty($this->parts["from"])) return false; @@ -299,10 +299,10 @@ class Doctrine_Query extends Doctrine_Hydrate { $this->parts['where'][] = '('.$string.')'; if($needsSubQuery) { + // all conditions must be preserved in subquery $subquery .= ( ! empty($this->parts['where']))?" WHERE ".implode(" AND ",$this->parts["where"]):''; $subquery .= ( ! empty($this->parts['groupby']))?" GROUP BY ".implode(", ",$this->parts["groupby"]):''; $subquery .= ( ! empty($this->parts['having']))?" HAVING ".implode(" ",$this->parts["having"]):''; - $subquery .= ( ! empty($this->parts['orderby']))?" ORDER BY ".implode(" ",$this->parts["orderby"]):''; } $modifyLimit = false; @@ -313,7 +313,7 @@ class Doctrine_Query extends Doctrine_Hydrate { $field = $table->getTableName().'.'.$table->getIdentifier(); array_unshift($this->parts['where'], $field.' IN ('.$subquery.')'); } else - $modifyLimit = true; + $modifyLimit = true; } $q .= ( ! empty($this->parts['where']))?" WHERE ".implode(" AND ",$this->parts["where"]):''; diff --git a/Doctrine/Table.php b/Doctrine/Table.php index 9eaaca358..f210149be 100644 --- a/Doctrine/Table.php +++ b/Doctrine/Table.php @@ -734,6 +734,10 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { $users = $q->query("FROM ".$this->name." WHERE ".$dql, $params); return $users; } + + public function findByDql($dql, array $params = array()) { + return $this->findBySql($dql, $params); + } /** * clear * clears the first level cache (identityMap) diff --git a/manual/codes/Basic Components - Collection - Deleting collection.php b/manual/codes/Basic Components - Collection - Deleting collection.php index d4f288459..125cfba6a 100644 --- a/manual/codes/Basic Components - Collection - Deleting collection.php +++ b/manual/codes/Basic Components - Collection - Deleting collection.php @@ -1,7 +1,7 @@ findBySql("name LIKE '%John%'"); +$users = $table->findByDql("name LIKE '%John%'"); $users->delete(); ?> diff --git a/manual/codes/Basic Components - Query - FROM - selecting tables.php b/manual/codes/Basic Components - Query - FROM - selecting tables.php index ebaafeda8..6a8001203 100644 --- a/manual/codes/Basic Components - Query - FROM - selecting tables.php +++ b/manual/codes/Basic Components - Query - FROM - selecting tables.php @@ -4,12 +4,24 @@ $coll = $session->query("FROM User"); +// find all users with only their names (and primary keys) fetched + +$coll = $session->query("FROM User(name)"); + // find all groups $coll = $session->query("FROM Group"); // find all users and user emails -$coll = $session->query("FROM User, User.Email"); +$coll = $session->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)"); + +// find all users, user email and user phonenumbers + +$coll = $session->query("FROM User.Email, User.Phonenumber"); ?> diff --git a/manual/codes/Basic Components - Record - Deleting records.php b/manual/codes/Basic Components - Record - Deleting records.php index 5cf6a1630..f486f8be3 100644 --- a/manual/codes/Basic Components - Record - Deleting records.php +++ b/manual/codes/Basic Components - Record - Deleting records.php @@ -1,15 +1,11 @@ getTable("User"); -try { - $user = $table->find(2); -} catch(Doctrine_Find_Exception $e) { - print "Couldn't find user"; -} +$user = $table->find(2); // deletes user and all related composite objects - -$user->delete(); +if($user !== false) + $user->delete(); $users = $table->findAll(); diff --git a/manual/codes/Basic Components - Record - Retrieving existing records.php b/manual/codes/Basic Components - Record - Retrieving existing records.php index cee6a20fb..9bdf929ee 100644 --- a/manual/codes/Basic Components - Record - Retrieving existing records.php +++ b/manual/codes/Basic Components - Record - Retrieving existing records.php @@ -2,19 +2,18 @@ $table = $session->getTable("User"); // find by primary key -try { - $user = $table->find(2); -} catch(Doctrine_Find_Exception $e) { - print "Couldn't find user"; -} + +$user = $table->find(2); +if($user !== false) + print $user->name; // get all users foreach($table->findAll() as $user) { print $user->name; } -// finding by sql -foreach($table->findBySql("name LIKE '%John%'") as $user) { +// finding by dql +foreach($table->findByDql("name LIKE '%John%'") as $user) { print $user->created; } diff --git a/manual/codes/Basic Components - Record - Updating records.php b/manual/codes/Basic Components - Record - Updating records.php index b5d318364..24026fec3 100644 --- a/manual/codes/Basic Components - Record - Updating records.php +++ b/manual/codes/Basic Components - Record - Updating records.php @@ -1,13 +1,12 @@ getTable("User"); -try { - $user = $table->find(2); -} catch(Doctrine_Find_Exception $e) { - print "Couldn't find user"; + +$user = $table->find(2); + +if($user !== false) { + $user->name = "Jack Daniels"; + + $user->save(); } - -$user->name = "Jack Daniels"; - -$user->save(); ?> diff --git a/manual/codes/Basic Components - Table - Custom finders.php b/manual/codes/Basic Components - Table - Custom finders.php index 50e0f6732..1a040f15a 100644 --- a/manual/codes/Basic Components - Table - Custom finders.php +++ b/manual/codes/Basic Components - Table - Custom finders.php @@ -9,9 +9,11 @@ class UserTable extends Doctrine_Table { } class User extends Doctrine_Record { } -$session = Doctrine_Manager::getInstance()->openSession(new PDO("dsn","username","password")); +$session = Doctrine_Manager::getInstance() + ->openSession(new PDO("dsn","username","password")); -// doctrine will now check if a class called UserTable exists and if it inherits Doctrine_Table +// doctrine will now check if a class called UserTable exists +// and if it inherits Doctrine_Table $table = $session->getTable("User"); diff --git a/manual/codes/Basic Components - Table - Finder methods.php b/manual/codes/Basic Components - Table - Finder methods.php index a78103611..380e6b48d 100644 --- a/manual/codes/Basic Components - Table - Finder methods.php +++ b/manual/codes/Basic Components - Table - Finder methods.php @@ -2,19 +2,20 @@ $table = $session->getTable("User"); // find by primary key -try { - $user = $table->find(2); -} catch(Doctrine_Find_Exception $e) { - print "Couldn't find user"; -} + +$user = $table->find(2); + +if($user !== false) + print $user->name; + // get all users foreach($table->findAll() as $user) { print $user->name; } -// finding by sql -foreach($table->findBySql("name LIKE '%John%'") as $user) { +// finding by dql +foreach($table->findByDql("name LIKE '%John%'") as $user) { print $user->created; } ?> diff --git a/manual/documentation.php b/manual/documentation.php index 745e9ead4..c4bcc90e1 100644 --- a/manual/documentation.php +++ b/manual/documentation.php @@ -114,14 +114,12 @@ $menu = array("Getting started" => "Getting record state", "Getting object copy", "Serializing", - "Getting identifiers", "Callbacks"), "Session" => array("Introduction", "Availible drivers", "Getting a table object", "Flushing the session", - "Limiting queries", "Querying the database", "Getting session state"), @@ -132,7 +130,7 @@ $menu = array("Getting started" => "Getting collection count", "Saving the collection", "Deleting collection", - "Fetching strategies", + //"Fetching strategies", "Loading related records", "Collection expanding", ), @@ -149,8 +147,8 @@ $menu = array("Getting started" => "LIMIT and OFFSET - limiting the query results", "WHERE - setting query conditions", "ORDER BY - sorting query results", - "Fetching strategies", - "Lazy property fetching", + //"Fetching strategies", + //"Lazy property fetching", "Method overloading", "Relation operators", "Bound parameters", diff --git a/tests/QueryLimitTestCase.php b/tests/QueryLimitTestCase.php index f2281e765..7990944f1 100644 --- a/tests/QueryLimitTestCase.php +++ b/tests/QueryLimitTestCase.php @@ -7,6 +7,7 @@ class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase { $users = $q->execute(); $this->assertEqual($users->count(), 5); + $this->assertEqual($q->getQuery(), "SELECT entity.id AS entity__id, email.id AS email__id, email.address AS email__address FROM entity LEFT JOIN email ON entity.email_id = email.id WHERE (entity.type = 0) LIMIT 5"); } public function testLimitWithOneToOneInnerJoin() { @@ -15,6 +16,7 @@ class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase { $users = $q->execute(); $this->assertEqual($users->count(), 5); + $this->assertEqual($q->getQuery(), "SELECT entity.id AS entity__id, email.id AS email__id, email.address AS email__address FROM entity INNER JOIN email ON entity.email_id = email.id WHERE (entity.type = 0) LIMIT 5"); } public function testLimitWithOneToManyLeftJoin() { $this->query->from("User(id).Phonenumber"); @@ -98,6 +100,9 @@ class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase { public function testLimitWithManyToManyLeftJoin() { $q = new Doctrine_Query($this->session); $q->from("User.Group")->limit(5); + $users = $q->execute(); + + $this->assertEqual($users->count(), 5); } }