From 239ed66eaf2ef2e8292fe890bd0241f756a49788 Mon Sep 17 00:00:00 2001 From: g4b0 Date: Mon, 10 Nov 2014 17:32:58 +0100 Subject: [PATCH] Bugfix: fixed inheritance breaks filtering if relations are included (issue #3610) --- model/DataQuery.php | 3 ++- tests/model/DataQueryTest.php | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/model/DataQuery.php b/model/DataQuery.php index 3ac3639a2..a94bdc6b2 100644 --- a/model/DataQuery.php +++ b/model/DataQuery.php @@ -610,8 +610,9 @@ class DataQuery { if(!$this->query->isJoinedTo($component)) { $has_one = array_flip($model->has_one()); $foreignKey = $has_one[$component]; + $realModelClass = ClassInfo::table_for_object_field($modelClass, "{$foreignKey}ID"); $this->query->addLeftJoin($component, - "\"$component\".\"ID\" = \"{$modelClass}\".\"{$foreignKey}ID\""); + "\"$component\".\"ID\" = \"{$realModelClass}\".\"{$foreignKey}ID\""); /** * add join clause to the component's ancestry classes so that the search filter could search on diff --git a/tests/model/DataQueryTest.php b/tests/model/DataQueryTest.php index c97355369..510cc8a70 100644 --- a/tests/model/DataQueryTest.php +++ b/tests/model/DataQueryTest.php @@ -49,6 +49,14 @@ class DataQueryTest extends SapphireTest { $dq->sql()); } + public function testApplyReplationDeepInheretence() { + $newDQ = new DataQuery('DataQueryTest_E'); + //apply a relation to a relation from an ancestor class + $newDQ->applyRelation('TestA'); + $this->assertTrue($newDQ->query()->isJoinedTo('DataQueryTest_C')); + $this->assertContains('"DataQueryTest_A"."ID" = "DataQueryTest_C"."TestAID"', $newDQ->sql()); + } + public function testRelationReturn() { $dq = new DataQuery('DataQueryTest_C'); $this->assertEquals('DataQueryTest_A', $dq->applyRelation('TestA'), @@ -64,6 +72,9 @@ class DataQueryTest extends SapphireTest { 'DataQuery::applyRelation should return the name of the related object.'); $this->assertEquals('DataQueryTest_B', $dq->applyRelation('ManyTestBs'), 'DataQuery::applyRelation should return the name of the related object.'); + $newDQ = new DataQuery('DataQueryTest_E'); + $this->assertEquals('DataQueryTest_A', $newDQ->applyRelation('TestA'), + 'DataQuery::applyRelation should return the name of the related object.'); } public function testRelationOrderWithCustomJoin() {