Skip to content
This repository
Browse code

FIX DataQuery::applyRelation was returning the base class.

If the applyRelation() was passed a relation that went to a class with a parent
class with a database table, applyRelation would return the name of the parent
class, rather than the class the relation was actually too.
  • Loading branch information...
commit 070ba56104faeffff9dc908e8af38338ff759346 1 parent 5d39c4d
Simon Welsh authored June 25, 2012
4  model/DataQuery.php
@@ -581,7 +581,7 @@ function applyRelation($relation) {
581 581
 	    if(!$relation) return $this->dataClass;
582 582
 	    
583 583
 	    if(is_string($relation)) $relation = explode(".", $relation);
584  
-	    
  584
+	   
585 585
 	    $modelClass = $this->dataClass;
586 586
 	    
587 587
     	foreach($relation as $rel) {
@@ -601,7 +601,6 @@ function applyRelation($relation) {
601 601
     					foreach($ancestry as $ancestor){
602 602
     						if($ancestor != $component){
603 603
     							$this->query->addInnerJoin($ancestor, "\"$component\".\"ID\" = \"$ancestor\".\"ID\"");
604  
-    							$component=$ancestor;
605 604
     						}
606 605
     					}
607 606
     				}
@@ -623,7 +622,6 @@ function applyRelation($relation) {
623 622
     					foreach($ancestry as $ancestor){
624 623
     						if($ancestor != $component){
625 624
     							$this->query->addInnerJoin($ancestor, "\"$component\".\"ID\" = \"$ancestor\".\"ID\"");
626  
-    							$component=$ancestor;
627 625
     						}
628 626
     					}
629 627
     				}
48  tests/model/DataQueryTest.php
@@ -13,5 +13,53 @@ function testJoins() {
13 13
 		$dq->leftJoin("Group_Members", "\"Group_Members\".\"MemberID\" = \"Member\".\"ID\"");
14 14
 		$this->assertContains("LEFT JOIN \"Group_Members\" ON \"Group_Members\".\"MemberID\" = \"Member\".\"ID\"", $dq->sql());
15 15
 	}
  16
+
  17
+	function testRelationReturn() {
  18
+		$dq = new DataQuery('DataQueryTest_C');
  19
+		$this->assertEquals('DataQueryTest_A', $dq->applyRelation('TestA'), 'DataQuery::applyRelation should return the name of the related object.');
  20
+		$this->assertEquals('DataQueryTest_A', $dq->applyRelation('TestAs'), 'DataQuery::applyRelation should return the name of the related object.');
  21
+		$this->assertEquals('DataQueryTest_A', $dq->applyRelation('ManyTestAs'), 'DataQuery::applyRelation should return the name of the related object.');
  22
+
  23
+		$this->assertEquals('DataQueryTest_B', $dq->applyRelation('TestB'), 'DataQuery::applyRelation should return the name of the related object.');
  24
+		$this->assertEquals('DataQueryTest_B', $dq->applyRelation('TestBs'), 'DataQuery::applyRelation should return the name of the related object.');
  25
+		$this->assertEquals('DataQueryTest_B', $dq->applyRelation('ManyTestBs'), 'DataQuery::applyRelation should return the name of the related object.');
  26
+	}
  27
+}
  28
+
  29
+
  30
+class DataQueryTest_A extends DataObject implements TestOnly {
  31
+	public static $db = array(
  32
+		'Name' => 'Varchar',
  33
+	);
  34
+
  35
+	public static $has_one = array(
  36
+		'TestC' => 'DataQueryTest_C',
  37
+	);
16 38
 }
17 39
 
  40
+class DataQueryTest_B extends DataQueryTest_A {
  41
+	public static $db = array(
  42
+		'Title' => 'Varchar',
  43
+	);
  44
+
  45
+	public static $has_one = array(
  46
+		'TestC' => 'DataQueryTest_C',
  47
+	);
  48
+}
  49
+
  50
+class DataQueryTest_C extends DataObject implements TestOnly {
  51
+	public static $has_one = array(
  52
+		'TestA' => 'DataQueryTest_A',
  53
+		'TestB' => 'DataQueryTest_B',
  54
+	);
  55
+
  56
+	public static $has_many = array(
  57
+		'TestAs' => 'DataQueryTest_A',
  58
+		'TestBs' => 'DataQueryTest_B',
  59
+	);
  60
+
  61
+	public static $many_many = array(
  62
+		'ManyTestAs' => 'DataQueryTest_A',
  63
+		'ManyTestBs' => 'DataQueryTest_B',
  64
+	);
  65
+}

0 notes on commit 070ba56

Please sign in to comment.
Something went wrong with that request. Please try again.