Commit c9b90179 authored by zYne's avatar zYne

Docs updated, more tests for DQL LIMIT

parent 75a2dea3
...@@ -247,7 +247,7 @@ class Doctrine_Query extends Doctrine_Hydrate { ...@@ -247,7 +247,7 @@ class Doctrine_Query extends Doctrine_Hydrate {
* *
* @return string * @return string
*/ */
final public function getQuery() { public function getQuery() {
if(empty($this->parts["select"]) || empty($this->parts["from"])) if(empty($this->parts["select"]) || empty($this->parts["from"]))
return false; return false;
...@@ -299,10 +299,10 @@ class Doctrine_Query extends Doctrine_Hydrate { ...@@ -299,10 +299,10 @@ class Doctrine_Query extends Doctrine_Hydrate {
$this->parts['where'][] = '('.$string.')'; $this->parts['where'][] = '('.$string.')';
if($needsSubQuery) { if($needsSubQuery) {
// all conditions must be preserved in subquery
$subquery .= ( ! empty($this->parts['where']))?" WHERE ".implode(" AND ",$this->parts["where"]):''; $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['groupby']))?" GROUP BY ".implode(", ",$this->parts["groupby"]):'';
$subquery .= ( ! empty($this->parts['having']))?" HAVING ".implode(" ",$this->parts["having"]):''; $subquery .= ( ! empty($this->parts['having']))?" HAVING ".implode(" ",$this->parts["having"]):'';
$subquery .= ( ! empty($this->parts['orderby']))?" ORDER BY ".implode(" ",$this->parts["orderby"]):'';
} }
$modifyLimit = false; $modifyLimit = false;
...@@ -313,7 +313,7 @@ class Doctrine_Query extends Doctrine_Hydrate { ...@@ -313,7 +313,7 @@ class Doctrine_Query extends Doctrine_Hydrate {
$field = $table->getTableName().'.'.$table->getIdentifier(); $field = $table->getTableName().'.'.$table->getIdentifier();
array_unshift($this->parts['where'], $field.' IN ('.$subquery.')'); array_unshift($this->parts['where'], $field.' IN ('.$subquery.')');
} else } else
$modifyLimit = true; $modifyLimit = true;
} }
$q .= ( ! empty($this->parts['where']))?" WHERE ".implode(" AND ",$this->parts["where"]):''; $q .= ( ! empty($this->parts['where']))?" WHERE ".implode(" AND ",$this->parts["where"]):'';
......
...@@ -734,6 +734,10 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { ...@@ -734,6 +734,10 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
$users = $q->query("FROM ".$this->name." WHERE ".$dql, $params); $users = $q->query("FROM ".$this->name." WHERE ".$dql, $params);
return $users; return $users;
} }
public function findByDql($dql, array $params = array()) {
return $this->findBySql($dql, $params);
}
/** /**
* clear * clear
* clears the first level cache (identityMap) * clears the first level cache (identityMap)
......
<?php <?php
// delete all users with name 'John' // delete all users with name 'John'
$users = $table->findBySql("name LIKE '%John%'"); $users = $table->findByDql("name LIKE '%John%'");
$users->delete(); $users->delete();
?> ?>
...@@ -4,12 +4,24 @@ ...@@ -4,12 +4,24 @@
$coll = $session->query("FROM User"); $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 // find all groups
$coll = $session->query("FROM Group"); $coll = $session->query("FROM Group");
// find all users and user emails // 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");
?> ?>
<?php <?php
$table = $session->getTable("User"); $table = $session->getTable("User");
try { $user = $table->find(2);
$user = $table->find(2);
} catch(Doctrine_Find_Exception $e) {
print "Couldn't find user";
}
// deletes user and all related composite objects // deletes user and all related composite objects
if($user !== false)
$user->delete(); $user->delete();
$users = $table->findAll(); $users = $table->findAll();
......
...@@ -2,19 +2,18 @@ ...@@ -2,19 +2,18 @@
$table = $session->getTable("User"); $table = $session->getTable("User");
// find by primary key // find by primary key
try {
$user = $table->find(2); $user = $table->find(2);
} catch(Doctrine_Find_Exception $e) { if($user !== false)
print "Couldn't find user"; print $user->name;
}
// get all users // get all users
foreach($table->findAll() as $user) { foreach($table->findAll() as $user) {
print $user->name; print $user->name;
} }
// finding by sql // finding by dql
foreach($table->findBySql("name LIKE '%John%'") as $user) { foreach($table->findByDql("name LIKE '%John%'") as $user) {
print $user->created; print $user->created;
} }
......
<?php <?php
$table = $session->getTable("User"); $table = $session->getTable("User");
try {
$user = $table->find(2);
} catch(Doctrine_Find_Exception $e) {
print "Couldn't find user";
}
$user->name = "Jack Daniels"; $user = $table->find(2);
$user->save(); if($user !== false) {
$user->name = "Jack Daniels";
$user->save();
}
?> ?>
...@@ -9,9 +9,11 @@ class UserTable extends Doctrine_Table { ...@@ -9,9 +9,11 @@ class UserTable extends Doctrine_Table {
} }
class User extends Doctrine_Record { } 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"); $table = $session->getTable("User");
......
...@@ -2,19 +2,20 @@ ...@@ -2,19 +2,20 @@
$table = $session->getTable("User"); $table = $session->getTable("User");
// find by primary key // find by primary key
try {
$user = $table->find(2); $user = $table->find(2);
} catch(Doctrine_Find_Exception $e) {
print "Couldn't find user"; if($user !== false)
} print $user->name;
// get all users // get all users
foreach($table->findAll() as $user) { foreach($table->findAll() as $user) {
print $user->name; print $user->name;
} }
// finding by sql // finding by dql
foreach($table->findBySql("name LIKE '%John%'") as $user) { foreach($table->findByDql("name LIKE '%John%'") as $user) {
print $user->created; print $user->created;
} }
?> ?>
...@@ -114,14 +114,12 @@ $menu = array("Getting started" => ...@@ -114,14 +114,12 @@ $menu = array("Getting started" =>
"Getting record state", "Getting record state",
"Getting object copy", "Getting object copy",
"Serializing", "Serializing",
"Getting identifiers",
"Callbacks"), "Callbacks"),
"Session" "Session"
=> array("Introduction", => array("Introduction",
"Availible drivers", "Availible drivers",
"Getting a table object", "Getting a table object",
"Flushing the session", "Flushing the session",
"Limiting queries",
"Querying the database", "Querying the database",
"Getting session state"), "Getting session state"),
...@@ -132,7 +130,7 @@ $menu = array("Getting started" => ...@@ -132,7 +130,7 @@ $menu = array("Getting started" =>
"Getting collection count", "Getting collection count",
"Saving the collection", "Saving the collection",
"Deleting collection", "Deleting collection",
"Fetching strategies", //"Fetching strategies",
"Loading related records", "Loading related records",
"Collection expanding", "Collection expanding",
), ),
...@@ -149,8 +147,8 @@ $menu = array("Getting started" => ...@@ -149,8 +147,8 @@ $menu = array("Getting started" =>
"LIMIT and OFFSET - limiting the query results", "LIMIT and OFFSET - limiting the query results",
"WHERE - setting query conditions", "WHERE - setting query conditions",
"ORDER BY - sorting query results", "ORDER BY - sorting query results",
"Fetching strategies", //"Fetching strategies",
"Lazy property fetching", //"Lazy property fetching",
"Method overloading", "Method overloading",
"Relation operators", "Relation operators",
"Bound parameters", "Bound parameters",
......
...@@ -7,6 +7,7 @@ class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase { ...@@ -7,6 +7,7 @@ class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase {
$users = $q->execute(); $users = $q->execute();
$this->assertEqual($users->count(), 5); $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() { public function testLimitWithOneToOneInnerJoin() {
...@@ -15,6 +16,7 @@ class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase { ...@@ -15,6 +16,7 @@ class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase {
$users = $q->execute(); $users = $q->execute();
$this->assertEqual($users->count(), 5); $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() { public function testLimitWithOneToManyLeftJoin() {
$this->query->from("User(id).Phonenumber"); $this->query->from("User(id).Phonenumber");
...@@ -98,6 +100,9 @@ class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase { ...@@ -98,6 +100,9 @@ class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase {
public function testLimitWithManyToManyLeftJoin() { public function testLimitWithManyToManyLeftJoin() {
$q = new Doctrine_Query($this->session); $q = new Doctrine_Query($this->session);
$q->from("User.Group")->limit(5); $q->from("User.Group")->limit(5);
$users = $q->execute();
$this->assertEqual($users->count(), 5);
} }
} }
......
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