Skip to content

Commit

Permalink
Fixing autoFields causing invalid SQL when cross database joins are b…
Browse files Browse the repository at this point in the history
…eing done. Tests added. Fixes #476
  • Loading branch information
markstory committed Apr 20, 2010
1 parent 5ed5c54 commit a5f0197
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
15 changes: 12 additions & 3 deletions cake/libs/model/behaviors/containable.php
Expand Up @@ -185,23 +185,32 @@ function beforeFind(&$Model, $query) {
foreach (array('hasOne', 'belongsTo') as $type) {
if (!empty($Model->{$type})) {
foreach ($Model->{$type} as $assoc => $data) {
if (!empty($data['fields'])) {
if ($Model->useDbConfig == $Model->{$assoc}->useDbConfig && !empty($data['fields'])) {
foreach ((array) $data['fields'] as $field) {
$query['fields'][] = (strpos($field, '.') === false ? $assoc . '.' : '') . $field;
}
}
}
}
}

if (!empty($mandatory[$Model->alias])) {
foreach ($mandatory[$Model->alias] as $field) {
if ($field == '--primaryKey--') {
$field = $Model->primaryKey;
} else if (preg_match('/^.+\.\-\-[^-]+\-\-$/', $field)) {
list($modelName, $field) = explode('.', $field);
$field = $modelName . '.' . (($field === '--primaryKey--') ? $Model->$modelName->primaryKey : $field);
if ($Model->useDbConfig == $Model->{$modelName}->useDbConfig) {
$field = $modelName . '.' . (
($field === '--primaryKey--') ? $Model->$modelName->primaryKey : $field
);
} else {
$field = null;
}
}
if ($field !== null) {
$query['fields'][] = $field;
}
$query['fields'][] = $field;
}
}
$query['fields'] = array_unique($query['fields']);
Expand Down
33 changes: 33 additions & 0 deletions cake/tests/cases/libs/model/behaviors/containable.test.php
Expand Up @@ -3573,6 +3573,39 @@ function testResetMultipleHabtmAssociations() {
$this->Article->find('all', array('contain' => array('ShortTag' => array('fields' => array('ShortTag.tag', 'ShortTag.created')))));
$this->assertEqual($expected, $this->Article->hasAndBelongsToMany);
}
/**
* test that autoFields doesn't splice in fields from other databases.
*
* @return void
*/
function testAutoFieldsWithMultipleDatabases() {
$config = new DATABASE_CONFIG();

$skip = $this->skipIf(
!isset($config->test) || !isset($config->test2),
'%s Primary and secondary test databases not configured, skipping cross-database '
.'join tests.'
.' To run these tests, you must define $test and $test2 in your database configuration.'
);
if ($skip) {
return;
}

$db =& ConnectionManager::getDataSource('test2');
$this->_fixtures[$this->_fixtureClassMap['User']]->create($db);
$this->_fixtures[$this->_fixtureClassMap['User']]->insert($db);

$this->Article->User->setDataSource('test2');

$result = $this->Article->find('all', array(
'fields' => array('Article.title'),
'contain' => array('User')
));
$this->assertTrue(isset($result[0]['Article']));
$this->assertTrue(isset($result[0]['User']));

$this->_fixtures[$this->_fixtureClassMap['User']]->drop($db);
}
/**
* containments method
*
Expand Down

0 comments on commit a5f0197

Please sign in to comment.