diff --git a/lib/Cake/Model/Datasource/DboSource.php b/lib/Cake/Model/Datasource/DboSource.php index 3c4153b02..e8342a162 100644 --- a/lib/Cake/Model/Datasource/DboSource.php +++ b/lib/Cake/Model/Datasource/DboSource.php @@ -1067,6 +1067,9 @@ class DboSource extends DataSource { } } + $originalJoins = $queryData['joins']; + $queryData['joins'] = array(); + // Generate hasOne and belongsTo associations inside $queryData $linkedModels = array(); foreach ($associations as $type) { @@ -1093,6 +1096,10 @@ class DboSource extends DataSource { } } + if (!empty($originalJoins)) { + $queryData['joins'] = array_merge($queryData['joins'], $originalJoins); + } + // Build SQL statement with the primary model, plus hasOne and belongsTo associations $query = $this->buildAssociationQuery($Model, $queryData); diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php index a403b97a6..f1e90d9d8 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php @@ -1268,6 +1268,46 @@ class MysqlTest extends CakeTestCase { return (array)$data + $base; } +/** + * test that read() places provided joins after the generated ones. + * + * @return void + */ + public function testReadCustomJoinsAfterGeneratedJoins() { + $db = $this->Dbo->config['database']; + $test = $this->getMock('Mysql', array('connect', '_execute', 'execute')); + $test->config['database'] = $db; + + $this->Model = $this->getMock('TestModel9', array('getDataSource')); + $this->Model->expects($this->any()) + ->method('getDataSource') + ->will($this->returnValue($test)); + + $this->Model->TestModel8 = $this->getMock('TestModel8', array('getDataSource')); + $this->Model->TestModel8->expects($this->any()) + ->method('getDataSource') + ->will($this->returnValue($test)); + + $search = "LEFT JOIN `cake_test_db`.`test_model8` AS `TestModel8` ON " . + "(`TestModel8`.`name` != 'larry' AND `TestModel9`.`test_model8_id` = `TestModel8`.`id`) " . + "LEFT JOIN `cake_test_db`.`users` AS `User` ON (`TestModel9`.`id` = `User`.`test_id`)"; + + $test->expects($this->at(0))->method('execute') + ->with($this->stringContains($search)); + + $test->read($this->Model, array( + 'joins' => array( + array( + 'table' => 'users', + 'alias' => 'User', + 'type' => 'LEFT', + 'conditions' => array('TestModel9.id = User.test_id') + ) + ), + 'recursive' => 1 + )); + } + /** * testGenerateInnerJoinAssociationQuery method *