Skip to content
This repository
Browse code

fix for cross database joins when recursive < 1

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8212 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information...
commit 92b8e87ed720a0b37d2c98c0cfe35ca0ecc2a752 1 parent a6d3193
authored July 02, 2009
28  cake/libs/model/datasources/dbo_source.php
@@ -627,16 +627,22 @@ function read(&$model, $queryData = array(), $recursive = null) {
627 627
 			$queryData['fields'] = $this->fields($model);
628 628
 		}
629 629
 
630  
-		foreach ($model->__associations as $type) {
  630
+		$_associations = $model->__associations;
  631
+
  632
+		if ($model->recursive == -1) {
  633
+			$_associations = array();
  634
+		} else if ($model->recursive == 0) {
  635
+			unset($_associations[2], $_associations[3]);
  636
+		}
  637
+
  638
+		foreach ($_associations as $type) {
631 639
 			foreach ($model->{$type} as $assoc => $assocData) {
632  
-				if ($model->recursive > -1) {
633  
-					$linkModel =& $model->{$assoc};
634  
-					$external = isset($assocData['external']);
  640
+				$linkModel =& $model->{$assoc};
  641
+				$external = isset($assocData['external']);
635 642
 
636  
-					if ($model->useDbConfig == $linkModel->useDbConfig) {
637  
-						if (true === $this->generateAssociationQuery($model, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null)) {
638  
-							$linkedModels[] = $type . '/' . $assoc;
639  
-						}
  643
+				if ($model->useDbConfig == $linkModel->useDbConfig) {
  644
+					if (true === $this->generateAssociationQuery($model, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null)) {
  645
+						$linkedModels[$type . '/' . $assoc] = true;
640 646
 					}
641 647
 				}
642 648
 			}
@@ -653,12 +659,12 @@ function read(&$model, $queryData = array(), $recursive = null) {
653 659
 
654 660
 		$filtered = $this->__filterResults($resultSet, $model);
655 661
 
656  
-		if ($model->recursive > 0) {
657  
-			foreach ($model->__associations as $type) {
  662
+		if ($model->recursive > -1) {
  663
+			foreach ($_associations as $type) {
658 664
 				foreach ($model->{$type} as $assoc => $assocData) {
659 665
 					$linkModel =& $model->{$assoc};
660 666
 
661  
-					if (!in_array($type . '/' . $assoc, $linkedModels)) {
  667
+					if (empty($linkedModels[$type . '/' . $assoc])) {
662 668
 						if ($model->useDbConfig == $linkModel->useDbConfig) {
663 669
 							$db =& $this;
664 670
 						} else {
9  cake/tests/cases/libs/model/model.test.php
@@ -12082,6 +12082,15 @@ function testCrossDatabaseJoins() {
12082 12082
 		$TestModel->User->setDataSource('test2');
12083 12083
 		$TestModel->Comment->setDataSource('test2');
12084 12084
 
  12085
+		foreach ($expected as $key => $value) {
  12086
+			unset($value['Comment'], $value['Tag']);
  12087
+			$expected[$key] = $value;
  12088
+		}
  12089
+
  12090
+		$TestModel->recursive = 0;
  12091
+		$result = $TestModel->find('all');
  12092
+		$this->assertEqual($result, $expected);
  12093
+
12085 12094
 		$result = Set::extract($TestModel->User->find('all'), '{n}.User.id');
12086 12095
 		$this->assertEqual($result, array('1', '2', '3', '4'));
12087 12096
 		$this->assertEqual($TestModel->find('all'), $expected);

0 notes on commit 92b8e87

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