Skip to content
Permalink
Browse files

Changes Model::find() to allow modification of DataSource connection …

…during callbacks.
  • Loading branch information...
nateabele authored and markstory committed Sep 10, 2009
1 parent 38f5781 commit 1c8a2f232bbe66cce3d0915b7f91d5b1ad16b100
@@ -1948,7 +1948,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();
@@ -1995,6 +1994,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;
@@ -4899,6 +4899,23 @@ function testCallbackDisabling() {
$expected = array('mariano', 'nate', 'larry', 'garrett');
$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
*
@@ -763,6 +763,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;
}
}
/**
* Author class

0 comments on commit 1c8a2f2

Please sign in to comment.
You can’t perform that action at this time.