From 94005a9fdd6046fa6d2e29fc18067bc6e756d779 Mon Sep 17 00:00:00 2001 From: doctrine Date: Wed, 17 May 2006 17:58:40 +0000 Subject: [PATCH] DQL alias bug fixed --- classes/Query.class.php | 17 ++++++++++++----- classes/Table.class.php | 2 +- tests/QueryTestCase.class.php | 19 +++++++++++++++++-- tests/TableTestCase.class.php | 3 ++- tests/classes.php | 1 + tests/run.php | 5 ++--- 6 files changed, 35 insertions(+), 12 deletions(-) diff --git a/classes/Query.class.php b/classes/Query.class.php index 94371b803..f29df9990 100644 --- a/classes/Query.class.php +++ b/classes/Query.class.php @@ -601,6 +601,7 @@ class Doctrine_Query extends Doctrine_Access { $field = array_pop($a); $reference = implode(".",$a); $name = end($a); + $this->load($reference); $tname = $this->tables[$name]->getTableName(); @@ -788,14 +789,20 @@ class Doctrine_Query extends Doctrine_Access { $operator = array_shift($e); $value = implode(" ",$e); $reference = implode(".",$a); - $objTable = $this->session->getTable(end($a)); + + if(count($a) > 1) + $objTable = $this->tables[$a[0]]->getForeignKey(end($a))->getTable(); + else + $objTable = $this->session->getTable(end($a)); + $where = $objTable->getTableName().".".$field." ".$operator." ".$value; if(count($a) > 1 && isset($a[1])) { $root = $a[0]; $fk = $this->tables[$root]->getForeignKey($a[1]); if($fk instanceof Doctrine_Association) { - $asf = $fk->getAssociationFactory(); + $asf = $fk->getAssociationFactory(); + switch($fk->getType()): case Doctrine_Relation::ONE_AGGREGATE: case Doctrine_Relation::ONE_COMPOSITE: @@ -803,15 +810,15 @@ class Doctrine_Query extends Doctrine_Access { break; case Doctrine_Relation::MANY_AGGREGATE: case Doctrine_Relation::MANY_COMPOSITE: - $b = array_shift($a); - $b = array_shift($a); + $b = $fk->getTable()->getComponentName(); + $graph = new Doctrine_Query($this->session); $graph->parseQuery("FROM $b-l WHERE $where"); $where = $this->tables[$root]->getTableName().".".$this->tables[$root]->getIdentifier()." IN (SELECT ".$fk->getLocal()." FROM ".$asf->getTableName()." WHERE ".$fk->getForeign()." IN (".$graph->getQuery()."))"; break; endswitch; } else - $this->load($reference); + $this->load($reference); } else $this->load($reference); diff --git a/classes/Table.class.php b/classes/Table.class.php index 4cf4c9135..2c43c85c3 100644 --- a/classes/Table.class.php +++ b/classes/Table.class.php @@ -23,7 +23,7 @@ class Doctrine_Table extends Doctrine_Configurable { /** * @var array $relations an array containing all the Doctrine_Relation objects for this table */ - private $relations = array(); + private $relations = array(); /** * @var array $primaryKeys an array containing all primary key column names */ diff --git a/tests/QueryTestCase.class.php b/tests/QueryTestCase.class.php index 7c867f6e9..3685cdd10 100644 --- a/tests/QueryTestCase.class.php +++ b/tests/QueryTestCase.class.php @@ -1,5 +1,20 @@ name = "Task 1"; + $task->ResourceAlias[0]->name = "Resource 1"; + $task->ResourceAlias[1]->name = "Resource 2"; + + $task->save(); + $query = new Doctrine_Query($this->session); + $coll = $query->query("FROM Task WHERE Task.ResourceAlias.name = 'Resource 1'"); + + $this->assertEqual($coll->count(), 1); + $this->assertTrue($coll[0] instanceof Task); + } + public function testQueryArgs() { $query = new Doctrine_Query($this->session); $query = $query->from("User-l"); @@ -75,7 +90,9 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { $this->assertEqual($users->count(),8); $this->assertTrue($users[0]->name == "Arnold Schwarzenegger"); } + public function testQuery() { + $query = new Doctrine_Query($this->session); $this->graph = $query; @@ -244,8 +261,6 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { $this->assertTrue($users instanceof Doctrine_Collection); $this->assertEqual($users->count(),1); - - $values = $query->query("SELECT COUNT(User.name) AS users, MAX(User.name) AS max FROM User"); $this->assertEqual(trim($query->getQuery()),"SELECT COUNT(entity.name) AS users, MAX(entity.name) AS max FROM entity WHERE (entity.type = 0)"); $this->assertTrue(is_array($values)); diff --git a/tests/TableTestCase.class.php b/tests/TableTestCase.class.php index 756d4c8b5..c9a554e65 100644 --- a/tests/TableTestCase.class.php +++ b/tests/TableTestCase.class.php @@ -1,6 +1,7 @@ session->getTable("User"); } @@ -27,7 +28,7 @@ class Doctrine_TableTestCase extends Doctrine_UnitTestCase { $this->assertTrue($fk->getLocal() == $this->objTable->getIdentifier()); $this->assertTrue($fk->getForeign() == "entity_id"); - + } public function testGetComponentName() { $this->assertTrue($this->objTable->getComponentName() == "User"); diff --git a/tests/classes.php b/tests/classes.php index d2b50e0fd..bdb2a2b47 100644 --- a/tests/classes.php +++ b/tests/classes.php @@ -98,6 +98,7 @@ class Phonenumber extends Doctrine_Record { $this->hasColumn("entity_id","integer"); } } + class Element extends Doctrine_Record { public function setTableDefinition() { $this->hasColumn("name", "string", 100); diff --git a/tests/run.php b/tests/run.php index bb5791817..4c799a811 100644 --- a/tests/run.php +++ b/tests/run.php @@ -27,13 +27,13 @@ $test = new GroupTest("Doctrine Framework Unit Tests"); - +$test->addTestCase(new Doctrine_TableTestCase()); $test->addTestCase(new Doctrine_SessionTestCase()); $test->addTestCase(new Doctrine_RecordTestCase()); -$test->addTestCase(new Doctrine_TableTestCase()); + $test->addTestCase(new Doctrine_ValidatorTestCase()); @@ -54,7 +54,6 @@ $test->addTestCase(new Doctrine_Collection_OffsetTestCase()); $test->addTestCase(new Sensei_UnitTestCase()); $test->addTestCase(new Doctrine_QueryTestCase()); - //$test->addTestCase(new Doctrine_Cache_FileTestCase()); //$test->addTestCase(new Doctrine_Cache_SqliteTestCase());