Docs updated, more tests for DQL LIMIT
This commit is contained in:
parent
75a2dea3b1
commit
c9b9017985
11 changed files with 57 additions and 41 deletions
|
@ -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"]):'';
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
// delete all users with name 'John'
|
||||
|
||||
$users = $table->findBySql("name LIKE '%John%'");
|
||||
$users = $table->findByDql("name LIKE '%John%'");
|
||||
|
||||
$users->delete();
|
||||
?>
|
||||
|
|
|
@ -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");
|
||||
?>
|
||||
|
|
|
@ -1,15 +1,11 @@
|
|||
<?php
|
||||
$table = $session->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();
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
<?php
|
||||
$table = $session->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();
|
||||
?>
|
||||
|
|
|
@ -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");
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
?>
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue