Skip to content

Commit

Permalink
fix a DboDataSource buildJoinStatement bug that table prefix is appen…
Browse files Browse the repository at this point in the history
…ded to subquery
  • Loading branch information
perrywky committed Jan 31, 2013
1 parent ee08fe5 commit 5ac5e78
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
5 changes: 2 additions & 3 deletions lib/Cake/Model/Datasource/DboSource.php
Expand Up @@ -1691,9 +1691,8 @@ public function buildJoinStatement($join) {
if (!empty($data['conditions'])) {
$data['conditions'] = trim($this->conditions($data['conditions'], true, false));
}
if (!empty($data['table'])) {
$schema = !(is_string($data['table']) && strpos($data['table'], '(') === 0);
$data['table'] = $this->fullTableName($data['table'], true, $schema);
if (!empty($data['table']) && (!is_string($data['table']) || strpos($data['table'], '(') !== 0)){
$data['table'] = $this->fullTableName($data['table']);
}
return $this->renderJoinStatement($data);
}
Expand Down
36 changes: 36 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
Expand Up @@ -1121,6 +1121,42 @@ public function testBuildJoinStatement($join, $expected) {
$this->assertEquals($expected, $result);
}

/**
* data provider for testBuildJoinStatementWithTablePrefix
*
* @return array
*/
public static function joinStatementsWithPrefix($schema) {
return array(
array(array(
'type' => 'LEFT',
'alias' => 'PostsTag',
'table' => 'posts_tags',
'conditions' => array('PostsTag.post_id = Post.id')
), 'LEFT JOIN pre_posts_tags AS PostsTag ON (PostsTag.post_id = Post.id)'),
array(array(
'type' => 'LEFT',
'alias' => 'Stock',
'table' => '(SELECT Stock.article_id, sum(quantite) quantite FROM stocks AS Stock GROUP BY Stock.article_id)',
'conditions' => 'Stock.article_id = Article.id'
), 'LEFT JOIN (SELECT Stock.article_id, sum(quantite) quantite FROM stocks AS Stock GROUP BY Stock.article_id) AS Stock ON (Stock.article_id = Article.id)')
);
}

/**
* Test buildJoinStatement()
* ensure that prefix is not added when table value is a subquery
*
* @dataProvider joinStatementsWithPrefix
* @return void
*/
public function testBuildJoinStatementWithTablePrefix($join, $expected) {
$db = new DboTestSource;
$db->config['prefix'] = 'pre_';
$result = $db->buildJoinStatement($join);
$this->assertEquals($expected, $result);
}

/**
* Test conditionKeysToString()
*
Expand Down

0 comments on commit 5ac5e78

Please sign in to comment.