1
0
mirror of synced 2025-01-18 22:41:43 +03:00

Fixed DQL many-to-many alias handling

This commit is contained in:
zYne 2007-01-27 10:08:06 +00:00
parent 8303cdb908
commit 25f1d5e8ff
10 changed files with 86 additions and 64 deletions

View File

@ -158,7 +158,7 @@ class Doctrine_Hook
*
* @param array $params an array containing all fields which the built query
* should be ordered by
* @return boolean whether or not the hooking was
* @return boolean whether or not the hooking was successful
*/
public function hookOrderby($params)
{
@ -187,6 +187,7 @@ class Doctrine_Hook
}
}
}
return true;
}
/**
* @param integer $limit

View File

@ -126,7 +126,7 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
}
foreach ($fields as $name) {
$name = $table->getColumnName($name);
$this->parts["select"][] = $tableAlias . '.' .$name . ' AS ' . $tableAlias . '__' . $name;
}
@ -521,12 +521,13 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
*/
public function getQueryBase()
{
switch($this->type) {
switch ($this->type) {
case self::DELETE:
if($this->conn->getName() == 'Mysql')
$q = 'DELETE '.end($this->tableAliases).' FROM ';
else
if ($this->conn->getName() == 'Mysql') {
$q = 'DELETE ' .end($this->tableAliases) . ' FROM ';
} else {
$q = 'DELETE FROM ';
}
break;
case self::UPDATE:
$q = 'UPDATE ';
@ -729,12 +730,11 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
}
}
// 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(' AND ',$this->parts['having']):'';
$subquery .= ( ! empty($this->parts['orderby']))? ' ORDER BY ' . implode(', ', $this->parts['orderby']):'';
$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(' AND ', $this->parts['having']) : '';
$subquery .= ( ! empty($this->parts['orderby']))? ' ORDER BY ' . implode(', ', $this->parts['orderby']) : '';
// add driver specific limit clause
$subquery = $this->conn->modifyLimitQuery($subquery, $this->parts['limit'], $this->parts['offset']);
@ -1222,7 +1222,7 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
$index += strlen($e[($key - 1)]) + 1;
// the mark here is either '.' or ':'
$mark = substr($path,($index - 1),1);
$mark = substr($path, ($index - 1), 1);
if(isset($this->tableAliases[$prevPath])) {
$tname = $this->tableAliases[$prevPath];
@ -1236,14 +1236,15 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
if(isset($this->tableAliases[$currPath])) {
if (isset($this->tableAliases[$currPath])) {
$tname2 = $this->tableAliases[$currPath];
} else
} else {
$tname2 = $this->aliasHandler->generateShortAlias($original);
}
$aliasString = $this->conn->quoteIdentifier($original) . ' ' . $tname2;
switch($mark) {
switch ($mark) {
case ':':
$join = 'INNER JOIN ';
break;
@ -1268,16 +1269,25 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
$assocTableName = $asf->getTableName();
if( ! $loadFields || $joinCondition) {
if( ! $loadFields || $fk->getTable()->usesInheritanceMap() || $joinCondition) {
$this->subqueryAliases[] = $assocTableName;
}
$this->parts['join'][$tname][$assocTableName] = $join . $assocTableName . ' ON ' . $tname . '.'
$assocPath = $prevPath . '.' . $asf->getComponentName();
if (isset($this->tableAliases[$assocPath])) {
$assocAlias = $this->tableAliases[$assocPath];
} else {
$assocAlias = $this->aliasHandler->generateShortAlias($assocTableName);
}
$this->parts['join'][$tname][$assocTableName] = $join . $assocTableName . ' ' . $assocAlias .' ON ' . $tname . '.'
. $table->getIdentifier() . ' = '
. $assocTableName . '.' . $fk->getLocal();
. $assocAlias . '.' . $fk->getLocal();
$this->parts['join'][$tname][$tname2] = $join . $aliasString . ' ON ' . $tname2 . '.'
. $fk->getTable()->getIdentifier() . ' = '
. $assocTableName . '.' . $fk->getForeign()
. $assocAlias . '.' . $fk->getForeign()
. $joinCondition;
} else {

View File

@ -60,7 +60,7 @@ class Doctrine_Validator
public static function getValidator($name)
{
if ( ! isset(self::$validators[$name])) {
$class = "Doctrine_Validator_".ucwords(strtolower($name));
$class = 'Doctrine_Validator_' . ucwords(strtolower($name));
if (class_exists($class)) {
self::$validators[$name] = new $class;
} else {

View File

@ -4,7 +4,7 @@ class Doctrine_Export_Firebird_TestCase extends Doctrine_UnitTestCase {
try {
$this->export->createDatabase('db');
$this->fail();
} catch(Doctrine_Export_Firebird_Exception $e) {
} catch(Doctrine_Export_Exception $e) {
$this->pass();
}
}
@ -12,7 +12,7 @@ class Doctrine_Export_Firebird_TestCase extends Doctrine_UnitTestCase {
try {
$this->export->dropDatabase('db');
$this->fail();
} catch(Doctrine_Export_Firebird_Exception $e) {
} catch(Doctrine_Export_Exception $e) {
$this->pass();
}
}

View File

@ -29,9 +29,9 @@ class Doctrine_Query_ComponentAlias_TestCase extends Doctrine_UnitTestCase {
$this->assertEqual($users->count(), 8);
$this->assertTrue($users[0]->Phonenumber instanceof Doctrine_Collection);
$this->assertEqual($q->getQuery(),
"SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, e2.id AS e2__id, e2.name AS e2__name, e2.loginname AS e2__loginname, e2.password AS e2__password, e2.type AS e2__type, e2.created AS e2__created, e2.updated AS e2__updated, e2.email_id AS e2__email_id, p.id AS p__id, p.phonenumber AS p__phonenumber, p.entity_id AS p__entity_id FROM entity e LEFT JOIN groupuser ON e.id = groupuser.user_id LEFT JOIN entity e2 ON e2.id = groupuser.group_id LEFT JOIN phonenumber p ON e2.id = p.entity_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
$this->assertEqual($q->getQuery(),
"SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, e2.id AS e2__id, e2.name AS e2__name, e2.loginname AS e2__loginname, e2.password AS e2__password, e2.type AS e2__type, e2.created AS e2__created, e2.updated AS e2__updated, e2.email_id AS e2__email_id, p.id AS p__id, p.phonenumber AS p__phonenumber, p.entity_id AS p__entity_id FROM entity e LEFT JOIN groupuser g2 ON e.id = g2.user_id LEFT JOIN entity e2 ON e2.id = g2.group_id LEFT JOIN phonenumber p ON e2.id = p.entity_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
$this->assertEqual(($count + 1), count($this->dbh));
}
public function testQueryWithMultipleNestedAliases() {
@ -45,9 +45,10 @@ class Doctrine_Query_ComponentAlias_TestCase extends Doctrine_UnitTestCase {
$count = count($this->dbh);
$this->assertEqual($users->count(), 8);
$this->assertTrue($users[0]->Phonenumber instanceof Doctrine_Collection);
$this->assertEqual($q->getQuery(),
"SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, p.id AS p__id, p.phonenumber AS p__phonenumber, p.entity_id AS p__entity_id, e2.id AS e2__id, e2.name AS e2__name, e2.loginname AS e2__loginname, e2.password AS e2__password, e2.type AS e2__type, e2.created AS e2__created, e2.updated AS e2__updated, e2.email_id AS e2__email_id, p2.id AS p2__id, p2.phonenumber AS p2__phonenumber, p2.entity_id AS p2__entity_id FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id LEFT JOIN groupuser ON e.id = groupuser.user_id LEFT JOIN entity e2 ON e2.id = groupuser.group_id LEFT JOIN phonenumber p2 ON e2.id = p2.entity_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
$this->assertTrue($users[0]->Phonenumber instanceof Doctrine_Collection);
$this->assertEqual($q->getQuery(),
"SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, p.id AS p__id, p.phonenumber AS p__phonenumber, p.entity_id AS p__entity_id, e2.id AS e2__id, e2.name AS e2__name, e2.loginname AS e2__loginname, e2.password AS e2__password, e2.type AS e2__type, e2.created AS e2__created, e2.updated AS e2__updated, e2.email_id AS e2__email_id, p2.id AS p2__id, p2.phonenumber AS p2__phonenumber, p2.entity_id AS p2__entity_id FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id LEFT JOIN groupuser g2 ON e.id = g2.user_id LEFT JOIN entity e2 ON e2.id = g2.group_id LEFT JOIN phonenumber p2 ON e2.id = p2.entity_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
$this->assertEqual($count, count($this->dbh));
}
}

View File

@ -5,19 +5,17 @@ class Doctrine_Query_From_TestCase extends Doctrine_UnitTestCase {
$q->from('User u LEFT JOIN u.Group');
$this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, e2.id AS e2__id, e2.name AS e2__name, e2.loginname AS e2__loginname, e2.password AS e2__password, e2.type AS e2__type, e2.created AS e2__created, e2.updated AS e2__updated, e2.email_id AS e2__email_id FROM entity e LEFT JOIN groupuser ON e.id = groupuser.user_id LEFT JOIN entity e2 ON e2.id = groupuser.group_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
$this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, e2.id AS e2__id, e2.name AS e2__name, e2.loginname AS e2__loginname, e2.password AS e2__password, e2.type AS e2__type, e2.created AS e2__created, e2.updated AS e2__updated, e2.email_id AS e2__email_id FROM entity e LEFT JOIN groupuser g ON e.id = g.user_id LEFT JOIN entity e2 ON e2.id = g.group_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
$users = $q->execute();
$this->assertEqual($users->count(), 8);
}
public function testDefaultLeftJoin() {
public function testDefaultJoinIsLeftJoin() {
$q = new Doctrine_Query();
$q->from('User u JOIN u.Group');
$this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, e2.id AS e2__id, e2.name AS e2__name, e2.loginname AS e2__loginname, e2.password AS e2__password, e2.type AS e2__type, e2.created AS e2__created, e2.updated AS e2__updated, e2.email_id AS e2__email_id FROM entity e LEFT JOIN groupuser ON e.id = groupuser.user_id LEFT JOIN entity e2 ON e2.id = groupuser.group_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
$this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, e2.id AS e2__id, e2.name AS e2__name, e2.loginname AS e2__loginname, e2.password AS e2__password, e2.type AS e2__type, e2.created AS e2__created, e2.updated AS e2__updated, e2.email_id AS e2__email_id FROM entity e LEFT JOIN groupuser g ON e.id = g.user_id LEFT JOIN entity e2 ON e2.id = g.group_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
$users = $q->execute();
$this->assertEqual($users->count(), 8);
@ -27,8 +25,7 @@ class Doctrine_Query_From_TestCase extends Doctrine_UnitTestCase {
$q->from('User u INNER JOIN u.Group');
$this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, e2.id AS e2__id, e2.name AS e2__name, e2.loginname AS e2__loginname, e2.password AS e2__password, e2.type AS e2__type, e2.created AS e2__created, e2.updated AS e2__updated, e2.email_id AS e2__email_id FROM entity e INNER JOIN groupuser ON e.id = groupuser.user_id INNER JOIN entity e2 ON e2.id = groupuser.group_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
$this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, e2.id AS e2__id, e2.name AS e2__name, e2.loginname AS e2__loginname, e2.password AS e2__password, e2.type AS e2__type, e2.created AS e2__created, e2.updated AS e2__updated, e2.email_id AS e2__email_id FROM entity e INNER JOIN groupuser g ON e.id = g.user_id INNER JOIN entity e2 ON e2.id = g.group_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
$users = $q->execute();
$this->assertEqual($users->count(), 1);
@ -38,7 +35,7 @@ class Doctrine_Query_From_TestCase extends Doctrine_UnitTestCase {
$q->from('User u LEFT JOIN u.Group LEFT JOIN u.Phonenumber');
$this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, e2.id AS e2__id, e2.name AS e2__name, e2.loginname AS e2__loginname, e2.password AS e2__password, e2.type AS e2__type, e2.created AS e2__created, e2.updated AS e2__updated, e2.email_id AS e2__email_id, p.id AS p__id, p.phonenumber AS p__phonenumber, p.entity_id AS p__entity_id FROM entity e LEFT JOIN groupuser ON e.id = groupuser.user_id LEFT JOIN entity e2 ON e2.id = groupuser.group_id LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
$this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, e2.id AS e2__id, e2.name AS e2__name, e2.loginname AS e2__loginname, e2.password AS e2__password, e2.type AS e2__type, e2.created AS e2__created, e2.updated AS e2__updated, e2.email_id AS e2__email_id, p.id AS p__id, p.phonenumber AS p__phonenumber, p.entity_id AS p__entity_id FROM entity e LEFT JOIN groupuser g ON e.id = g.user_id LEFT JOIN entity e2 ON e2.id = g.group_id LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
$users = $q->execute();
$this->assertEqual($users->count(), 8);
@ -48,7 +45,7 @@ class Doctrine_Query_From_TestCase extends Doctrine_UnitTestCase {
$q->from('User u LEFT JOIN u.Group LEFT JOIN u.Phonenumber');
$this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, e2.id AS e2__id, e2.name AS e2__name, e2.loginname AS e2__loginname, e2.password AS e2__password, e2.type AS e2__type, e2.created AS e2__created, e2.updated AS e2__updated, e2.email_id AS e2__email_id, p.id AS p__id, p.phonenumber AS p__phonenumber, p.entity_id AS p__entity_id FROM entity e LEFT JOIN groupuser ON e.id = groupuser.user_id LEFT JOIN entity e2 ON e2.id = groupuser.group_id LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
$this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, e2.id AS e2__id, e2.name AS e2__name, e2.loginname AS e2__loginname, e2.password AS e2__password, e2.type AS e2__type, e2.created AS e2__created, e2.updated AS e2__updated, e2.email_id AS e2__email_id, p.id AS p__id, p.phonenumber AS p__phonenumber, p.entity_id AS p__entity_id FROM entity e LEFT JOIN groupuser g ON e.id = g.user_id LEFT JOIN entity e2 ON e2.id = g.group_id LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
$users = $q->execute();
$this->assertEqual($users->count(), 8);
@ -56,9 +53,9 @@ class Doctrine_Query_From_TestCase extends Doctrine_UnitTestCase {
public function testMultipleInnerJoin() {
$q = new Doctrine_Query();
$q->from('User u INNER JOIN u.Group INNER JOIN u.Phonenumber');
$q->select('u.name')->from('User u INNER JOIN u.Group INNER JOIN u.Phonenumber');
$this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, e2.id AS e2__id, e2.name AS e2__name, e2.loginname AS e2__loginname, e2.password AS e2__password, e2.type AS e2__type, e2.created AS e2__created, e2.updated AS e2__updated, e2.email_id AS e2__email_id, p.id AS p__id, p.phonenumber AS p__phonenumber, p.entity_id AS p__entity_id FROM entity e INNER JOIN groupuser ON e.id = groupuser.user_id INNER JOIN entity e2 ON e2.id = groupuser.group_id INNER JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
$this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.name AS e__name FROM entity e INNER JOIN groupuser g ON e.id = g.user_id INNER JOIN entity e2 ON e2.id = g.group_id INNER JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
$users = $q->execute();
$this->assertEqual($users->count(), 1);
@ -66,9 +63,9 @@ class Doctrine_Query_From_TestCase extends Doctrine_UnitTestCase {
public function testMultipleInnerJoin2() {
$q = new Doctrine_Query();
$q->from('User u INNER JOIN u.Group, u.Phonenumber');
$q->select('u.name')->from('User u INNER JOIN u.Group, u.Phonenumber');
$this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, e2.id AS e2__id, e2.name AS e2__name, e2.loginname AS e2__loginname, e2.password AS e2__password, e2.type AS e2__type, e2.created AS e2__created, e2.updated AS e2__updated, e2.email_id AS e2__email_id, p.id AS p__id, p.phonenumber AS p__phonenumber, p.entity_id AS p__entity_id FROM entity e INNER JOIN groupuser ON e.id = groupuser.user_id INNER JOIN entity e2 ON e2.id = groupuser.group_id INNER JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
$this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.name AS e__name FROM entity e INNER JOIN groupuser g ON e.id = g.user_id INNER JOIN entity e2 ON e2.id = g.group_id INNER JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
$users = $q->execute();
$this->assertEqual($users->count(), 1);
@ -76,9 +73,10 @@ class Doctrine_Query_From_TestCase extends Doctrine_UnitTestCase {
public function testMixingOfJoins() {
$q = new Doctrine_Query();
$q->from('User u INNER JOIN u.Group LEFT JOIN u.Phonenumber');
$q->select('u.name, g.name, p.phonenumber')->from('User u INNER JOIN u.Group g LEFT JOIN u.Phonenumber p');
$this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, e2.id AS e2__id, e2.name AS e2__name, e2.loginname AS e2__loginname, e2.password AS e2__password, e2.type AS e2__type, e2.created AS e2__created, e2.updated AS e2__updated, e2.email_id AS e2__email_id, p.id AS p__id, p.phonenumber AS p__phonenumber, p.entity_id AS p__entity_id FROM entity e INNER JOIN groupuser ON e.id = groupuser.user_id INNER JOIN entity e2 ON e2.id = groupuser.group_id LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
$this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.name AS e__name, e2.id AS e2__id, e2.name AS e2__name, p.id AS p__id, p.phonenumber AS p__phonenumber FROM entity e INNER JOIN groupuser g ON e.id = g.user_id INNER JOIN entity e2 ON e2.id = g.group_id LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
$users = $q->execute();
$this->assertEqual($users->count(), 1);

View File

@ -54,14 +54,14 @@ class Doctrine_Query_JoinCondition_TestCase extends Doctrine_UnitTestCase
$q->parseQuery("SELECT u.name, g.id FROM User u LEFT JOIN u.Group g ON g.id > 2");
$this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.name AS e__name, e2.id AS e2__id FROM entity e LEFT JOIN groupuser ON e.id = groupuser.user_id LEFT JOIN entity e2 ON e2.id = groupuser.group_id AND e2.id > 2 WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
$this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.name AS e__name, e2.id AS e2__id FROM entity e LEFT JOIN groupuser g ON e.id = g.user_id LEFT JOIN entity e2 ON e2.id = g.group_id AND e2.id > 2 WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
}
public function testJoinConditionsAreSupportedForManyToManyInnerJoins()
{
$q = new Doctrine_Query();
$q->parseQuery("SELECT u.name, g.id FROM User u INNER JOIN u.Group g ON g.id > 2");
$this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.name AS e__name, e2.id AS e2__id FROM entity e INNER JOIN groupuser ON e.id = groupuser.user_id INNER JOIN entity e2 ON e2.id = groupuser.group_id AND e2.id > 2 WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
$this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.name AS e__name, e2.id AS e2__id FROM entity e INNER JOIN groupuser g ON e.id = g.user_id INNER JOIN entity e2 ON e2.id = g.group_id AND e2.id > 2 WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
}
}

View File

@ -74,4 +74,12 @@ class Doctrine_Query_Join_TestCase extends Doctrine_UnitTestCase
$this->assertEqual($c->City[0]->District->name, 'District 1');
$this->assertEqual($c->City[2]->District->name, 'District 2');
}
public function testManyToManyJoinUsesProperTableAliases()
{
$q = new Doctrine_Query();
$q->select('u.name')->from('User u INNER JOIN u.Group g');
$this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name FROM entity e INNER JOIN groupuser g ON e.id = g.user_id INNER JOIN entity e2 ON e2.id = g.group_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))');
}
}

View File

@ -211,8 +211,7 @@ class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase {
$this->assertEqual($users->count(), 3);
$this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id FROM entity e LEFT JOIN groupuser ON e.id = groupuser.user_id LEFT JOIN entity e2 ON e2.id = groupuser.group_id WHERE e2.id = ? AND (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL)) ORDER BY e.id DESC LIMIT 5");
$this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id FROM entity e LEFT JOIN groupuser g ON e.id = g.user_id LEFT JOIN entity e2 ON e2.id = g.group_id WHERE e2.id = ? AND (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL)) ORDER BY e.id DESC LIMIT 5");
$this->manager->setAttribute(Doctrine::ATTR_QUERY_LIMIT, Doctrine::LIMIT_RECORDS);
}
@ -239,10 +238,9 @@ class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase {
$q->from("Photo")->where("Photo.Tag.id = ?")->orderby("Photo.id DESC")->limit(100);
$photos = $q->execute(array(1));
$this->assertEqual($photos->count(), 3);
$this->assertEqual($photos->count(), 3);
$this->assertEqual($q->getQuery(),
"SELECT p.id AS p__id, p.name AS p__name FROM photo p LEFT JOIN phototag ON p.id = phototag.photo_id LEFT JOIN tag t ON t.id = phototag.tag_id WHERE p.id IN (SELECT DISTINCT p2.id FROM photo p2 LEFT JOIN phototag ON p2.id = phototag.photo_id LEFT JOIN tag t2 ON t2.id = phototag.tag_id WHERE t2.id = ? ORDER BY p2.id DESC LIMIT 100) AND t.id = ? ORDER BY p.id DESC");
"SELECT p.id AS p__id, p.name AS p__name FROM photo p LEFT JOIN phototag p2 ON p.id = p2.photo_id LEFT JOIN tag t ON t.id = p2.tag_id WHERE p.id IN (SELECT DISTINCT p3.id FROM photo p3 LEFT JOIN phototag p4 ON p3.id = p4.photo_id LEFT JOIN tag t2 ON t2.id = p4.tag_id WHERE t2.id = ? ORDER BY p3.id DESC LIMIT 100) AND t.id = ? ORDER BY p.id DESC");
}
}

View File

@ -60,16 +60,9 @@ $test = new GroupTest('Doctrine Framework Unit Tests');
$test->addTestCase(new Doctrine_Import_Firebird_TestCase());
$test->addTestCase(new Doctrine_Import_Informix_TestCase());
$test->addTestCase(new Doctrine_Import_Mysql_TestCase());
$test->addTestCase(new Doctrine_Import_Mssql_TestCase());
$test->addTestCase(new Doctrine_Import_Pgsql_TestCase());
$test->addTestCase(new Doctrine_Import_Oracle_TestCase());
$test->addTestCase(new Doctrine_Import_Sqlite_TestCase());
// DATABASE ABSTRACTION tests
/**
// Connection drivers (not yet fully tested)
$test->addTestCase(new Doctrine_Connection_Pgsql_TestCase());
$test->addTestCase(new Doctrine_Connection_Oracle_TestCase());
@ -123,6 +116,13 @@ $test->addTestCase(new Doctrine_Export_Sqlite_TestCase());
// Import module (not yet fully tested)
//$test->addTestCase(new Doctrine_Import_TestCase());
$test->addTestCase(new Doctrine_Import_Firebird_TestCase());
$test->addTestCase(new Doctrine_Import_Informix_TestCase());
$test->addTestCase(new Doctrine_Import_Mysql_TestCase());
$test->addTestCase(new Doctrine_Import_Mssql_TestCase());
$test->addTestCase(new Doctrine_Import_Pgsql_TestCase());
$test->addTestCase(new Doctrine_Import_Oracle_TestCase());
$test->addTestCase(new Doctrine_Import_Sqlite_TestCase());
// Expression module (not yet fully tested)
@ -155,7 +155,7 @@ $test->addTestCase(new Doctrine_Relation_Access_TestCase());
$test->addTestCase(new Doctrine_Relation_ManyToMany_TestCase());
$test->addTestCase(new Doctrine_Relation_OneToOne_TestCase());
$test->addTestCase(new Doctrine_TreeStructure_TestCase());
// Datatypes
$test->addTestCase(new Doctrine_Enum_TestCase());
@ -200,9 +200,11 @@ $test->addTestCase(new Doctrine_Query_ComponentAlias_TestCase());
$test->addTestCase(new Doctrine_Query_Subquery_TestCase());
$test->addTestCase(new Doctrine_Query_TestCase());
$test->addTestCase(new Doctrine_Query_ShortAliases_TestCase());
$test->addTestCase(new Doctrine_Query_From_TestCase());
$test->addTestCase(new Doctrine_Query_Delete_TestCase());
$test->addTestCase(new Doctrine_Query_Where_TestCase());
$test->addTestCase(new Doctrine_Query_Limit_TestCase());
$test->addTestCase(new Doctrine_Query_IdentifierQuoting_TestCase());
$test->addTestCase(new Doctrine_Query_Update_TestCase());
@ -210,13 +212,17 @@ $test->addTestCase(new Doctrine_Query_AggregateValue_TestCase());
$test->addTestCase(new Doctrine_Query_Select_TestCase());
$test->addTestCase(new Doctrine_Query_Expression_TestCase());
$test->addTestCase(new Doctrine_Query_Having_TestCase());
$test->addTestCase(new Doctrine_Query_JoinCondition_TestCase());
$test->addTestCase(new Doctrine_Query_Join_TestCase());
$test->addTestCase(new Doctrine_ColumnAlias_TestCase());
*/
$test->addTestCase(new Doctrine_Query_From_TestCase());
$test->addTestCase(new Doctrine_Query_JoinCondition_TestCase());
$test->addTestCase(new Doctrine_ColumnAlias_TestCase());
$test->addTestCase(new Doctrine_TreeStructure_TestCase());
// Cache tests
//$test->addTestCase(new Doctrine_Cache_Query_SqliteTestCase());
//$test->addTestCase(new Doctrine_Cache_FileTestCase());