Skip to content

Commit

Permalink
Changes Model::find() to allow modification of DataSource connection …
Browse files Browse the repository at this point in the history
…during callbacks.
  • Loading branch information
nateabele committed Sep 10, 2009
1 parent 4766f62 commit fd6538f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cake/libs/model/model.php
Expand Up @@ -2020,7 +2020,6 @@ function find($conditions = null, $fields = array(), $order = null, $recursive =
list($type, $query) = array($conditions, $fields);
}

$db =& ConnectionManager::getDataSource($this->useDbConfig);
$this->findQueryType = $type;
$this->id = $this->getID();

Expand Down Expand Up @@ -2067,6 +2066,9 @@ function find($conditions = null, $fields = array(), $order = null, $recursive =
}
}

if (!$db =& ConnectionManager::getDataSource($this->useDbConfig)) {
return false;
}
$results = $db->read($this, $query);
$this->resetAssociations();
$this->findQueryType = null;
Expand Down
16 changes: 16 additions & 0 deletions cake/tests/cases/libs/model/model_read.test.php
Expand Up @@ -4877,6 +4877,22 @@ function testCallbackDisabling() {
$this->assertEqual($result, $expected);
}

/**
* Tests that the database configuration assigned to the model can be changed using
* (before|after)Find callbacks
*
* @return void
*/
function testCallbackSourceChange() {
$this->loadFixtures('Post');
$TestModel = new Post();
$this->assertEqual(3, count($TestModel->find('all')));

$this->expectError(new PatternExpectation('/Non-existent data source foo/i'));
$this->expectError(new PatternExpectation('/Only variable references/i'));
$this->assertFalse($TestModel->find('all', array('connection' => 'foo')));
}

/**
* testMultipleBelongsToWithSameClass method
*
Expand Down
12 changes: 12 additions & 0 deletions cake/tests/cases/libs/model/models.php
Expand Up @@ -857,6 +857,18 @@ class Post extends CakeTestModel {
* @access public
*/
var $belongsTo = array('Author');

function beforeFind($queryData) {
if (isset($queryData['connection'])) {
$this->useDbConfig = $queryData['connection'];
}
return true;
}

function afterFind($results) {
$this->useDbConfig = 'test_suite';
return $results;
}
}

/**
Expand Down

0 comments on commit fd6538f

Please sign in to comment.