From acced2b9879230d7b52dbbc0f0dbc8c3aec82bd2 Mon Sep 17 00:00:00 2001 From: zYne Date: Mon, 21 Aug 2006 22:24:09 +0000 Subject: [PATCH] DQL Limit now works with normal many-to-many relations as well as many-to-many relations using column aggregation inheritance --- Doctrine/Query.php | 5 ++++- tests/QueryLimitTestCase.php | 28 +++++++++++++++++++++++++++- tests/classes.php | 26 ++++++++++++++++++++++++++ tests/run.php | 4 ++-- 4 files changed, 59 insertions(+), 4 deletions(-) diff --git a/Doctrine/Query.php b/Doctrine/Query.php index 096f8f57e..244b00227 100644 --- a/Doctrine/Query.php +++ b/Doctrine/Query.php @@ -642,7 +642,10 @@ class Doctrine_Query extends Doctrine_Hydrate { $asf = $fk->getAssociationFactory(); $assocTableName = $asf->getTableName(); - + + if( ! $loadFields) { + $this->subqueryAliases[] = $assocTableName; + } $this->parts["join"][$tname][$assocTableName] = $join.$assocTableName." ON ".$tname.".id = ".$assocTableName.".".$fk->getLocal(); $this->parts["join"][$tname][$tname2] = $join.$aliasString." ON ".$tname2.".id = ".$assocTableName.".".$fk->getForeign(); } diff --git a/tests/QueryLimitTestCase.php b/tests/QueryLimitTestCase.php index 19518d960..27809e370 100644 --- a/tests/QueryLimitTestCase.php +++ b/tests/QueryLimitTestCase.php @@ -1,6 +1,12 @@ tables[] = "Photo"; + $this->tables[] = "Tag"; + $this->tables[] = "Phototag"; + parent::prepareTables(); + } public function testLimitWithOneToOneLeftJoin() { $q = new Doctrine_Query($this->session); $q->from('User(id).Email')->limit(5); @@ -144,7 +150,7 @@ class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase { $this->session->flush(); } - public function testLimitWithManyToManyLeftJoin() { + public function testLimitWithManyToManyColumnAggInheritanceLeftJoin() { $q = new Doctrine_Query($this->session); $q->from("User.Group")->limit(5); $users = $q->execute(); @@ -183,7 +189,27 @@ class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase { $this->assertEqual($users->count(), 3); } + public function testLimitWithNormalManyToMany() { + $coll = new Doctrine_Collection($this->session->getTable("Photo")); + $tag = new Tag(); + $tag->tag = "Some tag"; + $coll[0]->Tag[0] = $tag; + $coll[0]->name = "photo 1"; + $coll[1]->Tag[0] = $tag; + $coll[1]->name = "photo 2"; + $coll[2]->Tag[0] = $tag; + $coll[2]->name = "photo 3"; + $coll[3]->Tag[0]->tag = "Other tag"; + $coll[3]->name = "photo 4"; + $this->session->flush(); + $q = new Doctrine_Query(); + $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($q->getQuery(), + "SELECT photo.id AS photo__id, photo.name AS photo__name FROM photo LEFT JOIN phototag ON photo.id = phototag.photo_id LEFT JOIN tag ON tag.id = phototag.tag_id WHERE photo.id IN (SELECT DISTINCT photo.id FROM photo LEFT JOIN phototag ON photo.id = phototag.photo_id LEFT JOIN tag ON tag.id = phototag.tag_id WHERE tag.id = ? LIMIT 100) AND tag.id = ? ORDER BY photo.id DESC"); + } } ?> diff --git a/tests/classes.php b/tests/classes.php index 4ef03e303..1e67a0902 100644 --- a/tests/classes.php +++ b/tests/classes.php @@ -386,4 +386,30 @@ class Validator_Test extends Doctrine_Record { $this->hasColumn("myemail2", "string", 100, "email|notblank"); } } + + + +class Tag extends Doctrine_Record { + public function setUp() { + $this->hasMany("Photo", "Phototag.photo_id"); + } + public function setTableDefinition() { + $this->hasColumn("tag", "string", 100); + } +} +class Photo extends Doctrine_Record { + public function setUp() { + $this->hasMany("Tag", "Phototag.tag_id"); + } + public function setTableDefinition() { + $this->hasColumn("name", "string", 100); + } +} + +class Phototag extends Doctrine_Record { + public function setTableDefinition() { + $this->hasColumn("photo_id", "integer"); + $this->hasColumn("tag_id", "integer"); + } +} ?> diff --git a/tests/run.php b/tests/run.php index 99a324864..c1c4c1ea5 100644 --- a/tests/run.php +++ b/tests/run.php @@ -28,7 +28,7 @@ require_once("QueryLimitTestCase.php"); error_reporting(E_ALL); $test = new GroupTest("Doctrine Framework Unit Tests"); - +/** $test->addTestCase(new Doctrine_RecordTestCase()); $test->addTestCase(new Doctrine_SessionTestCase()); @@ -66,7 +66,7 @@ $test->addTestCase(new Doctrine_CollectionTestCase()); $test->addTestCase(new Doctrine_QueryTestCase()); $test->addTestCase(new Doctrine_RawSql_TestCase()); - +*/ $test->addTestCase(new Doctrine_Query_Limit_TestCase()); //$test->addTestCase(new Doctrine_Cache_FileTestCase());