Merge pull request #3110 from cakephp/2.5-dbosource-joins

Insert manual joins *after* generated joins.
This commit is contained in:
Mark Story 2014-03-24 15:23:20 -04:00
commit 21d91b15bd
2 changed files with 50 additions and 0 deletions

View file

@ -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);

View file

@ -1268,6 +1268,49 @@ 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));
$model8Table = $test->fullTableName($this->Model->TestModel8);
$usersTable = $test->fullTableName('users');
$search = "LEFT JOIN $model8Table AS `TestModel8` ON " .
"(`TestModel8`.`name` != 'larry' AND `TestModel9`.`test_model8_id` = `TestModel8`.`id`) " .
"LEFT JOIN $usersTable 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
*