Skip to content

Commit

Permalink
fix for cross database joins when recursive < 1
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8212 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information
gwoo committed Jul 2, 2009
1 parent a6d3193 commit 92b8e87
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
28 changes: 17 additions & 11 deletions cake/libs/model/datasources/dbo_source.php
Expand Up @@ -627,16 +627,22 @@ function read(&$model, $queryData = array(), $recursive = null) {
$queryData['fields'] = $this->fields($model);
}

foreach ($model->__associations as $type) {
$_associations = $model->__associations;

if ($model->recursive == -1) {
$_associations = array();
} else if ($model->recursive == 0) {
unset($_associations[2], $_associations[3]);
}

foreach ($_associations as $type) {
foreach ($model->{$type} as $assoc => $assocData) {
if ($model->recursive > -1) {
$linkModel =& $model->{$assoc};
$external = isset($assocData['external']);
$linkModel =& $model->{$assoc};
$external = isset($assocData['external']);

if ($model->useDbConfig == $linkModel->useDbConfig) {
if (true === $this->generateAssociationQuery($model, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null)) {
$linkedModels[] = $type . '/' . $assoc;
}
if ($model->useDbConfig == $linkModel->useDbConfig) {
if (true === $this->generateAssociationQuery($model, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null)) {
$linkedModels[$type . '/' . $assoc] = true;
}
}
}
Expand All @@ -653,12 +659,12 @@ function read(&$model, $queryData = array(), $recursive = null) {

$filtered = $this->__filterResults($resultSet, $model);

if ($model->recursive > 0) {
foreach ($model->__associations as $type) {
if ($model->recursive > -1) {
foreach ($_associations as $type) {
foreach ($model->{$type} as $assoc => $assocData) {
$linkModel =& $model->{$assoc};

if (!in_array($type . '/' . $assoc, $linkedModels)) {
if (empty($linkedModels[$type . '/' . $assoc])) {
if ($model->useDbConfig == $linkModel->useDbConfig) {
$db =& $this;
} else {
Expand Down
9 changes: 9 additions & 0 deletions cake/tests/cases/libs/model/model.test.php
Expand Up @@ -12082,6 +12082,15 @@ function testCrossDatabaseJoins() {
$TestModel->User->setDataSource('test2');
$TestModel->Comment->setDataSource('test2');

foreach ($expected as $key => $value) {
unset($value['Comment'], $value['Tag']);
$expected[$key] = $value;
}

$TestModel->recursive = 0;
$result = $TestModel->find('all');
$this->assertEqual($result, $expected);

$result = Set::extract($TestModel->User->find('all'), '{n}.User.id');
$this->assertEqual($result, array('1', '2', '3', '4'));
$this->assertEqual($TestModel->find('all'), $expected);
Expand Down

0 comments on commit 92b8e87

Please sign in to comment.