diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index 3d7d454a86e..ab60176e5c8 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -2132,7 +2132,11 @@ function find($conditions = null, $fields = array(), $order = null, $recursive = if (!$db =& ConnectionManager::getDataSource($this->useDbConfig)) { return false; } - + if (!empty($query['joins']) && is_array($query['joins'])) { + foreach($query['joins'] as $i => $join) { + $query['joins'][$i]['table'] = $db->fullTableName($join['table']); + } + } $results = $db->read($this, $query); $this->resetAssociations(); diff --git a/cake/tests/cases/libs/model/model_integration.test.php b/cake/tests/cases/libs/model/model_integration.test.php index e5875e88249..3e09029fbfc 100644 --- a/cake/tests/cases/libs/model/model_integration.test.php +++ b/cake/tests/cases/libs/model/model_integration.test.php @@ -148,6 +148,44 @@ function testDynamicBehaviorAttachment() { $this->assertFalse(isset($TestModel->Behaviors->Tree)); } +/** + * testFindWithJoinsOption method + * + * @access public + * @return void + */ + function testFindWithJoinsOption() { + $this->loadFixtures('Article', 'User'); + $TestUser =& new User(); + + $options = array ( + 'fields' => array( + 'user', + 'Article.published', + ), + 'joins' => array ( + array ( + 'table' => 'articles', + 'alias' => 'Article', + 'type' => 'LEFT', + 'conditions' => array( + 'User.id = Article.user_id', + ), + ), + ), + 'group' => array('User.user'), + 'recursive' => -1, + ); + $result = $TestUser->find('all', $options); + $expected = array( + array('User' => array('user' => 'garrett'), 'Article' => array('published' => '')), + array('User' => array('user' => 'larry'), 'Article' => array('published' => 'Y')), + array('User' => array('user' => 'mariano'), 'Article' => array('published' => 'Y')), + array('User' => array('user' => 'nate'), 'Article' => array('published' => '')) + ); + $this->assertEqual($result, $expected); + } + /** * Tests cross database joins. Requires $test and $test2 to both be set in DATABASE_CONFIG * NOTE: When testing on MySQL, you must set 'persistent' => false on *both* database connections,